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++
#include <bits/stdc++.h>
using namespace std;
int min_ops( int arr[], int n)
{
int res = 0;
int i = n - 1;
while (i > 0) {
if (arr[i] == arr[n - 1]) {
i--;
}
else {
res++;
i -= (n - i - 1);
}
}
return res;
}
int main()
{
int arr[] = { 4, 2, 1, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << min_ops(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int min_ops( int arr[], int n)
{
int res = 0 ;
int i = n - 1 ;
while (i > 0 ) {
if (arr[i] == arr[n - 1 ]) {
i--;
}
else {
res++;
i -= (n - i - 1 );
}
}
return res;
}
public static void main (String[] args) {
int arr[] = { 4 , 2 , 1 , 3 };
int N = arr.length;
System.out.print(min_ops(arr, N));
}
}
|
Python3
def min_ops(arr, n):
res = 0
i = n - 1
while (i > 0 ):
if (arr[i] = = arr[n - 1 ]):
i = i - 1
else :
res = res + 1
i = i - (n - i - 1 )
return res
arr = [ 4 , 2 , 1 , 3 ]
N = len (arr)
print (min_ops(arr, N))
|
C#
using System;
class GFG {
static int min_ops( int [] arr, int n)
{
int res = 0;
int i = n - 1;
while (i > 0) {
if (arr[i] == arr[n - 1]) {
i--;
}
else {
res++;
i -= (n - i - 1);
}
}
return res;
}
public static void Main()
{
int [] arr = { 4, 2, 1, 3 };
int N = arr.Length;
Console.Write(min_ops(arr, N));
}
}
|
Javascript
<script>
const min_ops = (arr, n) => {
let res = 0;
let i = n - 1;
while (i > 0) {
if (arr[i] == arr[n - 1]) {
i--;
}
else {
res++;
i -= (n - i - 1);
}
}
return res;
}
let arr = [4, 2, 1, 3];
let N = arr.length;
document.write(min_ops(arr, N));
</script>
|
Time Complexity: O(N)
Space Complexity: O(1)
Last Updated :
11 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...