📌  相关文章
📜  使所有元素均等的最小移动次数

📅  最后修改于: 2021-04-26 07:16:19             🧑  作者: Mango

给定一个包含N个元素和整数K的数组。允许在给定数组上执行任意次以下操作:

  • 在数组末尾插入第K个元素,然后删除数组的第一个元素。

任务是找到使数组的所有元素相等所需的最小移动次数。如果不可能,则打印-1。

例子:

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.

让我们看一下有关原始数组的操作,首先将a [k]复制到末尾,然后复制a [k + 1],依此类推。为了确保只复制相等的元素,K到N范围内的所有元素都应该相等。

因此,要找到最小移动数,我们需要删除范围1到K中不等于a [k]的所有元素。因此,我们需要继续应用运算,直到达到范围1到K中最右边的项(不等于a [k])为止。

下面是上述方法的实现:

C++
// C++ Program to find minimum number of
// operations to make all array Elements
// equal
  
#include 
using namespace std;
  
// Function to find minimum number of operations
// to make all array Elements equal
int countMinimumMoves(int arr[], int n, int k)
{
    int i;
  
    // Check if it is possible or not
    // That is if all the elements from
    // index K to N are not equal
    for (i = k - 1; i < n; i++)
        if (arr[i] != arr[k - 1])
            return -1;
  
    // Find minimum number of moves
    for (i = k - 1; i >= 0; i--)
        if (arr[i] != arr[k - 1])
            return i + 1;
  
    // Elements are already equal
    return 0;
}
  
// Driver Code
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;
}


Java
// Java Program to find minimum number of
// operations to make all array Elements
// equal
  
  
import java.io.*;
  
class GFG {
    
  
  
// Function to find minimum number of operations
// to make all array Elements equal
static int countMinimumMoves(int arr[], int n, int k)
{
    int i;
  
    // Check if it is possible or not
    // That is if all the elements from
    // index K to N are not equal
    for (i = k - 1; i < n; i++)
        if (arr[i] != arr[k - 1])
            return -1;
  
    // Find minimum number of moves
    for (i = k - 1; i >= 0; i--)
        if (arr[i] != arr[k - 1])
            return i + 1;
  
    // Elements are already equal
    return 0;
}
  
// Driver Code
  
    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));
    }
}
// This code is contributed by shs


Python3
# Python3 Program to find minimum 
# number of operations to make all 
# array Elements equal
  
# Function to find minimum number 
# of operations to make all array
# Elements equal
def countMinimumMoves(arr, n, k) :
  
    # Check if it is possible or not
    # That is if all the elements from
    # index K to N are not equal
    for i in range(k - 1, n) :
        if (arr[i] != arr[k - 1]) :
            return -1
  
    # Find minimum number of moves
    for i in range(k - 1, -1, -1) :
        if (arr[i] != arr[k - 1]) :
            return i + 1
  
    # Elements are already equal
    return 0
  
# Driver Code
if __name__ == "__main__" :
  
    arr = [ 1, 2, 3, 4 ]
    K = 4
  
    n = len(arr)
  
    print(countMinimumMoves(arr, n, K))
  
# This code is contributed by Ryuga


C#
// C# Program to find minimum number of
// operations to make all array Elements
// equal
using System;
  
class GFG 
{
      
// Function to find minimum number 
// of operations to make all array
// Elements equal
static int countMinimumMoves(int []arr, 
                             int n, int k)
{
    int i;
  
    // Check if it is possible or not
    // That is if all the elements from
    // index K to N are not equal
    for (i = k - 1; i < n; i++)
        if (arr[i] != arr[k - 1])
            return -1;
  
    // Find minimum number of moves
    for (i = k - 1; i >= 0; i--)
        if (arr[i] != arr[k - 1])
            return i + 1;
  
    // Elements are already equal
    return 0;
}
  
// Driver Code
public static void Main ()
{
    int []arr = { 1, 2, 3, 4 };
    int K = 4;
      
    int n = arr.Length;
      
    Console.Write(countMinimumMoves(arr, n, K));
}
}
  
// This code is contributed 
// by 29AjayKumar


PHP
= 0; $i--)
        if ($arr[$i] != $arr[$k - 1])
            return $i + 1;
  
    // Elements are already equal
    return 0;
}
  
// Driver Code
$arr = array(1, 2, 3, 4);
$K = 4;
  
$n = sizeof($arr);
  
echo countMinimumMoves($arr, $n, $K);
  
// This code is contributed 
// by Akanksha Rai
?>


输出:
3

时间复杂度: O(N)