RainX_69

Minimize count of divisions by D to obtain at least K equal array elements | HARD | OA | READ IT

Apr 26th, 2023
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | Source Code | 0 0
  1. https://www.geeksforgeeks.org/minimize-count-of-divisions-by-d-to-obtain-at-least-k-equal-array-elements/
  2.  
  3. Given an array A[ ] of size N and two integers K and D, the task is to calculate the minimum possible number of operations required to obtain at least K equal array elements. Each operation involves replacing an element A[i] by A[i] / D. This operation can be performed any number of times.
  4.  
  5. Examples:
  6.  
  7. Input: N = 5, A[ ] = {1, 2, 3, 4, 5}, K = 3, D = 2
  8. Output: 2
  9. Explanation:
  10. Step 1: Replace A[3] by A[3] / D, i.e. (4 / 2) = 2. Hence, the array modifies to {1, 2, 3, 2, 5}
  11. Step 2: Replace A[4] by A[4] / D, i.e. (5 / 2) = 2. Hence, the array modifies to {1, 2, 3, 2, 2}
  12. Hence, the modified array has K(= 3) equal elements.
  13. Hence, the minimum number of operations required is 2.
  14.  
  15.  
  16. Input: N = 4, A[ ] = {1, 2, 3, 6}, K = 2, D = 3
  17. Output: 1
  18. Explanation:
  19. Replacing A[3] by A[3] / D, i.e. (6 / 3) = 2. Hence, the array modifies to {1, 2, 3, 2}.
  20. Hence, the modified array has K(= 2) equal elements.
  21. Hence, the minimum number of operations required is 1.
  22.  
  23.  
  24. ---------------------------------------------------------------------------------------------------------------------------------------
  25.  
  26. // C++ Program to implement
  27. // the above approach
  28. #include <bits/stdc++.h>
  29. using namespace std;
  30.  
  31. // Function to return minimum
  32. // number of moves required
  33. int getMinimumMoves(int n, int k, int d,
  34.                     vector<int> a)
  35. {
  36.     int MAX = 100000;
  37.  
  38.     // Stores the number of moves
  39.     // required to obtain respective
  40.     // values from the given array
  41.     vector<int> v[MAX];
  42.  
  43.     // Traverse the array
  44.     for (int i = 0; i < n; i++) {
  45.         int cnt = 0;
  46.  
  47.         // Insert 0 into V[a[i]] as
  48.         // it is the initial state
  49.         v[a[i]].push_back(0);
  50.  
  51.         while (a[i] > 0) {
  52.             a[i] /= d;
  53.             cnt++;
  54.  
  55.             // Insert the moves required
  56.             // to obtain current a[i]
  57.             v[a[i]].push_back(cnt);
  58.         }
  59.     }
  60.  
  61.     int ans = INT_MAX;
  62.  
  63.     // Traverse v[] to obtain
  64.     // minimum count of moves
  65.     for (int i = 0; i < MAX; i++) {
  66.  
  67.         // Check if there are at least
  68.         // K equal elements for v[i]
  69.         if (v[i].size() >= k) {
  70.  
  71.             int move = 0;
  72.  
  73.             sort(v[i].begin(), v[i].end());
  74.  
  75.             // Add the sum of minimum K moves
  76.             for (int j = 0; j < k; j++) {
  77.  
  78.                 move += v[i][j];
  79.             }
  80.  
  81.             // Update answer
  82.             ans = min(ans, move);
  83.         }
  84.     }
  85.  
  86.     // Return the final answer
  87.     return ans;
  88. }
  89.  
  90. // Driver Code
  91. int main()
  92. {
  93.     int N = 5, K = 3, D = 2;
  94.     vector<int> A = { 1, 2, 3, 4, 5 };
  95.  
  96.     cout << getMinimumMoves(N, K, D, A);
  97.  
  98.     return 0;
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment