Minimum operations required to make all the array elements equal
Last Updated :
15 Sep, 2022
Given an array arr[] of n integer and an integer k. The task is to count the minimum number of times the given operation is required to make all the array elements equal. In a single operation, the kth element of the array is appended at the end of the array and the first element of the array gets deleted (the size of the array remains same). If the array elements cannot be made equal with this operation then print -1 else print the count of minimum operations required.
Examples:
Input: arr[] = {2, 1, 1, 1, 1}, k = 3
Output: 1
Applying the operation 1st time
3rd element in the array is 1 we append it to the end of the array and get arr[] = {2, 1, 1, 1, 1, 1}
then we delete the 1st element and get arr[] = {1, 1, 1, 1, 1}
Input: arr[] = {1, 2, 3, 4}, k = 3
Output: -1
Approach: At each operation at first the kth element is copied to the end then the (k + 1)th element from the initial sequence is copied, then (k + 2)th and so on. So all the elements will become equal if and only if all the elements in the array starting from the kth element are equal. It's now also obvious that the number of operations needed for it is equal to the index of the last number that is not equal to the nth element of the initial sequence
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach
#include<bits/stdc++.h>
using namespace std;
// Function to return the minimum number of
// given operation required to make all the
// array elements equal
void minOperation(int n, int k, int a[])
{
// Check if all the elements
// from kth index to last are equal
for (int i = k; i < n; i++)
{
if(a[i] != a[k - 1])
cout << (-1)<<endl;
}
// Finding the 1st element which is
// not equal to the kth element
for (int i = k - 2; i > -1; i--)
{
if(a[i] != a[k - 1])
cout << (i + 1) << endl;
}
}
// Driver code
int main ()
{
int n = 5;
int k = 3;
int a[] = {2, 1, 1, 1, 1};
minOperation(n, k, a);
}
// This code is contributed by
// Surendra_Gangwar
Java
// Java implementation of the above approach
import java.io.*;
class GFG
{
// Function to return the minimum number of
// given operation required to make all the
// array elements equal
static void minOperation(int n, int k, int a[])
{
// Check if all the elements
// from kth index to last are equal
for (int i = k; i < n; i++)
{
if(a[i] != a[k - 1])
System.out.println(-1);
}
// Finding the 1st element which is
// not equal to the kth element
for (int i = k - 2; i > -1; i--)
{
if(a[i] != a[k - 1])
System.out.println(i + 1);
}
}
// Driver code
public static void main (String[] args)
{
int n = 5;
int k = 3;
int a[] = {2, 1, 1, 1, 1};
minOperation(n, k, a);
}
}
// This code is contributed by ajit.
Python
# Python3 implementation of the approach
# Function to return the minimum number of given operation
# required to make all the array elements equal
def minOperation(n, k, a):
# Check if all the elements
# from kth index to last are equal
for i in range(k, n):
if(a[i] != a[k - 1]):
return -1
# Finding the 1st element
# which is not equal to the kth element
for i in range(k-2, -1, -1):
if(a[i] != a[k-1]):
return i + 1
# Driver code
n = 5
k = 3
a = [2, 1, 1, 1, 1]
print(minOperation(n, k, a))
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to return the minimum number of
// given operation required to make all the
// array elements equal
static void minOperation(int n, int k, int []a)
{
// Check if all the elements
// from kth index to last are equal
for (int i = k; i < n; i++)
{
if(a[i] != a[k - 1])
Console.WriteLine(-1);
}
// Finding the 1st element which is
// not equal to the kth element
for (int i = k - 2; i > -1; i--)
{
if(a[i] != a[k - 1])
Console.WriteLine(i + 1);
}
}
// Driver code
static public void Main ()
{
int n = 5;
int k = 3;
int []a = {2, 1, 1, 1, 1};
minOperation(n, k, a);
}
}
// This code is contributed by Ryuga
PHP
<?php
// Php implementation of the approach
// Function to return the minimum number of
// given operation required to make all the
// array elements equal
function minOperation($n, $k, &$a)
{
// Check if all the elements
// from kth index to last are equal
for ($i = $k; $i < $n; $i++)
{
if($a[$i] != $a[$k - 1])
return -1;
}
// Finding the 1st element which is
// not equal to the kth element
for ($i = $k - 2; $i > -1; $i--)
{
if($a[$i] != $a[$k - 1])
return ($i + 1);
}
}
// Driver code
$n = 5;
$k = 3;
$a = array(2, 1, 1, 1, 1);
echo(minOperation($n, $k, $a));
// This code is contributed
// by Shivi_Aggarwal
?>
JavaScript
<script>
// javascript implementation of the above approach
// Function to return the minimum number of
// given operation required to make all the
// array elements equal
function minOperation(n, k, a)
{
// Check if all the elements
// from kth index to last are equal
for (i = k; i < n; i++)
{
if (a[i] != a[k - 1])
document.write(-1);
}
// Finding the 1st element which is
// not equal to the kth element
for (i = k - 2; i > -1; i--)
{
if (a[i] != a[k - 1])
document.write(i + 1);
}
}
// Driver code
var n = 5;
var k = 3;
var a = [ 2, 1, 1, 1, 1 ];
minOperation(n, k, a);
// This code is contributed by Rajput-Ji
</script>
Complexity Analysis:
- Time Complexity : O(n - k + k) => O(n)
- Auxiliary Space : O(1), since no extra space has been taken.
Similar Reads
Minimum operations required to make two elements equal in Array Given array A[] of size N and integer X, the task is to find the minimum number of operations to make any two elements equal in the array. In one operation choose any element A[i] and replace it with A[i] & X. where & is bitwise AND. If such operations do not exist print -1. Examples: Input:
9 min read
Find the minimum number of operations required to make all array elements equal Given an array arr[] of size N. The task is to make all the array elements equal by applying the below operations minimum number of times: Choose a pair of indices (i, j) such that |i - j| = 1 (indices i and j are adjacent) and set arr[i] = arr[i] + |arr[i] - arr[j]|Choose a pair of indices (i, j) s
6 min read
Find the number of operations required to make all array elements Equal Given an array of N integers, the task is to find the number of operations required to make all elements in the array equal. In one operation we can distribute equal weights from the maximum element to the rest of the array elements. If it is not possible to make the array elements equal after perfo
6 min read
Minimum operations to make all elements equal using the second array Given two arrays A[] and B[] both having N elements. Find the minimum number of operations to make all elements of A equal to the second array B. An operation comprises of: A[i] = A[i] - B[i], 0 <= i <= n-1 Note: If it's not possible to make array elements equal print -1.Examples: Input: A[] =
7 min read
Minimum operation to make all elements equal in array Given an array consisting of n positive integers, the task is to find the minimum number of operations to make all elements equal. In each operation, we can perform addition, multiplication, subtraction, or division with any number and an array element. Examples: Input : arr[] = [1, 2, 3, 4]Output :
11 min read