Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rishabhgargg/6c71442fd05727e0f55592c39240bc13 to your computer and use it in GitHub Desktop.
Save rishabhgargg/6c71442fd05727e0f55592c39240bc13 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
using namespace std;
int minCoupons(vector<int>& a) {
int n = a.size();
int coupons = 0;
int prev = a[0];
for (int i = 1; i < n; i++) {
if (a[i] < prev) {
int diff = prev - a[i];
int k = 1;
while (k < n && k + 1 <= diff) {
k++;
}
coupons += k + 1;
a[i] = prev;
} else {
prev = a[i];
}
}
return coupons;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int result = minCoupons(a);
// Output Format: a single integer
cout << result << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment