I have a text file which contains numbers from 1 to 1 0 0 , 0 0 0 inclusive, but exactly one of the number is missing.
What is the missing number?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
I used Python coding as follows:
s = [1,2,3,4...] * copy the data of the file here. s = sorted(s) for i in range(len(s)): if int(s[i]) != i+1: print i+1 break
Python's set class makes it easy.
Python 2.7:
1 2 3 |
|
Javascript:
1 2 3 4 5 6 7 8 9 10 |
|
Pretty simple question. Just make an array with the numbers and then put it in a for loop with i = 0 to i=100000 and check if the indexes of the array match with the numbers. If not, then break. The answer turns out to be a simple 77777. Pretty simple, huh :)
class findNum {
public static void main(String args[]) {
String s = "1, 2, 3 ..."
String[] arr = s.split(", ");
int[] test = new int[100000];
for (int i =0;i<arr.length;i++) {
test[i] = Integer.parseInt(arr[i]);
}
int answer = 0;
for(int i = 0;i<100001;i++){
if(test[i]!=(i+1)){
answer = i+1;
break;
}
}
System.out.println(answer);
}
}
It's easy if you analyse a particular column in notepad. Before the missing number i.e 77777 all the entries in that column will be odd but after the number is missed, the subsequent entries have even number in the end. (Given that only one number is misplaced).
The probability of the number being 7 7 7 7 7 was 1 0 0 0 0 0 1 so there are changes that it might be 7 7 7 7 7 so I verified with 'Find' command and guess what I was RIGHT !!!!!!!!!!!
Problem Loading...
Note Loading...
Set Loading...
Execution time (apart from time taken to read the file) ~ 0.03 seconds