给定一个由N 个正整数组成的数组arr[]和一个整数K ,任务是找到任何正整数的最小插入次数,使得每个相邻元素的总和最多为 K 。如果不可能,则打印“-1” 。
例子:
Input: arr[] = {1, 2, 3, 4, 5}, K = 6
Output: 2
Explanation:
Following insertions are required:
Operation 1: Insert 1 between indices 2 and 3. Therefore, the array modifies to {1, 2, 3, 1, 4, 5}.
Operation 2: Insert 1 between index 4 and 5. Therefore, the array modifies to {1, 2, 3, 1, 4, 1, 5}. Therefore, the minimum number of insertions required is 2.
Input: arr[] = {4, 5, 6, 7, 7, 8}, K = 8
Output: -1
方法:这个想法基于这样一个事实,如果元素本身不等于或大于K ,则在总和超过K的元素之间插入1会使连续元素的总和小于K 。请按照以下步骤解决给定的问题:
- 初始化三个变量,比如res = 0 、可能 = 1和last = 0来存储最小插入次数,检查是否有可能使所有连续对的总和最多为 K ,并存储前一个编号分别为当前元素。
- 遍历数组arr[]并执行以下步骤:
- 如果arr[i]的值至少为K ,则不可能使所有连续对的总和至多为 K 。
- 如果last和arr[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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live