📌  相关文章
📜  使所有数组元素相等所需的最少操作

📅  最后修改于: 2021-04-21 23:40:36             🧑  作者: Mango

给定数组arr [] ,其中包含n个整数和一个整数k 。任务是计算使所有数组元素相等所需的给定操作的最小次数。在单个操作中,将数组的k元素附加在数组的末尾,并删除数组的第一个元素(数组的大小保持不变)。如果无法使数组元素与此操作相等,则打印-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