Open In App

Convert to Strictly increasing integer array with minimum changes

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array of n integers. Write a program to find a minimum number of changes in the array so that the array is strictly increasing of integers. In strictly increasing array A[i] < A[i+1] for 0 <= i < n

Examples: 

Input: arr[] = { 1, 2, 6, 5, 4}
Output: 2
We can change a[2] to any value between 2 and 5 and a[4] to any value greater than 5.

Input: arr[] = { 1, 2, 3, 5, 7, 11 }
Output : 0
An array is already strictly increasing.

The problem is variation of Longest Increasing Subsequence. The numbers which are already a part of LIS need not to be changed. So minimum elements to change is difference of size of array and number of elements in LIS. Note that we also need to make sure that the numbers are integers. So while making LIS, we do not consider those elements as part of LIS that cannot form strictly increasing by inserting elements in middle. 

Example { 1, 2, 5, 3, 4 }, we consider length of LIS as three {1, 2, 5}, not as {1, 2, 3, 4} because we cannot make a strictly increasing array of integers with this LIS. 

Implementation:

C++




// CPP program to find min elements to
// change so array is strictly increasing
#include <bits/stdc++.h>
using namespace std;
 
// To find min elements to remove from array
// to make it strictly increasing
int minRemove(int arr[], int n)
{
    int LIS[n], len = 0;
 
    // Mark all elements of LIS as 1
    for (int i = 0; i < n; i++)
        LIS[i] = 1;
 
    // Find LIS of array
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            if (arr[i] > arr[j]
                && (i - j) <= (arr[i] - arr[j])) {
                LIS[i] = max(LIS[i], LIS[j] + 1);
            }
        }
        len = max(len, LIS[i]);
    }
 
    // Return min changes for array to strictly increasing
    return n - len;
}
 
// Driver program to test minRemove()
int main()
{
    int arr[] = { 1, 2, 6, 5, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << minRemove(arr, n);
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta


C




// C program to find min elements to
// change so array is strictly increasing
#include <stdio.h>
 
// Find maximum between two numbers.
int max(int num1, int num2)
{
    return (num1 > num2) ? num1 : num2;
}
 
// To find min elements to remove from array
// to make it strictly increasing
int minRemove(int arr[], int n)
{
    int LIS[n], len = 0;
 
    // Mark all elements of LIS as 1
    for (int i = 0; i < n; i++)
        LIS[i] = 1;
 
    // Find LIS of array
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            if (arr[i] > arr[j]
                && (i - j) <= (arr[i] - arr[j])) {
                LIS[i] = max(LIS[i], LIS[j] + 1);
            }
        }
        len = max(len, LIS[i]);
    }
 
    // Return min changes for array to strictly increasing
    return n - len;
}
 
// Driver program to test minRemove()
int main()
{
    int arr[] = { 1, 2, 6, 5, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("%d", minRemove(arr, n));
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta


Java




// Java program to find min elements to
// change so array is strictly increasing
public class Main {
 
    // To find min elements to remove from array
    // to make it strictly increasing
    static int minRemove(int arr[], int n)
    {
        int LIS[] = new int[n];
        int len = 0;
 
        // Mark all elements of LIS as 1
        for (int i = 0; i < n; i++)
            LIS[i] = 1;
 
        // Find LIS of array
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < i; j++) {
                if (arr[i] > arr[j] && (i - j) <= (arr[i] - arr[j]))
                    LIS[i] = Math.max(LIS[i], LIS[j] + 1);
            }
            len = Math.max(len, LIS[i]);
        }
 
        // Return min changes for array to strictly
        // increasing
        return n - len;
    }
 
    // Driver program to test minRemove()
    public static void main(String[] args)
    {
        int arr[] = { 1, 2, 6, 5, 4 };
        int n = arr.length;
        System.out.println(minRemove(arr, n));
    }
}
 
// This code is contributed by Sania Kumari Gupta


Python3




# Python3 program to find min elements to
# change so array is strictly increasing
 
