the basic explanation is that there will be n number of numbers will be given. then a line of n number will also be given.
after that here will be number Q the total number of test.
so after that line there will be a Q number of lines that is 2 long making. and that given line will give the coordinates of the array to start counting and its ending.
the structure is like this:
N___________________________: the number in the array
C1,C2,C3, ...C(N −1) ,C(N)__: the numbers
Q___________________________: number of tests
L1,R1_______________________: Q number of coordinates
L2,R2
…
LQ,RQ
and the output is :
A1__________________________:the total number of unique number between L1 R1
A2
...
AQ
example input would be:
7
1 1 2 3 3 4 3
3
1 1
1 3
3 6
and the output to be:
1
2
3
to explain it is just the Ln and Rn are the coordinates to start counting and to end counting
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
|
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int countDistinct(int arr[], int b, int p)
{
int res = 1;
for (int i = b - 1; i < p - 1; i++) {
int j = 0;
for (j = 0; j < i; j++)
if (arr[i] == arr[j])
break;
if (i == j)
res++;
}
return res;
}
int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int q;
cin >> q;
int ar[q][2];
for (int i = 0; i < q; i++) {
for (int j = 0; j < 2; j++) {
cin >> ar[i][j];
}
}
for (int i = 0; i < q; i++) {
cout << countDistinct(arr, ar[i][0], ar[i][1]) << endl;
}
return 0;
}
|