Created
September 21, 2014 15:23
-
-
Save Szil/071b3d5bd83707272ea5 to your computer and use it in GitHub Desktop.
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
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Random; | |
/** | |
* Created by Gergo on 2014.09.19.. | |
*/ | |
public class NinetyNineProblem { | |
static Integer[] riddleArray = new Integer[99]; | |
static Random random = new Random(); | |
static void initRiddle(){ | |
int rand = random.nextInt(101); | |
final int missingNum; | |
if (rand == 0) { | |
missingNum = rand + 1; | |
} else { | |
missingNum = rand; | |
} | |
System.out.println(missingNum); | |
for (int i = 1; i < missingNum; i++) { | |
riddleArray[i-1] = i; | |
} | |
for (int j = missingNum; j < 100; j++) { | |
riddleArray[j-1] = j+1; | |
} | |
} | |
static int findMissingNumber(Integer... array) { | |
ArrayList<Integer> va = new ArrayList<>(); | |
Collections.addAll(va, riddleArray); | |
int counter = 1; | |
Boolean notFound = true; | |
while (notFound) { | |
if (va.contains(counter)){ | |
counter++; | |
} else { | |
notFound = false; | |
return counter; | |
} | |
} | |
return 0; | |
} | |
public static void main(String[] args) { | |
initRiddle(); | |
System.out.println(findMissingNumber(riddleArray)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment