Advertisement
silentkiler029

19#10-B

Apr 28th, 2020
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.21 KB | None | 0 0
  1. ///BISMILLAHIR-RAHMANIR-RAHIM
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. int main()
  6. {
  7.     int t, n;
  8.     scanf("%d", &t);                                    //scanning number of test cases
  9.  
  10.     int i, j, count, len, max_count, dish;
  11.     int *ara;                                           //declaring integer pointer for an array
  12.  
  13.     int *dis_count;                    //another integer pointer for counting number of each distinct element
  14.     int *items;                                         //another integer pointer for storing dish no
  15.  
  16.     while(t--){
  17.         scanf("%d", &n);                                //scanning array size
  18.         ara = (int *) malloc(sizeof(int) * n);          //dynamic memory allocation for the integer pointer
  19.         dis_count = (int *) calloc(sizeof(int), 1005);  //dynamic memory allocation for dis_count
  20.  
  21.         count = 0;
  22.         for(i = 0; i < n; i++){
  23.             scanf("%d", &ara[i]);                       //scanning array
  24.             if(dis_count[ara[i]] == 0) count++;
  25.             dis_count[ara[i]]++;
  26.         }
  27.  
  28.  
  29.         len = count;
  30.  
  31.         printf("total items: %d\n", len);
  32.  
  33.         items = (int *)malloc(sizeof(int) * len);       //dynamic memory allocation for items
  34.  
  35.         for(i = 0, j = 0; i <= 1000; i++){
  36.             if(dis_count[i] != 0){
  37.                 items[j] = i;                           //storing items
  38.                 j++;
  39.             }
  40.         }
  41.  
  42.         max_count = 0;
  43.         for(i = 0; i < len; i++){
  44.             count = 0;
  45.             for(j = 0; j < n; ){
  46.                 if(items[i] == ara[j] && ara[j+1] != ara[j] && ara[j] != ara[j - 1]){
  47.                     count++;                                    //counting under condition
  48.                     j+=2;
  49.                 }
  50.                 else if(items[i] == ara[j]){
  51.                     j += 2;
  52.                 }
  53.                 else j++;
  54.             }
  55.             if(count > max_count){
  56.                 max_count = count;                              //preserving maximum number of count
  57.                 dish = items[i];                                //preserving according dish number
  58.             }
  59.         }
  60.  
  61.         printf("%d\n", dish);
  62.     }
  63.  
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement