subarray question

closed account (1vf9z8AR)
Question link-
https://www.hackerearth.com/practice/algorithms/searching/linear-search/practice-problems/algorithm/the-normal-type/description/

Issue-
Code doesn't pass for all test cases. Those test cases are too big to analyse

My logic-
Insert in a queue, if no of distinct elements till now is same as total distinct then pop front of queue and remove that element from unique

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
  #include<iostream>
#include<queue>
#include<set>
using namespace std;
int main()
{
    int n;
    cin>>n;
    queue<int>q;         //takes elements
    set<int>s,dis;    //s keeps count of current unique elements, dis for totol unique
    int arr[n];      //array for taking input
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        dis.insert(arr[i]);
    }
    int k=dis.size();       //k is number of unique elements
    long long int ans=0;
    for(int i=0;i<n;i++)
    {
        s.insert(arr[i]);
        q.push(arr[i]);
        int prev=q.front();
        if(s.size()==k)           //if unique elements till now is same as total unique
        {
            while(q.front()==prev)
            {
                ans+=n-i;
                q.pop();
            }
            s.erase(s.find(prev));
        }
    }
    cout<<ans;
    return 0;
}
Last edited on
4
1 2 1 3
you give 1 as answer, but it should be 2



about the problem
In an array, which consists of N elements, A1, A2, ..., AN, if a subarray has the total number of distinct elements as that of the original array, that determines the presence of Team Rocket.
at least keep the story going, invent something about the meaning of the numbers
and don't use a trademark
closed account (1vf9z8AR)
@ne555

Happy new year

Are you talking about the bad quality of question?
Last edited on
Topic archived. No new replies allowed.