Skip to content

Instantly share code, notes, and snippets.

@nalin-adh
Created October 3, 2015 15:35
Show Gist options
  • Save nalin-adh/6ec72dca7ad1fedb7e88 to your computer and use it in GitHub Desktop.
Save nalin-adh/6ec72dca7ad1fedb7e88 to your computer and use it in GitHub Desktop.
Missing Number Solution
package com.challenge.missingnumber;
import org.apache.commons.lang.ArrayUtils;
public class MissingNumber {
public static void main(String[] args) {
int[] num = new int[51];
for (int i = 1; i <= 50; i++) {
num[i] = i;
}
// Number at index 40 is deleted explicitly
num = ArrayUtils.remove(num, 40);
// Now finding and printing the missing number
System.out.println(findMissingNum(num));
}
private static int findMissingNum(int[] num) {
for (int i = 1; i <= 50; i++) {
// System.out.println(i);
if (i != num[i]) {
return i;
}
}
return (Integer) null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment