-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMax_No.of_Cups.cpp
77 lines (60 loc) · 1.71 KB
/
Max_No.of_Cups.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// /*
// In Turkey, an ice cream parlour gives an offer to a lucky kid.
// The parlour keeps N ice cream cups in a row, and there are different flavours
// of icecreams, where i-th cup filled with the flavour[i] type of ice cream.
// The kid can pick the continuous set of ice cream cups, where the cups are filled
// with icecreams of utmost two different flavours. The kid wants to
// pick the maximum number of ice cream cups from the row.
// You will be given the integer array, flavours[] of size N.
// Your task is to help the kid to pick the maximum number of ice cream cups
// with at most two different flavours.
// Input Format:
// -------------
// Line-1: An integer, number of icecreams.
// Line-2: N space separated integers, flavours[]
// Output Format:
// --------------
// Print an integer result, maximum number of icecream cups can be picked.
// Sample Input-1:
// ---------------
// 10
// 1 2 3 1 1 3 3 2 3 2
// Sample Output-1:
// ----------------
// 5
// Explanation:
// ------------
// The kid can pick the continuous set of icecream cups as follows: 3 1 1 3 3
// Where the cups are filled with two different flavours, 1 and 3.
// Sample Input-2:
// ---------------
// 10
// 2 1 1 3 2 1 3 0 0 3
// Sample Output-2:
// ----------------
// 4
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<int> v(n,0);
for(int i=0;i<n;i++){
cin>>v[i];
}
unordered_map<int,int> m;
int maxi=INT_MIN;
int j=0;
for(int i=0;i<n;i++){
m[v[i]]++;
while(m.size()>2){
m[v[j]]--;
if(m[v[j]]==0){
m.erase(v[j]);
}
j++;
}
maxi=max(maxi,i-j+1);
}
cout<<maxi;
}