Recently our Paroothi got a crush on a girl , as we all know he is not good with the girls so he asked Vikrant ( The stud ) for expert advise and he told him to take her to her favourite restaurant .
It is known that there are K restaurants in the city numbered from 1 to K.Now Paroothi has a list which contains N integers where i th integer represent that she was at a[i] th restaurant at i th day , but as we know he is not good in counting so he asked you to help him decide his crush's favourite restaurant.
NOTE:- Her favourite restaurant is where she goes maximum number of days.
INPUT
First line of input contains two integers N ( 1<N<1000000 ) and K ( 1<K<1000000 ) . Next line contains N integers ( each less than K ) denoting at which restaurant she was at ith day.
OUTPUT
Output contains a single integer representing her favourite restaurant.
NOTE:- if there are more than one restaurants that she visit maximum number of time print the one with smallest value .
SAMPLE INPUT
8 6
2 1 2 3 4 2 3 3
SAMPLE OUTPUT
2
Explanation
She visited restaurant "2" and restaurant " 3" both 3 times which is greater than all others so 2 will be the answer because it smallest among (2,3).
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to parse problem: Given N and K, and N integers a[i] (each between 1 and K? The problem says each less than K, but K is number of restaurants numbered from 1 to K. "each less than K" might be mis-stated; likely each integer is between 1 and K inclusive? Actually "each less than K" would be < K, so max K-1, but that seems odd. Sample: K=6, values include 2,1,2,3,4,2,3,3. All are less than 6, yes (max 4). But can they be 0? Usually they number from 1 to K. So I'll assume each integer is in [1, K]. Possibly they wrote "less than K" meaning <=K? We'll treat as 1..K inclusive. N up to 1e6, K up to 1e6. We need to find the restaurant that appears most frequently; tie: smallest number.
Simple: count frequencies using an array of size K+1 (since K up to 1e6, okay memory wise ~8MB if int). N also up to 1e6, fine. Or we can use map. But array is simpler.
Constraints: 1<N<1000000, 1<K<1000000. So both are less than 1e6. We can allocate array of size K+1 using ints (4 bytes each) = about 4 MB for K=1e6, fine. Alternatively we can use short if counts <= N <=1e6, but int safe.
So solution: read N, K. Read N integers. For each, increment freq[val]. Keep track of max count and the corresponding restaurant with smallest number. Since we need smallest in case of tie, we can iterate over restaurants from 1 to K and compute max freq and index. Or while reading we can maintain current best: if freq[val] > maxCount or (freq[val] == maxCount && val < best), update. But careful: updating on increment might need to compare after increment. As we increment freq[val], we can check new count: if freq[val] > maxCount or (freq[val] == maxCount && val < best), then update best = val, maxCount = freq[val]. But note: if there are multiple same val, this works. However, what about when val is not the current best but its count equals maxCount after increment, but we might have best that is smaller than val, then we shouldn't update because best remains smaller. So condition: if count > maxCount, definitely update; else if count == maxCount and val < best, update. That's correct.
But also when we update because count > maxCount, best becomes val. That might cause ties with previous lower counts? Not needed because new max is higher.
But we must also consider the scenario where we never read some restaurant; its freq stays 0. The max count will be at least 1 (since N>1). So fine.
Edge cases: If multiple restaurants with same max count, we output the smallest restaurant number. So our update rule ensures that.
Time: O(N) reading, O(K) maybe not needed if we track while reading. But K can be up to 1e6, and we would still allocate array of size K+1 but might not need to loop over all. The reading loop dominates N. So total O(N+K) ~2e6, fine.
Implementation details: In C, we could use scanf. But we need to ensure fast input due to N up to 1e6. Standard scanf is okay. But also we might need to handle large input; use faster reading. But for simple solution, it's fine.
Potential pitfalls: The note says "each less than K", so values might be 0...K-1? Then if K=6, values 0..5. But sample uses 2,1,2,3,4,2,3,3. Those are all <6 but >0. However, they said "restaurants numbered from 1 to K". So maybe the integers are 1-indexed. Sample: they have restaurant "2" and "3", both appear 3 times, and they output 2. So that matches. If they had 0, they'd not be restaurants. So I'll assume values between 1 and K inclusive. The phrase "each less than K" is likely a mistake; maybe they meant "each less than or equal to K". We'll treat as 1..K.
But also note: "1<N<1000000" and "1<K<1000000". So N and K are between 2 and 999999? Actually "1<N<1000000" means N>1 and N<1000000, so N up to 999999; similarly K up to 999999. So we can allocate array of size K+1 (max 1e6). Good.
Now, I'll write solution.
Pseudo:
#include <stdio.h>
int main() {
int N, K;
scanf("%d %d", &N, &K);
int freq[K+1]; // but VLA may be large; but C99 supports, but maybe better to allocate dynamically? Stack size may be limited for 1e6 ints. Better to use heap: int *freq = calloc(K+1, sizeof(int));
Actually 4MB on stack might be okay on many judges? Some restrict stack to small (e.g., 8MB). 4MB is fine but safe to use calloc.
We'll use calloc to zero.
code
int i, val;
int best = 0, maxCount = 0;
for (i=0; i<N; i++) {
scanf("%d", &val);
freq[val]++;
int count = freq[val];
if (count > maxCount || (count == maxCount && val < best)) {
maxCount = count;
best = val;
}
}
printf("%d\n", best);
free(freq);
return 0;
}
But be careful: best initially 0; but val always >=1; after first increment, best will be set. Works.
Alternatively we could just read all and then loop over restaurants. But that would be O(N+K). Since N and K both up to 1e6, both ways okay. But my method is O(N) only.
One nuance: When we increment freq[val], it's possible that val equals best and count == maxCount, but val == best, so condition (val < best) false, so no update; that's fine.
Also, if there is a tie later: suppose current best is 5 with maxCount=3. Then we increment restaurant 3, count becomes 3. Now we check: count == maxCount and val (3) < best (5) -> true, so update best=3, maxCount=3. That's correct because we want the smallest among ties, and 3 is smaller. But careful: what if there were previous restaurant 2 also with count 3, but we haven't updated best to 2 because maybe we never saw 2 after it reached count 3? Actually if we read 2 after best is 5 with count=3, and we increment 2 from 2 to 3, then count becomes 3, which equals maxCount, and val=2 < best=5 => update best=2. So final best will be the smallest among those that ever achieved the max count. However, there is a scenario: Suppose we have two restaurants: 1 appears 3 times early, we set best=1, maxCount=3. Then later, restaurant 2 appears 3 times, when its count becomes 3, we compare: count == maxCount (3) and val=2 < best=1? false (2<1 is false). So we do not update. Then best remains 1, which is correct (smallest). But suppose we had best=5, and later we see restaurant 3 with count=3, we update to 3. Then later we see restaurant 1 also with count=3? Actually 1 would have been maybe counted earlier. But if we haven't yet counted 1 enough, if 1's count reaches 3 later than 3's count reaching 3, but after best became 3, if 1 reaches 3, then we compare: count == maxCount and val=1 < best=3 => true, update best=1. So final best becomes 1. So this method eventually yields the smallest number among those with maximum frequency because whenever any restaurant's count ties the current max and its number is smaller, we switch to it. So final best will be the smallest number among all restaurants that achieve the global max. But we need to consider that the maxCount could increase. If later some restaurant surpasses the current max, we update accordingly. So final best will be correct.
But is it possible that a restaurant with a smaller number attains the max count earlier, then later another restaurant gets higher count, best updates to that, and then later the smaller restaurant also attains that higher count? Not possible because if a smaller restaurant attains the same count later, but best currently has the higher count? Actually scenario: best=1 with count=3 initially. Then later restaurant 2 gets to count=4, update best=2, maxCount=4. Then later restaurant 1 gets additional increments to become count=4 as well, then at that moment, count == maxCount (4) and val=1 < best=2, so update best=1. So final best=1 correct.
But what if after best=2 with maxCount=4, we never see restaurant 1 again, so it remains at count 3. That's fine. So the algorithm works.
Thus solution is straightforward.
However, note that the input constraints: N up to 1e6 and K up to 1e6. So we need to ensure we don't exceed memory/time. Using calloc for frequencies: size (K+1)*sizeof(int). 4MB for K=1e6, which is fine.
Edge: If K is close to 1e6, but N is 1e6, so frequencies will be at most 1e6. So int (32-bit) is enough.
Potential optimization: Use short or unsigned short? But maximum count could be up to N (1e6), which doesn't fit in 16-bit (max 65535). So need 32-bit.
Thus final C code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int N, K;
scanf("%d %d", &N, &K);
int freq = (int)calloc(K+1, sizeof(int));
if (!freq) return 1;
int best = 0, maxCount = 0;
for (int i = 0; i < N; i++) {
int val;
scanf("%d", &val);
freq[val]++;
int cnt = freq[val];
if (cnt > maxCount || (cnt == maxCount && val < best)) {
maxCount = cnt;
best = val;
}
}
printf("%d\n", best);
free(freq);
return 0;
}
But note: val might be out of range? But according to problem, each integer is less than K, so if K=6, val max 5? But then freq array size K+1 (7) can hold index up to 6; if val=6? Not allowed. But we might want to be safe: if val > K or val < 1, ignore? But problem says valid.
Now if there is a tie with same count but smaller number appears later? Our algorithm handles.
Thus solution ready.
But the problem statement: "NOTE:- if there are more than one restaurants that she visit maximum number of time print the one with smallest value." So good.
Now we just need to output answer.
Potential issues: Input format: First line contains N and K, second line contains N integers. Could there be multiple lines? It says next line contains N integers. So we can read all in one line with scanf as we did.
Large input: using scanf might be okay but to be safe we could use getchar fast input. But for 1e6 numbers, scanf is fine in most judges; but some might be strict. However, typical CodeChef/SPOJ? The problem seems from some platform. But I'll provide a solution with getchar for speed. Actually we can implement fast integer reading.
Simplest: Use fread to read whole input and parse. But maybe not needed.
We'll provide a solution with getchar_unlocked for faster I/O if available. But to be generic, we can use scanf.
But careful: Some judges treat stack overflow if we declare large VLA. So we'll use dynamic memory.
Thus final code.
Potential pitfalls: For K up to 1e6, we allocate K+1 ints; that's 4 MB on heap, fine.
Now I'll produce final answer.
But the user asked: "Recently our Paroothi got a crush on a girl ..." They want code? The user asked: "Now Paroothi has a list which contains N integers ... but as we know he is not good in counting so he asked you to help him decide his crush's favourite restaurant." So they need a solution.
We'll output C code.
Also note: The constraints: N and K are less than 1,000,000, but maybe they could be 1,000,000 exactly? It says 1<N<1000000, so N is strictly less than 1,000,000, so maximum 999,999. K also. So array size K+1 <= 1,000,000. Good.
We'll also handle the possibility that K may be less than the maximum value in list? Not per spec.
Now code.
But the problem says "each less than K". For K=6, values could be 5 max. But sample includes 4, not 5. So they probably meant <= K. But anyway, we allocated K+1 indices; so if any value equals K, it's within range (since indices 0..K inclusive). So fine.
Thus final answer: C program.
We'll also mention the logic.
Now produce final output. ```c
#include <stdio.h>
#include <stdlib.h>
int main() {
int N, K;
scanf("%d %d", &N, &K);
code
// allocate frequency array, indices 0..K (we use only 1..K)
int *freq = (int*) calloc(K + 1, sizeof(int));
if (freq == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return 1;
}
int best = 0; // smallest restaurant with maximal visits
int maxCount = 0; // number of visits of best
for (int i = 0; i < N; ++i) {
int x;
scanf("%d", &x);
freq[x]++;
int cur = freq[x];
// update best if we have a new record or a tie with a smaller number
if (cur > maxCount || (cur == maxCount && x < best)) {
maxCount = cur;
best = x;
}
}
printf("%d\n", best);
free(freq);
return 0;