-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmissing_no_array.java
43 lines (40 loc) · 1.17 KB
/
missing_no_array.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
// Missing number in an array
// Given an array of size N-1 such that it only contains distinct integers in the range of 1 to N. Find the missing element.
/*
Input:
N = 5
A[] = {1,2,3,5}
Output: 4
*/
import java.util.Scanner;
class missing_no_array{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int ar[] = new int[n];
for(int i=0;i<ar.length-1;i++){
ar[i] = s.nextInt();
}
System.out.println(MissingNumber1(ar,n)); // Method 1 : Summation Formula
// Integer overflow could happen in method 1
System.out.println(MissingNumber2(ar,n)); // Modification for integer overflow
}
static int MissingNumber1(int array[], int n) {
// Your Code Here
int sum = (n*(n+1))/2;
int ar_sum=0;
for(int i=0;i<array.length;i++){
ar_sum+=array[i];
}
return sum-ar_sum;
}
static int MissingNumber2(int array[],int n){
int sum = 1;
int c = 2;
for(int i=0;i<array.length;i++){
sum-=array[i]+c;
c++;
}
return sum;
}
}