GoConqr has a new look! Let us know what you think.

Flashcards

Find the Missing Number

Suhas S English 1 card

Cards in this deck (1)

  1. 1You are given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers is missing in the list. Write an efficient code to find the missing integer.class Main
    {
    // Function to find missing number
    static int getMissingNo (int a[], int n)
    {
    int x1 = a[0];
    int x2 = 1;
    /* For xor of all the elements
    in array */
    for (int i = 1; i < n; i++)
    x1 = x1 ^ a[i];
    /* For xor of all the elements
    from 1 to n+1 */
    for (int i = 2; i <= n+1; i++)
    x2 = x2 ^ i;
    return (x1 ^ x2);
    }
    /* program to test above function */
    public static void main(String args[])
    {
    int a[] = {1,2,4,5,6};
    int miss = getMissingNo(a,5);
    System.out.println(miss);
    }
    }

Information

Description
Flashcards on Find the Missing Number, created by Suhas S on 21/03/2018.
Created by
Suhas S
Views
0
Rating

Feedback

What is it about?

What do you think of the new look?

How do you like it?