Advertisement
Idanref

N Efficiency

Dec 21st, 2020
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int biggest_sum(int *arr, int n)
  5. {
  6.     // {1, 4, 2, 2, 3, 3, 2, 4, 2}
  7.     // 0,0,0,0,0,0,0,0,0
  8.     // 0,1,8,6,8,0,0,0,0
  9.     // 0,1,2,3,4,5,6,7,8
  10.     // n = 9
  11.  
  12.     int *count_array;
  13.     count_array = (int*)calloc(n, sizeof(int));
  14.  
  15.     int max = 0;
  16.     int max_index;
  17.  
  18.     for(int i = 0; i < n; i++)
  19.     {
  20.         // int count_arr_value = count_array[arr[i]];
  21.         // int count_arr_index = arr[i];
  22.  
  23.         count_array[arr[i]] += arr[i];
  24.  
  25.         if(count_array[arr[i]] > max)
  26.         {
  27.             max = count_array[arr[i]];
  28.             max_index = arr[i];
  29.         }
  30.  
  31.         else if(count_array[arr[i]] == max)
  32.         {
  33.             if(max_index < arr[i])
  34.                 max_index = arr[i];
  35.         }
  36.     }
  37.  
  38.     return max_index;
  39. }
  40.  
  41. void main()
  42. {
  43.     int arr[9] = {1, 4, 2, 2, 3, 3, 2, 2, 7};
  44.     int x = biggest_sum(arr, 9);
  45.  
  46.     printf("%d", x);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement