Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Q18-Mohit has an array of N integers containing all elements from 1 to N, somehow he lost one element from the array.
- Given N-1 elements your task is to find the missing one.
- Ans-import java.io.*; // for handling input/output
- import java.util.*; // contains Collections framework
- // don't change the name of this class
- // you can add inner classes if needed
- class Main {
- public static void main (String[] args){
- // Your code here
- Scanner s=new Scanner(System.in);
- int N=s.nextInt();
- int arr[]=new int[N];
- for(int i=0;i<N-1;i++){
- arr[i]=s.nextInt();
- }
- Arrays.sort(arr);
- int sumOfAll = (N*(N+1))/2;
- int sumOfArray = 0;
- for(int j=0; j<=N-1; j++) {
- sumOfArray = sumOfArray+arr[j];
- }
- int missingNumber = sumOfAll-sumOfArray;
- System.out.println(missingNumber);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement