Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
a set of numbers which are in sequence are arranged in decending order as string s.but
one of the number in sequence is missing in between in the string s.this string s is passed as input to the program.this program must identify the missing number m and print it as output.

sample input and output:

input:

600599598596

output:

597


What I have tried:

help me to solve this program.
Posted
Updated 11-Mar-18 9:08am
v2
Comments
PrafullaVedante 11-Mar-18 11:06am    
Your input is not in ascending order. Either the problem statement or the input value is wrongGryffindor.
Patrice T 11-Mar-18 11:15am    
That is why statement says "descending order"

Look at the input string:
600599598596
The first thing to do is to identify the sequence.
Start by assuming the highest number has 1 digit:
6,0,0,5,9,9,5,9,8,5,9,6
Does that work? No - because you have two zeros together.
So try with 2 digits:
60,05,99,59,85,96
Does that work? No - 99 is larger than 05
Next three digits:
600,599,598,596
Dos that work? Yes. Each of the values is less than the previous one, and most of them have a difference of one.
What number is missing? 597

You can to that manually just like that, and your task is to automate the process - but that's just the same as doing it manually, but in code. All you need to do is find the right number length by extracting each set of characters and converting that to a number. You can then look at the number sequences and see what you have, and if it's a descending sequence.

Pick a language from the 5 very different ones you tagged, and give it a go: it's not that complex when you start by doing it manually first to work out how.
 
Share this answer
 
Comments
Maciej Los 11-Mar-18 13:07pm    
Well explained!
CPallini 11-Mar-18 15:17pm    
5.
In addition to OriginalGriff[^]'s solution, i'd like to provide sample code with bit of improvement.
My solution is Linq based:
C#
string sentence = "600599598596";
//string sentence = "60595856";
int len = sentence.Length;
int half = len/2;
//here is a magic!
    var dividers =  Enumerable.Range(1, half).Where(i=>len%i==0).ToList();
foreach(int d in dividers)
{
    //create list of chars for later use
    List<int> numbers = new List<int>();
    //loop through the chars in sentence
    //[i] - counter
    int i = 0;
    do
    {
        //get number of chars accordingly  to the value stored in [d]
        string tmp = string.Join("", sentence.Skip(i).Take(d));
        //try parse string into integer
        //if possible, then add it to the list
        int number = 0;
        if (Int32.TryParse(tmp, out number)) numbers.Add(number);
        i+=d;
    } while (i<len);
    //find missing value
    var misVal = numbers.Zip(numbers.Skip(1), (first, second) => Tuple.Create(first, second, first-second-1))
        .Where(x=>x.Item3==1)
        .Select(x=> x.Item2+1)
        .FirstOrDefault();
    Console.WriteLine("For list: [{0}] missing value is: {1}", string.Join(";", numbers), misVal);
}


Result:
For list: [6;0;0;5;9;9;5;9;8;5;9;6] missing value is: 0
For list: [60;5;99;59;85;96] missing value is: 0
For list: [600;599;598;596] missing value is: 597
For list: [600599;598596] missing value is: 0


In case you'd like to return list of missing values, change last query into this form:
C#
var misVal = numbers.Zip(numbers.Skip(1), (first, second) => Tuple.Create(first, second, first-second-1))
	.Where(x=>x.Item3==1)
	.Select(x=>x.Item2+1)
	.ToList();


Feel free to change the code to your needs!
 
Share this answer
 
v2
Comments
CPallini 11-Mar-18 15:17pm    
5.
Maciej Los 11-Mar-18 15:22pm    
Thank you, Carlo.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900