Minimize operations to make all elements equal by replacing left half of Subarray with right half
Given an array arr[] of length N, the task is to find the minimum operations to make all the array elements equal where in each operation:
- Choose any value K any subarray of even length 2*K.
- Replace the left half of the subarray by the right half of the subarray.
Examples:
Input: arr[] = {4, 4, 4, 2, 4}
Output: 1
Explanation: In 1st operation choose index i = 3 and K = 1.
So subarray of 2K length of indices [3, 4] is chosen.
Replace first half by the second half. So it becomes {4, 4, 4, 4, 4}Input: arr[] = {4, 2, 1, 3}
Output: 2
Explanation: In 1st operation choose index i = 2, K = 1.
The array converts to {4, 2, 3, 3}.
In the second operation choose index 0 and K = 2.
So the subarray of length 4 and first two gets replaced by the last two.
So the array becomes {3, 3, 3, 3}. So operations are 2.
Approach: This problem can be solved based on the following observation:
Observations:
The last element can never be changed with some other element since there are no elements to the right so all the previous elements must be equal to the last element.
To minimize the operations, keep as many same elements in the right half of the subarray as possible
Follow the steps mentioned below to solve the above problem:
- Initialize a variable res = 0 to store the operations required
- Traverse from the end of the array i = N – 2 to 0:
- Check if arr[i] is equal to last element
- If it is equal no operation is required
- Else there is a need to do an operation by choosing some K and an array of length 2*K and replace the first half with the second half.
- Consider arr[i] to be last element of the left half and (i+1 to N-1) to be the right half and replace all left half elements with the right half elements.
- So increment the res and move to index i = (i – (N – 1 – i)) to have minimum operations by replacing the whole half by the second half.
- Return the res as the required answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to count minimum operations int min_ops( int arr[], int n) { int res = 0; int i = n - 1; while (i > 0) { // Check if arr[i]== arr[n-1] // If they are equal do i-- if (arr[i] == arr[n - 1]) { i--; } else { // Try to do an operation // It is always optimal to // to move to the index of // length n-1-i before i // since it is easy to replace // first half with second half res++; // Move i to half of the // elements after that i -= (n - i - 1); } } return res; } // Driver code int main() { int arr[] = { 4, 2, 1, 3 }; int N = sizeof (arr) / sizeof (arr[0]); // Function call cout << min_ops(arr, N); return 0; } |
Java
// Java program for the above approach import java.io.*; class GFG { // Function to count minimum operations static int min_ops( int arr[], int n) { int res = 0 ; int i = n - 1 ; while (i > 0 ) { // Check if arr[i]== arr[n-1] // If they are equal do i-- if (arr[i] == arr[n - 1 ]) { i--; } else { // Try to do an operation // It is always optimal to // to move to the index of // length n-1-i before i // since it is easy to replace // first half with second half res++; // Move i to half of the // elements after that i -= (n - i - 1 ); } } return res; } // Driver code public static void main (String[] args) { int arr[] = { 4 , 2 , 1 , 3 }; int N = arr.length; // Function call System.out.print(min_ops(arr, N)); } } // This code is contributed by hrithikgarg03188. |
Python3
# Python program for the above approach # Function to count minimum operations def min_ops(arr, n): res = 0 i = n - 1 while (i > 0 ): # Check if arr[i]== arr[n-1] # If they are equal do i-- if (arr[i] = = arr[n - 1 ]): i = i - 1 else : # Try to do an operation # It is always optimal to # to move to the index of # length n-1-i before i # since it is easy to replace # first half with second half res = res + 1 # Move i to half of the # elements after that i = i - (n - i - 1 ) return res # Driver code arr = [ 4 , 2 , 1 , 3 ] N = len (arr) # Function call print (min_ops(arr, N)) # This code is contributed by Taranpreet |
C#
// C# program for the above approach using System; class GFG { // Function to count minimum operations static int min_ops( int [] arr, int n) { int res = 0; int i = n - 1; while (i > 0) { // Check if arr[i]== arr[n-1] // If they are equal do i-- if (arr[i] == arr[n - 1]) { i--; } else { // Try to do an operation // It is always optimal to // to move to the index of // length n-1-i before i // since it is easy to replace // first half with second half res++; // Move i to half of the // elements after that i -= (n - i - 1); } } return res; } // Driver code public static void Main() { int [] arr = { 4, 2, 1, 3 }; int N = arr.Length; // Function call Console.Write(min_ops(arr, N)); } } // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // JavaScript program for the above approach // Function to count minimum operations const min_ops = (arr, n) => { let res = 0; let i = n - 1; while (i > 0) { // Check if arr[i]== arr[n-1] // If they are equal do i-- if (arr[i] == arr[n - 1]) { i--; } else { // Try to do an operation // It is always optimal to // to move to the index of // length n-1-i before i // since it is easy to replace // first half with second half res++; // Move i to half of the // elements after that i -= (n - i - 1); } } return res; } // Driver code let arr = [4, 2, 1, 3]; let N = arr.length; // Function call document.write(min_ops(arr, N)); // This code is contributed by rakeshsahni </script> |
2
Time Complexity: O(N)
Space Complexity: O(1)
Please Login to comment...