Convert to Strictly increasing integer array with minimum changes
Last Updated :
08 Feb, 2024
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
?>
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
Minimum increments to convert to an array of consecutive integers Given an array arr[] with N elements, the task is to find the minimum number of operations required so that an Arithmetic Progression with the array elements is achieved with common difference as 1. In a single operation, any element can be incremented by 1.Examples: Input: arr[] = {4, 4, 5, 5, 7} O
9 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] < A[i+1]) where in each operation we can choose any element and perform nums[i] = [ nums[i] / 2] (where [x] represents
6 min read
Minimum replacements with any positive integer to make the array K-increasing Given an array arr[] of N positive integers and an integer K, The task is to replace minimum number of elements with any positive integer to make the array K-increasing. An array is K-increasing if for every index i in range [K, N), arr[i] ⥠arr[i-K] Examples: Input: arr[] = {4, 1, 5, 2, 6, 2}, k =
11 min read
Minimum increment/decrement to make array non-Increasing Given an array a, your task is to convert it into a non-increasing form such that we can either increment or decrement the array value by 1 in the minimum changes possible. Examples : Input : a[] = {3, 1, 2, 1}Output : 1Explanation : We can convert the array into 3 1 1 1 by changing 3rd element of a
11 min read
Minimize swap between same indexed elements to make given Arrays strictly increasing Given two arrays arr1[] and arr2[] of size N each, the task is to find the minimum number of interchange of the same indexed elements required to make both arrays strictly increasing. Note: Return -1 if it is not possible to make them strictly increasing. Examples: Input: arr1 = {1, 3, 5, 4}, arr2 =
10 min read