How do I count the number of different elements in an array

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;
}

Last edited on
If you use a mixture of spaces and tabs to indent your code, most forums will turn the result into crap.
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 = 0;
  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;
}


> int arr[n];
Variable length arrays are not legal in C++.



example input would be:
1
2
3
4
5
6
6
1 1 2 3 3 4 3
3
1 1
1 3
3 6

There are 7 numbers in the second line of input.
If you're feeding this into your program, then cin >> q is getting the last number from line 2 and everything else is screwed.



Last edited on
countDistinct is not even close.
I don't see how you would expect that to count distinct values.
Think about it.
@salem c
> int arr[n];
Variable length arrays are not legal in C++.

I think it doesn't matter. To me it looks like a codechef(or some other platform) challenge where they use GCC which allows it.
The key to portability is the thing between your ears, not whatever any particular compiler lets you get away with.

You shouldn't care about what compiler you're using.
Your code shouldn't care about what compiler gets used.


1
2
3
4
5
6
7
8
$ g++ -Wall -Wextra -pedantic bar.cpp
bar.cpp: In function ‘int main()’:
bar.cpp:20:14: warning: ISO C++ forbids variable length array ‘arr’ [-Wvla]
     int arr[n];
              ^
bar.cpp:26:13: warning: ISO C++ forbids variable length array ‘ar’ [-Wvla]
  int ar[q][2];
             ^


Besides, there is no mechanism to determine how much stack space is currently available. The only 'out' in case it all goes wrong is a segfault.

At least with int *arr = new int[n]; or std::vector<int> arr(n); there are opportunities to catch exceptions and have some kind of plan to either recover or exit gracefully.



Not legal is not legal. Are those "challenges" to be "won" by any means necessary? (For example, by making someone else do the work for you?)

C++ Standard Library has all the necessary tools to complete this task legally. If the challenge is to show what you know and you don't know how to use standard library, then you should fail, IMHO.
I think we are not taking about "real" code or people wanting to learn proper coding..

Are those "challenges" to be "won" by any means necessary? (For example, by making someone else do the work for you?)

I have seen many solutions on Codechef written in a style like this and it seems people there are only concerned about an accepted answer and rating points - especially people using C or C++.
For some reason solutions in Java or C# are very often use good coding practices.
And yes it seems many people just copy from others.
@ZZEZ73,
Did you actually ask a question in your original post?

One way of counting distinct elements is to use std::set - just construct it with the relevant subarray and return its size.

If the C(:) array were sorted then there are much faster methods (if you are doing several queries). However, that doesn't appear to be the case - from your example at least.
Topic archived. No new replies allowed.