Find Missing Number In String
You had a sequence of consecutive nonnegative integers. You appended all integers at the end of each other to form a string ‘S’ without any separators. While appending each integer in a string, you forgot to append exactly one integer from the sequence. Now all the integers from a string and you don’t know which integer you have missed.
For example sequence 11, 12, 13 may form a string (without any separators) “1113” if you miss 12.
Your task is to find the missing number in the string such that it is possible to obtain a sequence of consecutive non-negative integers from the given string. If more than one missing integer is present or all the integers are already present or if the string is not valid then the answer will be -1 for all such cases.
Note:
1. The string consists of only digits 0 to 9.
2. The numbers will have no more than six digits.
Input Format:-
The first line of input contains an integer ‘T’ denoting the number of test cases.
The only line of each test case contains a string of numbers 'S'.
Output Format:-
For each test case, print a single line containing a single integer denoting the missing number.
The output of each test case will be printed in a separate line.
Note:
You don’t have to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100
1 <= |S| <= 10 ^ 4
0 <= S[i] <= 9
Where |S| denotes the length of the given string 'S' and S[i] is the digit at index i.
Time Limit: 1 sec.
CodingNinjas
author
2y
XOR approach can be used to solve this question in an efficient manner. The following property of XOR can be used :
1. If x1^x2^…xn = a and x1^x2^..xn-1 = b , then a^b = xn
Steps:
1. Declare two variable...read more
CodingNinjas
author
2y
Brute Force
- The idea is to try all lengths from 1 to 6 for the first number of the sequence.
- For each length, we check if the current length satisfies the property of all consecutive numbers except one ...read more
Help your peers!
Add answer anonymously...
Top UBS ENO interview questions & answers
Top HR questions asked in UBS ENO
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app