Open In App

Minimize steps to make all Array elements equal by cyclically shifting a segment or replacing prefix by maximum

Given an array arr[] consisting of N positive integers, the task is to print the minimum number of steps required to make the array such that all elements are equal by performing the following operations on the array any number of times (possibly 0).

Examples:



Input: arr[] = {1, 2, 12, 20, 18, 19}
Output: 2
Explanation: It can be solved in two steps:
In the first step, apply operation 2 by choosing L = 4 and R = 6 yields the sequence {1, 2, 12, 18, 19, 20}. 
In the second step, apply operation 1 on the prefix arr{1, 2, 12, 18, 19, 20} which converts arr into {20, 20, 20, 20, 20} where all elements are equal.

Input: arr[] = {2, 2, 2, 2}
Output: 0
Explanation: the elements in the array are all same.



 

Approach: The given problem can be solved by using the following observation:

So based on the above observation, the answer can be 0, 1, or 2. Follow the steps mentioned below.

Below is the implementation of the above approach:




// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the minimum number
// of steps required to make all
// the elements are equal
void solve(int arr[], int N)
{
    // Initially assume all elements
    // are equal
    bool isEqual = true;
 
    // For finding the largest element
    int maxi = arr[0];
 
    // Loop to iterate through the array
    for (int i = 1; i < N; i++) {
 
        // If any element is not equal
        // than previous element, mark
        // it as false
        if (arr[i] != arr[i - 1]) {
            isEqual = false;
        }
 
        // Storing greater element
        maxi = max(maxi, arr[i]);
    }
 
    // If all the elements are equal
    if (isEqual) {
        cout << "0";
    }
 
    // If max element is found
    // at the last index
    else if (maxi == arr[N - 1]) {
        cout << "1";
    }
 
    // If max element is found
    // other than the last index
    else {
        cout << "2";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 12, 20, 18, 19 };
    int N = sizeof(arr) / sizeof(arr[0]);
    solve(arr, N);
    return 0;
}




/*package whatever //do not write package name here */
import java.io.*;
 
class GFG {
 
  // Function to print the minimum number
  // of steps required to make all
  // the elements are equal
  static void solve(int arr[], int N)
  {
 
    // Initially assume all elements
    // are equal
    boolean isEqual = true;
 
    // For finding the largest element
    int maxi = arr[0];
 
    // Loop to iterate through the array
    for (int i = 1; i < N; i++) {
 
      // If any element is not equal
      // than previous element, mark
      // it as false
      if (arr[i] != arr[i - 1]) {
        isEqual = false;
      }
 
      // Storing greater element
      maxi = Math.max(maxi, arr[i]);
    }
 
    // If all the elements are equal
    if (isEqual) {
      System.out.println("0");
    }
 
    // If max element is found
    // at the last index
    else if (maxi == arr[N - 1]) {
      System.out.println("1");
    }
 
    // If max element is found
    // other than the last index
    else {
      System.out.println("2");
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int arr[] = { 1, 2, 12, 20, 18, 19 };
    int N = arr.length;
    solve(arr, N);
  }
}
 
// This code is contributed by Potta Lokesh




# Python3 code to implement the above approach
   
# Function to print the minimum number
# of steps required to make all
# the elements are equal
def solve(arr, N):
     
    # Initially assume all elements
    # are equal
    isEqual = True
   
    # For finding the largest element
    maxi = arr[0]
   
    # Loop to iterate through the array
    for i in range(1, N):
   
        # If any element is not equal
        # than previous element, mark
        # it as false
        if (arr[i] != arr[i - 1]):
            isEqual = False
             
        # Storing greater element
        maxi = max(maxi, arr[i])
   
    # If all the elements are equal
    if (isEqual):
        print("0")
     
    # If max element is found
    # at the last index
    elif (maxi == arr[N - 1]):
        print("1")
     
    # If max element is found
    # other than the last index
    else:
        print("2")
     
# Driver Code
if __name__=="__main__":
   
    arr = [ 1, 2, 12, 20, 18, 19 ]
    N = len(arr)
     
    solve(arr, N)
 
# This code is contributed by Akash Jha




// C# code to implement the above approach
 
using System;
 
class GFG {
 
  // Function to print the minimum number
  // of steps required to make all
  // the elements are equal
  static void solve(int[] arr, int N)
  {
 
    // Initially assume all elements
    // are equal
    bool isEqual = true;
 
    // For finding the largest element
    int maxi = arr[0];
 
    // Loop to iterate through the array
    for (int i = 1; i < N; i++) {
 
      // If any element is not equal
      // than previous element, mark
      // it as false
      if (arr[i] != arr[i - 1]) {
        isEqual = false;
      }
 
      // Storing greater element
      maxi = Math.Max(maxi, arr[i]);
    }
 
    // If all the elements are equal
    if (isEqual) {
      Console.WriteLine("0");
    }
 
    // If max element is found
    // at the last index
    else if (maxi == arr[N - 1]) {
      Console.WriteLine("1");
    }
 
    // If max element is found
    // other than the last index
    else {
      Console.WriteLine("2");
    }
  }
 
  // Driver Code
  public static void Main()
  {
    int[] arr = { 1, 2, 12, 20, 18, 19 };
    int N = arr.Length;
    solve(arr, N);
  }
}
 
// This code is contributed by Saurabh Jaiswal




<script>
// Javascript code to implement the above approach
 
// Function to print the minimum number
// of steps required to make all
// the elements are equal
function solve(arr, N)
{
 
    // Initially assume all elements
    // are equal
    let isEqual = true;
 
    // For finding the largest element
    let maxi = arr[0];
 
    // Loop to iterate through the array
    for (let i = 1; i < N; i++) {
 
        // If any element is not equal
        // than previous element, mark
        // it as false
        if (arr[i] != arr[i - 1]) {
            isEqual = false;
        }
 
        // Storing greater element
        maxi = Math.max(maxi, arr[i]);
    }
 
    // If all the elements are equal
    if (isEqual) {
        document.write("0");
    }
 
    // If max element is found
    // at the last index
    else if (maxi == arr[N - 1]) {
        document.write("1");
    }
 
    // If max element is found
    // other than the last index
    else {
        document.write("2");
    }
}
 
// Driver Code
 
let arr = [1, 2, 12, 20, 18, 19];
let N = arr.length;
solve(arr, N);
 
// This code is contributed by saurabh_jaiswal.
</script>

Output
2

Time Complexity: O(N)
Auxiliary Space: O(1)

 


Article Tags :