Convert to Strictly increasing array - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Sunday 26 July 2020

Convert to Strictly increasing array

Convert to Strictly increasing array
Convert to Strictly increasing array


                Convert to Strictly increasing array

Given an array A of N positive integers. Find the minimum number of operations (change a number to greater or lesser than original number) in array so that array is strictly increasing (A[i] < A[i+1]).

Note : The array should remain array of integers.  For example, consider {6, 9, 7}, we cannot insert 6.x between and 7.

Single line output, print the minimum operations to be done to make it strictly increasing


#include <iostream>
using namespace std;
void findsol(int a[],int i,int cnt)
{
    if(i<0)
    return ;
    if(a[i]<a[i-1])
    ++cnt;
    findsol(a,i-1,cnt);
}
int main() {
    //code
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n],b[n];
        for(int i=0;i<n;++i)
        {
        cin>>a[i];
        b[i]=1;
        }
        int mx=0;
//finding longest increasing subsequence
// substring total -lis to get number change
        for(int i=1;i<n;++i)
        {
            for(int j=0;j<i;++j)
            {
// this check i-j should be less than equal to a[i]-a[j]
                if(i-j<=a[i]-a[j])
                if(a[j]<a[i]&&b[j]+1>b[i])
                b[i]=b[j]+1;
            }
//finding max length
            if(b[i]>mx)
            mx=b[i];
        }
// n-mx number have to change to make lis
        cout<<n-mx<<endl;
    }
    return 0;
}