Minimize steps to make Array elements equal by using giving operations
Last Updated :
20 Feb, 2023
Given an arr[] of length N. Then the task is to find minimum steps to make all the elements of the given array equal. Two types of operations can be performed as given below:
- Choose a prefix of size K, where arr[K] = max element in that chosen sub-array and convert all elements equal to max.
- Choose any sub-array and left-rotate it once.
Examples:
Input: N = 2, arr[] = {34, 34}
Output: 0
Explanation: All the elements are equal already. Therefore, No need to apply operations on it, Hence cost is zero.
Input: N = 3, arr[] = {1, 2, 3}
Output: 1
Explanation:
First operation: Choose prefix sub-array {A1, A2, A3} in which arr[3] = max element in sub-array and equal all the elements with max = {3, 3, 3}. Therefore, minimum cost is 1.
Approach: The problem can be solved based on the following observation:
We can solve any case at most 2 operations. For more clarification see the concept of approach and its illustration below.
If the max element is present at last index of the arr[], Then all the elements can be make equal by using the first operation only. From this we can conclude that the optimal choice is to place the maximum element of the array at the last index. We can shift max element to the last index using only one instance of second operation.
So there are three cases:
- If max = min in arr[], Then arr[] elements are equal already. Therefore, minimum cost is 0.
- If max element is present at last index of arr[], Then elements can be make equal by applying First operation. Therefore, minimum cost is 1.
- Rest of the cases except above two will take two operations. Therefore, minimum cost is 2.
Follow the below steps to implement the idea:
- Create variables max, min, and max_index for storing the maximum, minimum element, and index of the maximum element respectively.
- Then check for the below conditions:
- If max = min, Then the minimum cost is 0.
- If max_index = N, Then the minimum cost is 1.
- The rest of the cases will have a minimum cost equal to 2.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minimum_cost( int N, int ayami[])
{
int max = INT_MIN;
int min = INT_MAX;
int max_index = 0;
for ( int i = 0; i < N; i++) {
max = ayami[i] > max ? ayami[i] : max;
min = ayami[i] < min ? ayami[i] : min;
if (max == ayami[i]) {
max_index = i;
}
}
if (max == min) {
return 0;
}
else if (max == ayami[N - 1]) {
return 1;
}
else {
return 2;
}
}
int main()
{
int N = 5;
int ayami[] = { 1, 8, 5, 4, 3 };
cout << minimum_cost(N, ayami);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
throws java.lang.Exception
{
int N = 5 ;
int [] ayami = { 1 , 8 , 5 , 4 , 3 };
System.out.println(minimum_cost(N, ayami));
}
static int minimum_cost( int N, int [] ayami)
{
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int max_index = 0 ;
for ( int i = 0 ; i < N; i++) {
max = ayami[i] > max ? ayami[i] : max;
min = ayami[i] < min ? ayami[i] : min;
if (max == ayami[i]) {
max_index = i;
}
}
if (max == min) {
return 0 ;
}
else if (max == ayami[N - 1 ]) {
return 1 ;
}
else {
return 2 ;
}
}
}
|
Python3
def minimum_cost(N, ayami):
Max = float ( "-inf" )
Min = float ( "inf" )
max_index = 0
for i in range (N):
Max = ayami[i] if ayami[i] > Max else Max
Min = ayami[i] if ayami[i] < Min else Min
if ( Max = = ayami[i]):
max_index = i
if ( Max = = Min ):
return 0
elif ( Max = = ayami[N - 1 ]):
return 1
else :
return 2
N = 5
ayami = [ 1 , 8 , 5 , 4 , 3 ]
print (minimum_cost(N, ayami))
|
C#
using System;
public class GFG{
static int minimum_cost( int N, int [] ayami)
{
int max = Int32.MinValue;
int min = Int32.MaxValue;
int max_index = 0;
for ( int i = 0; i < N; i++) {
max = ayami[i] > max ? ayami[i] : max;
min = ayami[i] < min ? ayami[i] : min;
if (max == ayami[i]) {
max_index = i;
}
}
if (max == min) {
return 0;
}
else if (max == ayami[N - 1]) {
return 1;
}
else {
return 2;
}
}
static public void Main (){
int N = 5;
int [] ayami = { 1, 8, 5, 4, 3 };
Console.WriteLine(minimum_cost(N, ayami));
}
}
|
Javascript
function minimum_cost(N,ayami)
{
let max = Number.MIN_VALUE;
let min = Number.MAX_VALUE;
let max_index = 0;
for (let i = 0; i < N; i++) {
max = ayami[i] > max ? ayami[i] : max;
min = ayami[i] < min ? ayami[i] : min;
if (max == ayami[i]) {
max_index = i;
}
}
if (max == min) {
return 0;
}
else if (max == ayami[N - 1]) {
return 1;
}
else {
return 2;
}
}
let N = 5;
let ayami = [ 1, 8, 5, 4, 3 ];
console.log(minimum_cost(N, ayami));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Articles:
Share your thoughts in the comments
Please Login to comment...