📌  相关文章
📜  最小化插入,以使每对连续的数组元素的总和最多为K

📅  最后修改于: 2021-05-17 17:31:02             🧑  作者: Mango

给定N个正整数和整数K的数组arr [] ,任务是找到任何正整数的最小插入数,以使每个相邻元素的总和最大为K。如果不可能,则打印“ -1”

例子:

方法:该思想基于以下事实:如果元素本身不等于或大于K,则在总和超过K的元素之间插入1会使连续元素的总和小于K。请按照以下步骤解决给定的问题:

  • 初始化三个变量,例如res = 0可能= 1最后一个= 0,以存储最小插入次数,以检查是否有可能使所有连续对的总和最多为K ,并存储前一个分别编号为当前元素。
  • 遍历数组arr []并执行以下步骤:
    • 如果arr [i]的值至少为K ,则不可能使所有连续对的总和最多为K。
    • 如果lastarr [i]的总和大于K ,则将res增加1 ,然后 分配last = arr [i]
  • 完成上述步骤后,如果可能的值为1 ,则将res的值打印为所需的最小插入次数。否则,打印“ -1”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count minimum number of
// insertions required to make sum of
// every pair of adjacent elements at most K
void minimumInsertions(int arr[],
                       int N, int K)
{
    // Stores if it is possible to
    // make sum of each pair of
    // adjacent elements at most K
    bool possible = 1;
 
    // Stores the count of insertions
    int res = 0;
 
    // Stores the previous
    // value to index i
    int last = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // If arr[i] is greater
        // than or equal to K
        if (arr[i] >= K) {
 
            // Mark possible 0
            possible = 0;
            break;
        }
 
        // If last + arr[i]
        // is greater than K
        if (last + arr[i] > K)
 
            // Increment res by 1
            res++;
 
        // Assign arr[i] to last
        last = arr[i];
    }
 
    // If possible to make the sum of
    // pairs of adjacent elements at most K
    if (possible) {
        cout << res;
    }
 
    // Otherwise print "-1"
    else {
 
        cout << "-1";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int K = 6;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    minimumInsertions(arr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
  // Function to count minimum number of
  // insertions required to make sum of
  // every pair of adjacent elements at most K
  static void minimumInsertions(int arr[],
                                int N, int K)
  {
 
    // Stores if it is possible to
    // make sum of each pair of
    // adjacent elements at most K
    boolean possible = true;
 
    // Stores the count of insertions
    int res = 0;
 
    // Stores the previous
    // value to index i
    int last = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
      // If arr[i] is greater
      // than or equal to K
      if (arr[i] >= K) {
 
        // Mark possible 0
        possible = false;
        break;
      }
 
      // If last + arr[i]
      // is greater than K
      if (last + arr[i] > K)
 
        // Increment res by 1
        res++;
 
      // Assign arr[i] to last
      last = arr[i];
    }
 
    // If possible to make the sum of
    // pairs of adjacent elements at most K
    if (possible) {
      System.out.print(res);
    }
 
    // Otherwise print "-1"
    else {
 
      System.out.print("-1");
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int arr[] = { 1, 2, 3, 4, 5 };
    int K = 6;
    int N = arr.length;
 
    minimumInsertions(arr, N, K);
 
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# Python 3 program for the above approach
 
# Function to count minimum number of
# insertions required to make sum of
# every pair of adjacent elements at most K
def minimumInsertions(arr, N, K):
 
    # Stores if it is possible to
    # make sum of each pair of
    # adjacent elements at most K
    possible = 1
 
    # Stores the count of insertions
    res = 0
 
    # Stores the previous
    # value to index i
    last = 0
 
    # Traverse the array
    for i in range(N):
 
        # If arr[i] is greater
        # than or equal to K
        if (arr[i] >= K):
 
            # Mark possible 0
            possible = 0
            break
 
        # If last + arr[i]
        # is greater than K
        if (last + arr[i] > K):
 
            # Increment res by 1
            res += 1
 
        # Assign arr[i] to last
        last = arr[i]
 
    # If possible to make the sum of
    # pairs of adjacent elements at most K
    if (possible):
        print(res)
 
    # Otherwise print "-1"
    else:
        print("-1")
 
# Driver Code
if __name__ == "__main__":
 
    arr = [1, 2, 3, 4, 5]
    K = 6
    N = len(arr)
 
    minimumInsertions(arr, N, K)
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
 
class GFG{
 
  // Function to count minimum number of
  // insertions required to make sum of
  // every pair of adjacent elements at most K
  static void minimumInsertions(int[] arr,
                                int N, int K)
  {
 
    // Stores if it is possible to
    // make sum of each pair of
    // adjacent elements at most K
    bool possible = true;
 
    // Stores the count of insertions
    int res = 0;
 
    // Stores the previous
    // value to index i
    int last = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
      // If arr[i] is greater
      // than or equal to K
      if (arr[i] >= K) {
 
        // Mark possible 0
        possible = false;
        break;
      }
 
      // If last + arr[i]
      // is greater than K
      if (last + arr[i] > K)
 
        // Increment res by 1
        res++;
 
      // Assign arr[i] to last
      last = arr[i];
    }
 
    // If possible to make the sum of
    // pairs of adjacent elements at most K
    if (possible) {
      Console.Write(res);
    }
 
    // Otherwise print "-1"
    else {
 
      Console.Write("-1");
    }
  }
 
  // Driver code
  static void Main()
  {
 
    int[] arr = { 1, 2, 3, 4, 5 };
    int K = 6;
    int N = arr.Length;
 
    minimumInsertions(arr, N, K);
  }
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
2

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