Created
September 21, 2014 15:46
-
-
Save Szil/57f64fd2ece779fce64f 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; | |
} | |
static int missingNumV2(Integer... array) { | |
int sum = 5050; | |
int c = 0; | |
for (Integer s : array) c = c + s; | |
return sum - c; | |
} | |
public static void main(String[] args) { | |
initRiddle(); | |
System.out.println(findMissingNumber(riddleArray)); | |
System.out.println(missingNumV2(riddleArray)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment