TCS NQT The Saga of the Missing Coin code solution

The Saga of the Missing Coin

        A teacher came into the class with a large box that has several coins. Each coin has a number printed on it. Before coming to the class, she ensured that all the numbers occur an even number of times. However, while coming to the class once coin fell down and got lost. She wants to find out the number of the missing coin.


Sample Input:

8

5 7 2 7 5 2 5

 

Sample Output

5


Code

#include <iostream>

using namespace std;

int main()

{

    int n;

    cin >> n;

    n--;                           // Actual array size will be number of coins minus 1 as only 1 coin is missing

    int a[n];

    for(int i = 0;i < n;i++){       // Taking all coins

        cin >> a[i];

    }

    for(int i = 0;i < n;i++){                // to hold each array element(coin) one by one

        int count = 0;                            // to keep the count

        for(int j = 0;j < n;j++){             // to check the match of coin

            if(a[i] == a[j]) count++;            // if match found increment the count by one

        }

        if(count % 2 != 0){                    // if the count is not even print that element(coin)

            cout << a[i];

            break;                                        // break the loop to exit

        }

    }

    return 0;

}


Input

8

5 7 2 7 5 2 5


Output

5

Post a Comment

0 Comments