Created
August 9, 2024 07:34
-
-
Save rishabhgargg/6c71442fd05727e0f55592c39240bc13 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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