Minimum increments by index value required to obtain at least two equal Array elements
Given a strictly decreasing array arr[] consisting of N integers, the task is to find the minimum number of operations required to make at least two array elements equal, where each operation involves increasing every array element by its index value.
Examples:
Input: arr[] = {6, 5, 1}
Output: 1
Explanation:
{6 + 1, 5 + 2, 1 + 3} = {7, 7, 4}
Input: arr[] = {12, 8, 4}
Output: 4
Explanation:
Step 1 : {12 + 1, 8 + 2, 4 + 3} = {13, 10, 7}
Step 2 : {13 + 1, 10 + 2, 7 + 3} = {14, 12, 10}
Step 3 : {15, 14, 13}
Step 4 : {16, 16, 16}
Naive approach: Follow the below steps to solve the problem:
- Check if the array already has at least two equal elements or not. If found to be true, print 0.
- Otherwise, keep updating the array by increasing each array element by its index value and increase count. Check if array has two equal elements or not.
- Print count once the array is found to be containing at least two equal elements.
Below is the implementation of the above approach:
C++
// C++ Program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to update every element// adding to it its index valuevoid update(int arr[], int N){ for (int i = 0; i < N; i++) { arr[i] += (i + 1); }}// Function to check if at least// two elements are equal or notbool check(int arr[], int N){ bool f = 0; for (int i = 0; i < N; i++) { // Count the frequency of arr[i] int count = 0; for (int j = 0; j < N; j++) { if (arr[i] == arr[j]) { count++; } } if (count >= 2) { f = 1; break; } } if (f == 1) return true; else return false;}// Function to calculate the number// of increment operations requiredvoid incrementCount(int arr[], int N){ // Stores the minimum number of steps int min = 0; while (check(arr, N) != true) { update(arr, N); min++; } cout << min;}// Driver Codeint main(){ int N = 3; int arr[N] = { 12, 8, 4 }; incrementCount(arr, N); return 0;} |
Java
// Java program to implement// the above approachimport java.util.*;class GFG{// Function to update every element// adding to it its index valuestatic void update(int arr[], int N){ for(int i = 0; i < N; i++) { arr[i] += (i + 1); }}// Function to check if at least// two elements are equal or notstatic boolean check(int arr[], int N){ int f = 0; for(int i = 0; i < N; i++) { // Count the frequency of arr[i] int count = 0; for(int j = 0; j < N; j++) { if (arr[i] == arr[j]) { count++; } } if (count >= 2) { f = 1; break; } } if (f == 1) return true; else return false;}// Function to calculate the number// of increment operations requiredstatic void incrementCount(int arr[], int N){ // Stores the minimum number of steps int min = 0; while (check(arr, N) != true) { update(arr, N); min++; } System.out.println(min);} // Driver codepublic static void main (String[] args){ int N = 3; int arr[] = { 12, 8, 4 }; incrementCount(arr, N);}}// This code is contributed by offbeat |
Python3
# Python3 program to implement# the above approach# Function to update every element# adding to it its index valuedef update(arr, N): for i in range(N): arr[i] += (i + 1);# Function to check if at least# two elements are equal or notdef check(arr, N): f = 0; for i in range(N): # Count the frequency of arr[i] count = 0; for j in range(N): if (arr[i] == arr[j]): count += 1; if (count >= 2): f = 1; break; if (f == 1): return True; else: return False;# Function to calculate the number# of increment operations requireddef incrementCount(arr, N): # Stores the minimum number of steps min = 0; while (check(arr, N) != True): update(arr, N); min += 1; print(min);# Driver codeif __name__ == '__main__': N = 3; arr = [ 12, 8, 4 ]; incrementCount(arr, N);# This code is contributed by 29AjayKumar |
C#
// C# program to implement// the above approachusing System;class GFG{// Function to update every element// adding to it its index valuestatic void update(int []arr, int N){ for(int i = 0; i < N; i++) { arr[i] += (i + 1); }}// Function to check if at least// two elements are equal or notstatic bool check(int []arr, int N){ int f = 0; for(int i = 0; i < N; i++) { // Count the frequency of arr[i] int count = 0; for(int j = 0; j < N; j++) { if (arr[i] == arr[j]) { count++; } } if (count >= 2) { f = 1; break; } } if (f == 1) return true; else return false;}// Function to calculate the number// of increment operations requiredstatic void incrementCount(int []arr, int N){ // Stores the minimum number of steps int min = 0; while (check(arr, N) != true) { update(arr, N); min++; } Console.WriteLine(min);} // Driver codepublic static void Main(String[] args){ int N = 3; int []arr = { 12, 8, 4 }; incrementCount(arr, N);}}// This code is contributed by Amit Katiyar |
Javascript
<script> // Java Script program to implement// the above approach// Function to update every element// adding to it its index valuefunction update(arr,N){ for(let i = 0; i < N; i++) { arr[i] += (i + 1); }}// Function to check if at least// two elements are equal or notfunction check(arr,N){ let f = 0; for(let i = 0; i < N; i++) { // Count the frequency of arr[i] let count = 0; for(let j = 0; j < N; j++) { if (arr[i] == arr[j]) { count++; } } if (count >= 2) { f = 1; break; } } if (f == 1) return true; else return false;}// Function to calculate the number// of increment operations requiredfunction incrementCount(arr,N){ // Stores the minimum number of steps let min = 0; while (check(arr, N) != true) { update(arr, N); min++; } document.write(min);} // Driver code let N = 3; let arr= [12, 8, 4 ]; incrementCount(arr, N);//contributed by 171fa07058</script> |
4
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by observing that by the given operation, the difference between any two adjacent elements reduces by 1 as the array is decreasing. Therefore, the minimum number of operations required is equal to the minimum difference between any two adjacent elements.
Below is the implementation of the above approach:
C++
// C++ Program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to calculate the minimum// number of steps requiredvoid incrementCount(int arr[], int N){ // Stores minimum difference int mini = arr[0] - arr[1]; for (int i = 2; i < N; i++) { mini = min(mini, arr[i - 1] - arr[i]); } cout << mini;}// Driver Codeint main(){ int N = 3; int arr[N] = { 12, 8, 4 }; incrementCount(arr, N); return 0;} |
Java
// Java program to implement// the above approachimport java.util.*;class GFG{ // Function to calculate the minimum// number of steps requiredstatic void incrementCount(int arr[], int N){ // Stores minimum difference int mini = arr[0] - arr[1]; for(int i = 2; i < N; i++) { mini = Math.min(mini, arr[i - 1] - arr[i]); } System.out.println(mini);} // Driver codepublic static void main (String[] args){ int N = 3; int arr[] = { 12, 8, 4 }; incrementCount(arr, N);}}// This code is contributed by offbeat |
Python3
# Python3 program to implement# the above approach# Function to calculate the minimum# number of steps requireddef incrementCount(arr, N): # Stores minimum difference mini = arr[0] - arr[1] for i in range(2, N): mini = min(mini, arr[i - 1] - arr[i]) print(mini)# Driver CodeN = 3arr = [ 12, 8, 4 ]# Function callincrementCount(arr, N)# This code is contributed by Shivam Singh |
C#
// C# program to implement// the above approachusing System;class GFG{ // Function to calculate the minimum// number of steps requiredstatic void incrementCount(int []arr, int N){ // Stores minimum difference int mini = arr[0] - arr[1]; for(int i = 2; i < N; i++) { mini = Math.Min(mini, arr[i - 1] - arr[i]); } Console.WriteLine(mini);} // Driver codepublic static void Main(String[] args){ int N = 3; int []arr = { 12, 8, 4 }; incrementCount(arr, N);}}// This code is contributed by 29AjayKumar |
Javascript
<script>// JavaScript program for the above approach// Function to calculate the minimum// number of steps requiredfunction incrementCount(arr, N){ // Stores minimum difference let mini = arr[0] - arr[1]; for(let i = 2; i < N; i++) { mini = Math.min(mini, arr[i - 1] - arr[i]); } document.write(mini);} // Driver Code let N = 3; let arr = [ 12, 8, 4 ]; incrementCount(arr, N); </script> |
4
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


