Created
October 3, 2015 15:35
-
-
Save nalin-adh/6ec72dca7ad1fedb7e88 to your computer and use it in GitHub Desktop.
Missing Number Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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