File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
CSES/Sorting_and_Searching Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ written by Pankaj Kumar.
3+ country:-INDIA
4+ */
5+ #include < bits/stdc++.h>
6+ using namespace std ;
7+ typedef long long ll;
8+
9+ int solve ()
10+ {
11+ int n,left=0 ;
12+ cin >> n;
13+ map<int , int > m;
14+ // don't use unordered_map because in wrose case it will take O(n) in find operation
15+ int maxo = 0 ;
16+
17+ for (int right = 0 ; right < n; right++)
18+ {
19+ int temp;
20+ cin >> temp;
21+ if (m.find (temp)!=m.end () && m[temp]>=left)
22+ // take O(log(n)) to find element
23+ {
24+ // already selected in current range
25+ left = m[temp] + 1 ;
26+ }
27+ // not in current chunk
28+ m[temp] = right;
29+ maxo = max (maxo, right-left+1 );
30+ }
31+
32+ cout << maxo << endl;
33+ return 0 ;
34+ }
35+ int main ()
36+ {
37+ int testCase = 1 ;
38+ // cin>>testCase;
39+ while (testCase--)
40+ {
41+ solve ();
42+ }
43+ return 0 ;
44+ }
You can’t perform that action at this time.
0 commit comments