Strictly Increasing Array | Practice | GeeksforGeeks

Given an array nums[] of N positive integers. Find the minimum number of operations required to modify the array such that array elements are in strictly increasing order (A[i] < A[i+1]).
Changing a number to greater or lesser than original number is counted as one operation.

Example 1:
Input: nums[] = [1, 2, 3, 6, 5, 4]
Output: 2
Explanation: By decreasing 6 by 2 and
increasing 4 by 2, arr will be like
[1, 2, 3, 4, 5, 6] which is stricly 
increasing.


Example 2:
Input: nums[] = [1, 2, 3, 4]
Output: 0
Explanation: Arrays is already strictly
increasing.

 
Your Task:
You don't need to read or print anything. Your task is to complete the function min_opeartions() which takes the array nums[] as input parameter and returns the minimum number of opeartion needed to make the array strictly increasing.
 
Expected Time Complexity: O(n^2)
Expected Space Complexity: O(n)
 
Constraints: 
1 <= length of array <= 1000
1 <= arr[i] <= 1000000
0
lindan1234 months ago
int min_operations(vector<int>nums){
		    
		    int n = nums.size();
		    int dp[n];
		    
		    for(int i=0;i<n;i++)
		    {
		        dp[i] =1;
		    }
		    int ans=1;
		    for(int i=1;i<n;i++)
		    {
		        for(int j=0;j<i;j++)
		        {
		            if(nums[i]>nums[j] && dp[i]<dp[j]+1 && (i-j)<=nums[i]-nums[j])
		            {
		                dp[i] = max(dp[i],dp[j]+1);
		            }
		        }
		        ans = max(ans,dp[i]);
		    }
		    return n-ans;
		}
Time Taken : 0.0
Language : Cpp
0
shivambhadani1235 months ago
		int min_operations(vector<int>nums){
		    return nums.size()-lis(nums);
		}
		int lis(vector<int>& nums) {
		    int n = nums.size();
		    int dp[n];
            dp[0] = 1;
            for(int i=1; i<n; i++) {
                dp[i] = 1;
                for(int j=i-1; j>=0; j--) {
                    if(nums[i]<=nums[j]) continue;
                    if(i-j<=nums[i]-nums[j])
                        dp[i] = max(dp[i], 1+dp[j]);
                }
            }
            return *max_element(dp, dp+n);
		}
0
rohitpendse1386 months ago
class Solution {
    int lis(vector<int> &nums, int n) {
        int dp[n];
        dp[0] = 1;
        for (int i = 1; i < n; i++) {
            dp[i] = 1;
            for (int j = 0; j < i; j++) {
                if (nums[j] < nums[i] 
                    && dp[i] < 1 + dp[j]
                    && (i - j) <= nums[i] - nums[j]) {
                    dp[i] = 1 + dp[j];
                }
            }
        }
        int ans = 1;
        for (int i = 0; i < n; i++) {
            ans = max(ans, dp[i]);
        }
        return ans;
    }

public:

    int min_operations(vector<int> &nums) {
        // Code here
        int n = nums.size();
        return n - lis(nums, n);
    }
};
0
rezashahriari9 months ago
rezashahriari
C++ using DP
int min_operations(vector<int>nums){   int n = nums.size();   int LIS[n];   int res=0;   if(n==1)      return 0;   for(int i = 0 ; i < n ;i++)      LIS[i]=1;   for(int i =0; i < n ;i++)      for(int j = 0 ; j nums[j]&&(i-j)<=(nums[i]-nums[j]))            LIS[i]=max(LIS[i],LIS[j]+1);                     res=max(res,LIS[i]);      }   return n-res;    }
0
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.02
class Solution{    public:    int min_operations(vector<int> arr)    {        int N = arr.size();                vector<int> dp(N, 1);                for(int i = 1; i < N; i++)        {            for(int j = 0; j < i; j++)            {                if(arr[j] < arr[i] and (i - j) <= arr[i] - arr[j])                {                    dp[i] = max(dp[i], dp[j] + 1);                }            }        }                return N - *max_element(dp.begin(), dp.end());    }};
-1
raghav1 year ago
raghav
USING BINARY INDEX TREE NLOG SOLUTION FOR NO DULICATES ALLOWED C++ :: NOTE - IT WILL NOT PASS TEST CASE HERE THOUGH void add( int t[] , int k , int x , int N ){    while( k<= N )    {        t[k] += x ;        k += k&-k;    }}int sum( int t[] , int k ){    int s = 0 ;    while( k >= 1 )    {        s += t[k] ;         k -= k&-k ;    }     return s ; }struct ti{    public:    int a , b ; };bool c( ti c , ti d ){    if( c.a == d.a )return c.b < d.b ;     return c.a < d.a ; }class Solution{public:int min_operations(vector<int>A ){   int N = A.size() ;   if( N == 0 || N == 1 )return 0 ;   vector< ti > V(N) ;   for( int i = 0 ; i < N ; i++ )  {      V[i].a = A[i] ;       V[i].b = i;   }   sort( V.begin() , V.end() , c );    for( int i = 0 ; i < N ; i++ )  {      auto x = V[i] ;      int in = x.b ;       A[in] = i+1 ;    }  int t[N+1];  memset(t , 0 , sizeof t );   int cnt = 0 ;   for( int i = 0 ; i < N ; i++ )  {      add( t , A[i] , 1 , N );       int s = sum( t , A[i] );       if( s == i + 1)continue;      else      {          add( t , A[i] , -1 , N );           cnt++;           add( t , i + 1 , 1 , N );       }  }   return cnt  ;  }};
GFG SOLUTION EASY BUT N^2 TIME COMPLEXITYclass Solution{public:int min_operations(vector<int>A ){    int N = A.size();   int t[N];    memset( t , 0  , sizeof t );    t[0] = 1 ;     for( int i = 1 ; i < N ; i++ )   {       t[i] = 1 ;       for( int j = 0 ; j < i ; j++ )       {           if( A[i] > A[j] && A[i] - A[j] >= i -j && t[i] < t[j] + 1 )           {              t[i] = t[j] + 1 ;            }       }   }     return N-*max_element( t, t+N ) ; }};
-1
aditya anand2 years ago
aditya anand
class Solution{public:int min_operations(vector<int>arr){    int n=arr.size();    vector<int> table(n,1);            for(int i=n-1;i>=0;i--){                for(int j=i+1;j<n;j++){ if(arr[j]="">arr[i] && (arr[j]-arr[i])>=(j-i) && (1+table[j])>table[i])                        table[i]=1+table[j];                }            }            int m=-1;            for(int i=0;i<n;i++) {="" if(table[i]="">m){                    m=table[i];                }            }            return n-m;    }
+1
best2 years ago
best
Very similar to LIS. just keep in mind not take the case 1,2,3,4 because u cant insert elements in between here and if array length is one just return 0
+1
Dnyaneshwar Ware2 years ago
Dnyaneshwar Ware
n^2 works here and so why big constraints like 1e6
0
Malay Pandey2 years ago
Malay Pandey
How does this gives an output of 4.181 3 1 6 1 2 3 6
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.