给定数组arr [] ,其中包含n个整数和一个整数k 。任务是计算使所有数组元素相等所需的给定操作的最小次数。在单个操作中,将数组的第k个元素附加在数组的末尾,并删除数组的第一个元素(数组的大小保持不变)。如果无法使数组元素与此操作相等,则打印-1,否则打印所需的最小操作数。
例子:
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
方法:在每次操作时,首先将第k个元素复制到末尾,然后复制初始序列中的第(k +1)个元素,然后复制第(k + 2)个,依此类推。因此,当且仅当从第k个元素开始的数组中的所有元素相等时,所有元素才会变得相等。现在也很明显,它所需的操作数等于不等于初始序列的第n个元素的最后一个数字的索引
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
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)< -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
-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
?>
输出:
1