# Find min elements to remove from array
# to make it strictly increasing
def minRemove(arr, n):
    LIS = [0 for i in range(n)]
    len = 0
 
    # Mark all elements of LIS as 1
    for i in range(n):
        LIS[i] = 1
 
    # Find LIS of array
    for i in range(1, n):
         
        for j in range(i):
            if (arr[i] > arr[j] and (i-j)<=(arr[i]-arr[j]) ):
                LIS[i] = max(LIS[i], LIS[j] + 1)
                 
        len = max(len, LIS[i])
 
    # Return min changes for array
    # to strictly increasing
    return (n - len)
 
# Driver Code
arr = [ 1, 2, 6, 5, 4 ]
n = len(arr)
print(minRemove(arr, n))
 
# This code is contributed by Azkia Anam.


C#




// C# program to find min elements to change so
// array is strictly increasing
using System;
 
class GFG
{
 
    // To find min elements to remove from array to
    // make it strictly increasing
    static int minRemove(int []arr,
                        int n)
    {
        int []LIS = new int[n];
        int len = 0;
 
        // Mark all elements
        // of LIS as 1
        for (int i = 0; i < n; i++)
            LIS[i] = 1;
 
        // Find LIS of array
        for (int i = 1; i < n; i++)
        {
            for (int j = 0; j < i; j++)
            {
                if (arr[i] > arr[j] && (i-j)<=(arr[i]-arr[j]))
                    LIS[i] = Math.Max(LIS[i],
                                LIS[j] + 1);
            }
            len = Math.Max(len, LIS[i]);
        }
 
        // Return min changes for array 
        // to strictly increasing
        return n - len;
    }
 
    // Driver Code
    public static void Main()
    {
        int []arr = {1, 2, 6, 5, 4};
        int n = arr.Length;
 
        Console.WriteLine(minRemove(arr, n));
    }
}
 
// This code is contributed
// by anuj_67.


Javascript




<script>
 
// Javascript program to find min elements to
// change so array is strictly increasing
 
    // To find min elements to remove from array
    // to make it strictly increasing
    function minRemove(arr, n)
    {
        let LIS = new Array(n).fill(0);
        let len = 0;
   
        // Mark all elements of LIS as 1
        for (let i = 0; i < n; i++)
            LIS[i] = 1;
   
        // Find LIS of array
        for (let i = 1; i < n; i++) {
            for (let j = 0; j < i; j++) {
                if (arr[i] > arr[j] && (i-j)<=(arr[i]-arr[j]))
                    LIS[i] = Math.max(LIS[i],
                                 LIS[j] + 1);
            }
            len = Math.max(len, LIS[i]);
        }
   
        // Return min changes for array
        // to strictly increasing
        return n - len;
    }
     
// driver program
     
        let arr = [ 1, 2, 6, 5, 4 ];
        let n = arr.length;
   
        document.write(minRemove(arr, n));
 
// This code is contributed by Code_hunt.
</script>


PHP




<?php
// PHP program to find min elements to change so
// array is strictly increasing
 
// To find min elements to remove from array 
// to make it strictly increasing
function minRemove($arr, $n)
{
    $LIS = array();
    $len = 0;
 
    // Mark all elements
    // of LIS as 1
    for ($i = 0; $i < $n; $i++)
        $LIS[$i] = 1;
 
    // Find LIS of array
    for ($i = 1; $i < $n; $i++)
    {
        for ($j = 0; $j < $i; $j++)
        {
            if ($arr[$i] > $arr[$j])
                $LIS[$i] = max($LIS[$i],
                            $LIS[$j] + 1);
        }
        $len = max($len, $LIS[$i]);
    }
 
    // Return min changes for array to strictly
    // increasing
    return $n - $len;
}
 
// Driver Code
$arr = array(1, 2, 6, 5, 4);
$n = count($arr);
 
echo minRemove($arr, $n);
 
// This code is contributed
// by anuj_6
?>


Output

2

Time Complexity: O(n*n), as nested loops are used
Auxiliary Space: O(n), Use of an array to store LIS values at each index.



Similar Reads

