-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissingNumber.java
More file actions
40 lines (36 loc) · 830 Bytes
/
missingNumber.java
File metadata and controls
40 lines (36 loc) · 830 Bytes
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
public class missingNumber {
public static void main(String[] args) {
int array[] = {1,2,4,5};
Finder obj= new Finder();
System.out.println(obj.xor(array));
}
}
class Finder
{
int summation(int[] a)
{
int N= a[a.length-1];
int S1= (N* (N+1))/2;
int S2=0;
for (int i = 0; i < a.length; i++) {
S2+=a[i];
}
return S1- S2;
}
int xor(int[] a)
{
// int xor1=0, xor2=0;
for(int i=1; i<= a.length; i++)
{
if((a[i-1]^ i)!= 0)
{
return i;
}
// xor1= xor1^a[i-1];
// xor2= xor2 ^ i;
// System.out.printf("%d %d %d %d\n", a[i-1], xor1, i, xor2);
}
return -1;
// return xor2 - xor1;
}
}