A set of numbers which are in sequence are arranged in descending order as a string S. But one of the number in the sequence is missing in between in the string S. This string S is passed as input to the program. The program must identify the missing number M and print it as output.
Input Format:
The first line will contain the set of numbers in the sequence.
Boundary Conditions:
1 <= M <= 99999
Length of string S is from 5 to 200.
Output Format:
The first line will contain the missing number M.
Example Input/Output 1:
Input:
98764321
Output:
5
Explanation:
The numbers a sequence in descending order are 9 8 7 6 5 4 3 2 1. As 5 is missing, it is printed as the output.
Example Input/Output 2:
Input:
601600598597596
Output:
599
Explanation:
The numbers a sequence in descending order are 601 600 599 598 597 596. As 599 is missing, it is printed as the output.
#include <stdio.h>
long int n(char *a){
long int b=0;
for(int i=0;i<strlen(a);i++){
if(isdigit(a[i])&&a[i])
b=b*10+a[i]-'0';
}
return b;
}
void stcpy(char *a,char *b,int i){
int n=0,l=strlen(b);
for(n=0;n<l&&n<i;n++){
a[n]=b[n];
}
a[n]=' ';
}
int main(){
char a[1000],b[3][10];
scanf("%s",a);
int i,k,k1,l=strlen(a),f=0;
i=5;
while(i>0){
stcpy(b[0],a,i);
stcpy(b[1],a+i,i);
stcpy(b[2],a+i,i-1);
int k[3]={n(b[0]),n(b[1]),n(b[2])};
if(k[0]-k[1]==1||k[0]-k[1]==2||k[0]-k[2]==1||k[0]-k[2]==2){
f=k[0];
break;
}
i--;
}
for(int i=0;i<l;i++){
sprintf(b[0],"%d",f);
if(strncmp(a+i,b[0],strlen(b[0]))==0)
i+=strlen(b)-1;
else{
printf("%d",f);
break;
}
f--;
}
}
This code is designed to find the missing number in a sequence of numbers that are arranged in descending order as a string. The input is a string of numbers, and the program needs to identify the missing number and print it as output. Let’s break down the code and create a simple algorithm to explain it:
Algorithm:
- Read the input string
a
. - Define a function
n
to convert a substring to a long integer.- Initialize
b
to 0. - Loop through the characters of the string.
- If the character is a digit, convert it to an integer and add it to
b
. - Return
b
as the converted integer.
- Initialize
- Define a function
stcpy
to copy the firsti
characters from stringb
to stringa
.- Initialize
n
to 0. - Loop until
n
reachesi
or the end ofb
. - Copy each character from
b
toa
. - Null-terminate
a
after copying.
- Initialize
- In the
main
function:- Define an array
b
to hold substrings of the input. - Read the input string
a
from the user. - Initialize variables
i
,k
,k1
,l
, andf
for loop control and storage. - Set
i
to 5, as we start looking for a missing number in a 5-digit sequence.
- Define an array
- Run a loop while
i
is greater than 0:- Use the
stcpy
function to split the input string into three substringsb[0]
,b[1]
, andb[2]
. - Convert these substrings to integers and store them in an array
k
. - Check if the difference between
k[0]
andk[1]
is 1 or 2, or if the difference betweenk[0]
andk[2]
is 1 or 2. - If any of these conditions are met, set
f
tok[0]
and break out of the loop. - If not, decrease
i
by 1 and continue the loop to check smaller sequences.
- Use the
- Run a loop to compare the substrings of the input with the value of
f
.- Convert
f
to a string and store it inb[0]
. - Check if the substring of
a
starting from the current position matchesb[0]
. - If it does, skip the length of
b[0]
characters in the input string. - If not, print
f
as the missing number and break out of the loop. - Decrease
f
by 1 to check for the next missing number.
- Convert
Leave a Reply