Advertisement
Guest User

Increasing Array

a guest
Nov 12th, 2020
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. /*
  2. You are given an array of n integers.
  3. You want to modify the array so that it is increasing, i.e., every element is at least as large as the previous element.
  4.  
  5. On each turn, you may increase the value of any element by one. What is the minimum number of turns required?
  6.  
  7. Input:
  8. The first input line contains an integer n: the size of the array.
  9. Then, the second line contains n integers x1,x2,…,xn: the contents of the array.
  10.  
  11. Output:
  12. Print the minimum number of turns.
  13.  
  14. */
  15.  
  16. #include <iostream>
  17.  
  18. using namespace std;
  19.  
  20. void minTurns(int n, long int* arr){
  21.   long long turns = 0;
  22.   for(int i=0;i<n;i++){
  23.     if(arr[i+1]<arr[i]){
  24.       turns+=arr[i]-arr[i+1];
  25.       arr[i+1]=arr[i];
  26.     }
  27.     else
  28.       continue;
  29.   }
  30.   cout<<turns;
  31. }
  32.  
  33. int main(){
  34.   int n;
  35.   cin>>n;
  36.   long* arr = new long[n];
  37.   for(int j=0;j<n;j++)
  38.     cin>>arr[j];
  39.   minTurns(n,arr);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement