forked from iamAnki/CPP-Programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSACS.cpp
More file actions
75 lines (56 loc) · 1.54 KB
/
SACS.cpp
File metadata and controls
75 lines (56 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to return minimum
// number of moves required
int getMinimumMoves(int n, int k, int d,
vector<int> a)
{
int MAX = 100000;
// Stores the number of moves
// required to obtain respective
// values from the given array
vector<int> v[MAX];
// Traverse the array
for (int i = 0; i < n; i++) {
int cnt = 0;
// Insert 0 into V[a[i]] as
// it is the initial state
v[a[i]].push_back(0);
while (a[i] > 0) {
a[i] /= d;
cnt++;
// Insert the moves required
// to obtain current a[i]
v[a[i]].push_back(cnt);
}
}
int ans = INT_MAX;
// Traverse v[] to obtain
// minimum count of moves
for (int i = 0; i < MAX; i++) {
// Check if there are at least
// K equal elements for v[i]
if (v[i].size() >= k) {
int move = 0;
sort(v[i].begin(), v[i].end());
// Add the sum of minimum K moves
for (int j = 0; j < k; j++) {
move += v[i][j];
}
// Update answer
ans = min(ans, move);
}
}
// Return the final answer
return ans;
}
// Driver Code
int main()
{
int N = 5, K = 3, D = 2;
vector<int> A = { 1, 2, 3, 4, 5 };
cout << getMinimumMoves(N, K, D, A);
return 0;
}