Wednesday, June 29, 2022

Mohit and array

 Mohit and array

Problem Statement
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.

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 scan = new Scanner(System.in);
        int n = scan.nextInt();
        int arr[] = new int[n];
        
        
        for(int i=0;i<n-1;i++)
        {
            arr[i] = scan.nextInt();
        }
        int sum = n*(n+1)/2;
        int total = 0;
        for(int i=0;i<n-1;i++)
        {
            //arr[i] = scan.nextInt();
            total = total + arr[i];
        }
        int b = sum - total;
        
        System.out.println(b);
    }
    
}

No comments:

Post a Comment

ads vert

Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody

  Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody < table >      < thead >    ...