Minimum number of moves to make all elements equal
Given an array containing N elements and an integer K. It is allowed to perform the following operation any number of times on the given array:
- Insert the K-th element at the end of the array and delete the first element of the array.
The task is to find the minimum number of moves needed to make all elements of the array equal. Print -1 if it is not possible.
Examples:
Input : arr[] = {1, 2, 3, 4}, K = 4
Output : 3
Step 1: 2 3 4 4
Step 2: 3 4 4 4
Step 3: 4 4 4 4
Input : arr[] = {2, 1}, K = 1
Output : -1
The array will keep alternating between 1, 2 and
2, 1 regardless of how many moves you apply.
Let’s look at the operations with respect to the original array, first, we copy a[k] to the end, then a[k+1], and so on. To make sure that we only copy equal elements, all elements in the range K to N should be equal.
So, to find the minimum number of moves, we need to remove all elements in range 1 to K that are not equal to a[k]. Hence, we need to keep applying operations until we reach the rightmost term in range 1 to K that is not equal to a[k].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countMinimumMoves( int arr[], int n, int k)
{
int i;
for (i = k - 1; i < n; i++)
if (arr[i] != arr[k - 1])
return -1;
for (i = k - 1; i >= 0; i--)
if (arr[i] != arr[k - 1])
return i + 1;
return 0;
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int K = 4;
int n = sizeof (arr) / sizeof (arr[0]);
cout << countMinimumMoves(arr, n, K);
return 0;
}
|
C
#include <stdio.h>
int countMinimumMoves( int arr[], int n, int k)
{
int i;
for (i = k - 1; i < n; i++)
if (arr[i] != arr[k - 1])
return -1;
for (i = k - 1; i >= 0; i--)
if (arr[i] != arr[k - 1])
return i + 1;
return 0;
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int K = 4;
int n = sizeof (arr) / sizeof (arr[0]);
printf ( "%d" , countMinimumMoves(arr, n, K));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countMinimumMoves( int arr[], int n, int k)
{
int i;
for (i = k - 1 ; i < n; i++)
if (arr[i] != arr[k - 1 ])
return - 1 ;
for (i = k - 1 ; i >= 0 ; i--)
if (arr[i] != arr[k - 1 ])
return i + 1 ;
return 0 ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int K = 4 ;
int n = arr.length;
System.out.print(countMinimumMoves(arr, n, K));
}
}
|
Python3
def countMinimumMoves(arr, n, k) :
for i in range (k - 1 , n) :
if (arr[i] ! = arr[k - 1 ]) :
return - 1
for i in range (k - 1 , - 1 , - 1 ) :
if (arr[i] ! = arr[k - 1 ]) :
return i + 1
return 0
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 ]
K = 4
n = len (arr)
print (countMinimumMoves(arr, n, K))
|
C#
using System;
class GFG
{
static int countMinimumMoves( int []arr,
int n, int k)
{
int i;
for (i = k - 1; i < n; i++)
if (arr[i] != arr[k - 1])
return -1;
for (i = k - 1; i >= 0; i--)
if (arr[i] != arr[k - 1])
return i + 1;
return 0;
}
public static void Main ()
{
int []arr = { 1, 2, 3, 4 };
int K = 4;
int n = arr.Length;
Console.Write(countMinimumMoves(arr, n, K));
}
}
|
PHP
<?php
function countMinimumMoves( $arr , $n , $k )
{
for ( $i = $k - 1; $i < $n ; $i ++)
if ( $arr [ $i ] != $arr [ $k - 1])
return -1;
for ( $i = $k - 1; $i >= 0; $i --)
if ( $arr [ $i ] != $arr [ $k - 1])
return $i + 1;
return 0;
}
$arr = array (1, 2, 3, 4);
$K = 4;
$n = sizeof( $arr );
echo countMinimumMoves( $arr , $n , $K );
?>
|
Javascript
<script>
function countMinimumMoves(arr, n, k)
{
let i;
for (i = k - 1; i < n; i++)
if (arr[i] != arr[k - 1])
return -1;
for (i = k - 1; i >= 0; i--)
if (arr[i] != arr[k - 1])
return i + 1;
return 0;
}
let arr = [ 1, 2, 3, 4 ];
let K = 4;
let n = arr.length;
document.write(countMinimumMoves(arr, n, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
19 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...