Skip to content

Instantly share code, notes, and snippets.

@Ch-sriram
Created June 3, 2020 04:16
Show Gist options
  • Select an option

  • Save Ch-sriram/f4116e9b4aac7a861be8aa4b85302fc0 to your computer and use it in GitHub Desktop.

Select an option

Save Ch-sriram/f4116e9b4aac7a861be8aa4b85302fc0 to your computer and use it in GitHub Desktop.
Find the two repeating elements in a given array.
// Problem link: https://www.geeksforgeeks.org/find-the-two-repeating-elements-in-a-given-array/
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define ull uint64_t
void countsort_rec(const vector<int> &ar, vector<int> &cnt, ull idx = 0) {
if(idx == ar.size()) return;
cnt[ar[idx]]++;
countsort_rec(ar, cnt, idx+1);
}
void check_rec(const vector<int> &cnt, ull idx = 1) {
if(idx == cnt.size())
return;
if(cnt[idx] == 2)
cout << idx << " ";
check_rec(cnt, idx+1);
}
void repeated_nums(vector<int> &ar) {
vector<int> count(ar.size()+1, 0);
countsort_rec(ar, count);
check_rec(count);
}
int main() {
int t; cin >> t;
while(t--) {
int n; cin >> n;
vector<int> ar(n);
for(int i = 0; i < n; ++i)
cin >> ar[i];
repeated_nums(ar);
cout << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment