-
Notifications
You must be signed in to change notification settings - Fork 71
/
count-repeated-element.cpp
47 lines (42 loc) · 1.01 KB
/
count-repeated-element.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// https://practice.geeksforgeeks.org/problems/count-only-repeated/0/?track=SPCF-Searching&batchId=154
/*
Given an array of positive integers, where elements are consecutive (sorted).
Also, there is a single element which is repeating X (any variable) number of times. Now, the task is to find the
element which is repeated and number of times it is repeated.
*/
#include <iostream>
using namespace std;
void solve_test()
{
int size_list;
cin >> size_list;
int arr[size_list];
for(int i =0; i < size_list; i++)
{
cin >> arr[i];
}
int counter = 0;
int element;
for(int i =0; i < size_list-1; i++)
{
if(arr[i] == arr[i+1])
element = arr[i];
while(arr[i] == arr[i+1])
{
counter +=1;
i+= 1;
}
}
counter+=1;
cout << element << " "<<counter<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long int t;
cin >> t;
while(t--)
solve_test();
return 0;
}