📌  相关文章
📜  通过重复递增子序列,使所有数组元素等于K

📅  最后修改于: 2021-05-13 22:48:04             🧑  作者: Mango

给定由N个整数和整数K组成的数组arr [] ,任务是通过将子序列的所有元素重复递增1来使所有数组元素等于K。
注意: K的值至少是array最大元素

例子:

方法:想法是使用散列来跟踪子序列中的元素。当子序列中的元素增加1时,其频率减少1 ,其修改值的频率增加1
请按照以下步骤解决问题:

  • 初始化一个变量,例如ans ,该变量存储所需的最少操作数。
  • 初始化一个Hashmap,例如mp ,并存储数组元素的频率。
  • K的频率小于N时,即mp [K] ,请执行以下操作:
    • 使用变量i遍历范围[1,K – 1]
      • 如果mp [i]大于0 ,则将当前值的频率降低,并将下一组(i + 1)元素的频率提高1
      • 如果(i + 1)不属于任何先前值,则跳过该值并继续遍历循环。
    • ans的值增加1。
  • 完成上述步骤后,输出ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum number
// of operations required to make all
// elements equal to k
void minOperations(int arr[], int n, int k)
{
    // Initialize a hashmap
    map mp;
 
    // Store frequency of array elements
    for (int i = 0; i < n; i++) {
        mp[arr[i]]++;
    }
 
    // Store the minimum number of
    // operations required
    int ans = 0;
 
    // Iterate until all array elements
    // becomes equal to K
    while (mp[k] < n) {
 
        // Iterate through range [1, k - 1]
        // since only one element can be
        // increased from each group
        for (int i = 1; i <= k - 1; i++) {
 
            // Check if the current number
            // has frequency > 0, i.e.,
            // it is a part of a group
            if (mp[i]) {
 
                // If true, decrease the
                // frequency of current
                // group of element by 1
                mp[i]--;
 
                // Increase the frequency
                // of the next group of
                // elements by 1
                mp[i + 1]++;
 
                // If the next element is
                // not the part of any
                // group, then skip it
                if (mp[i + 1] == 1) {
                    i++;
                }
            }
        }
 
        // Increment count of operations
        ans++;
    }
 
    // Print the count of operations
    cout << ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 3, 3, 4 };
    int K = 5;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    minOperations(arr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
 
  // Function to find the minimum number
  // of operations required to make all
  // elements equal to k
  static void minOperations(int arr[], int n, int k)
  {
 
    // Initialize a hashmap
    Map mp = new HashMap<>();
 
    // Store frequency of array elements
    for (int i = 0; i < n; i++)
    {
      if (mp.containsKey(arr[i]))
      {
        mp.put(arr[i], mp.get(arr[i]) + 1);
      }
      else
      {
        mp.put(arr[i], 1);
      }
    }
 
    // Store the minimum number of
    // operations required
    int ans = 0;
 
    // Iterate until all array elements
    // becomes equal to K
    while (mp.containsKey(k) == false
           || mp.get(k) < n) {
 
      // Iterate through range [1, k - 1]
      // since only one element can be
      // increased from each group
      for (int i = 1; i <= k - 1; i++) {
 
        // Check if the current number
        // has frequency > 0, i.e.,
        // it is a part of a group
        if (mp.containsKey(i) && mp.get(i) > 0) {
 
          // If true, decrease the
          // frequency of current
          // group of element by 1
          mp.put(i, mp.get(i) - 1);
 
          // Increase the frequency
          // of the next group of
          // elements by 1
          if (mp.containsKey(i + 1))
            mp.put(i + 1, mp.get(i + 1) + 1);
          else
            mp.put(i + 1, 1);
 
          // If the next element is
          // not the part of any
          // group, then skip it
          if (mp.containsKey(i + 1)
              && mp.get(i + 1) == 1) {
            i++;
          }
        }
      }
 
      // Increment count of operations
      ans++;
    }
 
    // Print the count of operations
    System.out.print(ans);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int arr[] = { 2, 3, 3, 4 };
    int K = 5;
    int N = arr.length;
 
    // Function Call
    minOperations(arr, N, K);
  }
}
 
// This code is contributed by Dharanendra L V.


C#
// C# program for the above approach
 
using System;
using System.Collections.Generic;
 
public class GFG
{
 
  // Function to find the minimum number
  // of operations required to make all
  // elements equal to k
  static void minOperations(int []arr, int n, int k)
  {
 
    // Initialize a hashmap
    Dictionary mp = new Dictionary();
 
    // Store frequency of array elements
    for (int i = 0; i < n; i++)
    {
      if (mp.ContainsKey(arr[i]))
      {
        mp[arr[i]] =  mp[arr[i]] + 1;
      }
      else
      {
        mp.Add(arr[i], 1);
      }
    }
 
    // Store the minimum number of
    // operations required
    int ans = 0;
 
    // Iterate until all array elements
    // becomes equal to K
    while (mp.ContainsKey(k) == false
           || mp[k] < n) {
 
      // Iterate through range [1, k - 1]
      // since only one element can be
      // increased from each group
      for (int i = 1; i <= k - 1; i++) {
 
        // Check if the current number
        // has frequency > 0, i.e.,
        // it is a part of a group
        if (mp.ContainsKey(i) && mp[i] > 0) {
 
          // If true, decrease the
          // frequency of current
          // group of element by 1
          mp[i] = mp[i] - 1;
 
          // Increase the frequency
          // of the next group of
          // elements by 1
          if (mp.ContainsKey(i + 1))
            mp[i + 1] = mp[i + 1] + 1;
          else
            mp.Add(i + 1, 1);
 
          // If the next element is
          // not the part of any
          // group, then skip it
          if (mp.ContainsKey(i + 1)
              && mp[i + 1] == 1) {
            i++;
          }
        }
      }
 
      // Increment count of operations
      ans++;
    }
 
    // Print the count of operations
    Console.Write(ans);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int []arr = { 2, 3, 3, 4 };
    int K = 5;
    int N = arr.Length;
 
    // Function Call
    minOperations(arr, N, K);
  }
}
 
 
// This code is contributed by 29AjayKumar


输出:
4

时间复杂度: O(N * K)
辅助空间: O(N)