-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUnsortedArray.java
73 lines (59 loc) · 1.5 KB
/
UnsortedArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package algorithms;
import java.util.ArrayList;
/**
* Determines which number is missing from an unsorted array of values 1-100.
* Iterate through the array and compute the sum of all numbers.
* Now, sum of natural numbers from 1 to N, can be expressed as N(N+1)/2. In your case N=100.
* Subtract the sum of the array from N(N+1)/2, where N=100.
*
* Complexity: O(n)
*
* @author joeytawadrous
*/
public class UnsortedArray
{
public static void main(String[] args)
{
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 0; i < 101; i++) // leave 100 out
{
if(i != 77)
{
list.add(i);
}
}
getMissingNumber(list);
getMissingNumber2();
}
/**
* Prints the missing number in a list of numbers.
* @param list
*/
public static void getMissingNumber(ArrayList<Integer> list)
{
int sum = 0;
for (int i = 0; i < list.size(); i++)
{
sum += list.get(i);
}
// the total sum of numbers between 1 and arr.length.
int total = (list.size() + 1) * list.size() / 2;
System.out.println("Missing number is: " + (total - sum));
}
/**
* Prints the missing number in an array of numbers.
*/
public static void getMissingNumber2()
{
int[] arr = {1,2,3,5};
int indexes = 5;
int values = 0;
for (int i = 0; i < arr.length; i++)
{
indexes += i + 1;
values += arr[i];
}
int result = indexes - values;
System.out.println("Missing number is: " + result);
}
}