Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- int cnt[1000] = {0};
- int arr[1000] = {0};
- int main(){
- int n;
- scanf("%d", &n);
- for(int i = 1; i <= n; i++){
- scanf("%d", &arr[i]);
- }
- int q;
- scanf("%d", &q);
- for(int m = 1; m <= q; m++){
- int l, r;
- int maxCnt = 0;
- int best = 0;
- int value = 100000;
- scanf("%d %d", &l, &r);
- for(int i = l; i <= r; i++){
- cnt[arr[i]]++;
- if(maxCnt < cnt[arr[i]]){
- maxCnt = cnt[arr[i]];
- value = arr[i];
- } else if(cnt[arr[i]] == maxCnt) {
- if(arr[i] < value) value = arr[i];
- }
- }
- printf("%lld\n", value);
- memset(cnt, 0, sizeof(cnt)); //reset the value of the array cnt to 0
- }
- return 0;
- }
- //notes
- // cnt : a new array that counts the total amount of elements scanned
- // 1 1 3
- //e.g cnt[arr[1]] = cnt[1]++ = 0 + 1;
- // cnt[arr[2]] = cnt[1]++ = 1 + 1;
- //update the max amount of count everytime to maxCnt
- //this is to compare the maxCnt right now with the other elements maxCnt
- //if the cnt of element atm is still < maxCnt that means it does nothing and use previous value
- //update value everytime maxCnt is updated
- //else if turns out the cnt[arr[i]] during the scanning are the same as the maxCnt
- //always take the smaller value (according to question)
- //e.g value = 3 and arr[i] = 2, then value will become 2 if they have the same amount of cnt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement