Question Name:Element appearing once binary search

#include<bits/stdc++.h>
using namespace std;

void once(int arr[], int n){
    map<int, int> mp;
    for(int i=0; i<n; i++){
        mp[arr[i]]++;
    }
    for(int i=0; i<n; i++){
        if(mp[arr[i]]==1){
            cout<<arr[i];
            break;
        }
    }
}

int main()
 {
	int t; cin>>t;
	while(t--){
	    int n; cin>>n;
	    int arr[n];
	    for(int i=0; i<n; i++){
	        cin>>arr[i];
	    }
	    once(arr, n);
	    cout<<"\n";
	}
	return 0;
}

Problem Description

Given an sorted array A[i] of N positive integers having all the numbers occuring exactly twice, except for one number which will occur only once. Find the number occuring only once.

Input
The first line of input contains an integer T denoting the number of test cases. Then T test cases
follow. The first line of each test case contains a positive integer N, denoting the size of the array.
The second line of each test case contains a N positive integers, denoting the elements of the
array.

Output
Print out the singly occuring number.

Constraints
1 <= T <= 100
0 < N <= 100
0 <= A[i] <= 100

  • Test Case 1

    Input (stdin)

    2
    5
    1 1 2 5 5
    7
    2 2 5 5 20 30 30
    

    Expected Output

    2
    20
  • Test Case 2

    Input (stdin)

    2
    3
    1 5 5
    5
    2 2 5 5 20
    

    Expected Output

    1
    20

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro