You are given an array of integers. The special property of the array is that exactly two different elements occur once while other elements occur twice.
You are required to determine those two elements.
Example:
Input: n = 8, a = [1,2,3,4,5,5,3,4]
Output: 1 2
Approach
C++
#include <bits/stdc++.h>using namespace std;void detemineNumbers(int n, int a[]){sort(a, a + n);int i = 0;while (i < n){if (i != n - 1 && a[i] == a[i + 1]){i += 2;}else{cout << a[i] << " ";i++;}}}int main(){int n = 8;int a[n] = {1, 2, 3, 4, 5, 5, 3, 4};detemineNumbers(n, a);return 0;}
No comments:
Post a Comment