Count elements in Array having strictly smaller and strictly greater element present
Given an array arr[], the task is to find the count of elements in the given array such that there exists an element strictly smaller and an element strictly greater than it. Examples: Input: arr [] = {11, 7, 2, 15}Output: 2Explanation: For arr[1] = 7, arr[0] is strictly greater than it and arr[2] is strictly smaller than it. Similarly for arr[1],
8 min read
Minimum cost to select K strictly increasing elements
Given an array and an integer K. Also given one more array which stores the cost of choosing elements from the first array. The task is to calculate the minimum cost of selecting K strictly increasing elements from the array.Examples: Input: N = 4, K = 2ele[] = {2, 6, 4, 8}cost[] = {40, 20, 30, 10}Output: 30Explanation:30 is the minimum cost by sel
15 min read
Find an element in an array such that elements form a strictly decreasing and increasing sequence
Given an array of positive integers, the task is to find a point/element up to which elements form a strictly decreasing sequence first followed by a sequence of strictly increasing integers. Both of the sequences must at least be of length 2 (considering the common element).The last value of the decreasing sequence is the first value of the increa
10 min read
Split the array elements into strictly increasing and decreasing sequence
Given an array of N elements. The task is to split the elements into two arrays say a1[] and a2[] such that one contains strictly increasing elements and the other contains strictly decreasing elements and a1.size() + a2.size() = a.size(). If it is not possible to do so, print -1 or else print both the arrays. Note: There can be multiple answers an
7 min read
Minimize division by 2 to make an Array strictly increasing
Given an array nums[] of size N, the task is to find the minimum number of operations required to modify the array such that array elements are in strictly increasing order (A[i] &lt; A[i+1]) where in each operation we can choose any element and perform nums[i] = [ nums[i] / 2] (where [x] represents the floor value of integer x). Examples: Input nu
6 min read
Check whether an array can be made strictly increasing by modifying atmost one element
Given an array arr[] of positive integers, the task is to find whether it is possible to make this array strictly increasing by modifying atmost one element.Examples: Input: arr[] = {2, 4, 8, 6, 9, 12} Output: Yes By modifying 8 to 5, array will become strictly increasing. i.e. {2, 4, 5, 6, 9, 12}Input: arr[] = {10, 5, 2} Output: No Approach: For e
8 min read
Maximum length of Strictly Increasing Sub-array after removing at most one element
Given an array arr[], the task is to remove at most one element and calculate the maximum length of strictly increasing subarray. Examples: Input: arr[] = {1, 2, 5, 3, 4} Output: 4 After deleting 5, the resulting array will be {1, 2, 3, 4} and the maximum length of its strictly increasing subarray is 4. Input: arr[] = {1, 2} Output: 2 The complete
10 min read
Minimize the number of strictly increasing subsequences in an array | Set 2
Given an array arr[] of size N, the task is to print the minimum possible count of strictly increasing subsequences present in the array. Note: It is possible to swap the pairs of array elements. Examples: Input: arr[] = {2, 1, 2, 1, 4, 3}Output: 2Explanation: Sorting the array modifies the array to arr[] = {1, 1, 2, 2, 3, 4}. Two possible increasi
6 min read
Make an array strictly increasing by repeatedly subtracting and adding arr[i - 1] - (i - 1) to adjacent indices
Given an array arr[] consisting of N positive integers, the task is to check whether the given array arr[] can be made strictly increasing such that for any index i from the range [1, N - 1], if (arr[i - 1] - (i - 1)) is at least 0, then it is added to arr[i] and subtracted from arr[i - 1]. If it is possible to make the array strictly increasing, t
6 min read
Check if it’s possible to split the Array into strictly increasing subsets of size at least K
Given an array arr[] of size N and an integer K, the task is to check whether it's possible to split the array into strictly increasing subsets of size at least K. If it is possible then print "Yes". Otherwise, print "No". Examples: Input: arr[] = {5, 6, 4, 9, 12}, K = 2Output: YesExplanation: One possible way to split the array into subsets of at
6 min read