MCPcopy
hub / github.com/careercup/ctci / initialize

Method initialize

java/Chapter 5/Question5_7/Question.java:8–23  ·  view source on GitHub ↗
(int n, int missing)

Source from the content-addressed store, hash-verified

6public class Question {
7 /* Create a random array of numbers from 0 to n, but skip 'missing' */
8 public static ArrayList<BitInteger> initialize(int n, int missing) {
9 BitInteger.INTEGER_SIZE = Integer.toBinaryString(n).length();
10 ArrayList<BitInteger> array = new ArrayList<BitInteger>();
11
12 for (int i = 1; i <= n; i++) {
13 array.add(new BitInteger(i == missing ? 0 : i));
14 }
15
16 // Shuffle the array once.
17 for (int i = 0; i < n; i++){
18 int rand = i + (int) (Math.random() * (n-i));
19 array.get(i).swapValues(array.get(rand));
20 }
21
22 return array;
23 }
24
25
26 public static int findMissing(ArrayList<BitInteger> array) {

Callers 1

mainMethod · 0.95

Calls 4

swapValuesMethod · 0.80
lengthMethod · 0.45
addMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected