最大化 K 的值,使得在 [1, K] 范围内的每个整数 i 都存在一个总和为 i 的子序列
给定一个由N个整数组成的数组arr[] ,任务是找到K的最大值,使得对于[1, K]范围内的每个整数i ,都存在一个总和为i的子序列。
例子:
Input: arr[] = {1, 2, 1, 3}
Output: 7
Explanation:
Below are the possible values of the sum for all the subsequences that can be formed:
- Subsequence {1}, the sum of elements is 1.
- Subsequence {2}, the sum of elements is 2.
- Subsequence {3}, the sum of elements is 3.
- Subsequence {1, 3}, the sum of elements is 4.
- Subsequence {1, 1, 3}, the sum of elements is 5.
- Subsequence {1, 2, 3}, the sum of elements is 6.
- Subsequence {1, 1, 2, 3}, the sum of elements is 7.
Hence, the maximum value of K is 7. Hence print 7.
Input: arr[] = {2, 5, 2, 3}
Output: 0
方法:按照以下步骤解决给定问题:
- 对给定的数组arr[]进行排序。
- 初始化一个变量,比如next为0以存储K的结果最大值。
- 在[0, N – 1]范围内遍历数组arr[ ] 并执行以下步骤:
- 如果arr[i]的值小于或等于(next + 1) ,则可以使用数组元素生成子序列{0, 1, 2, ..., next + arr[i]} 。因此,将值arr[i]添加到变量next 。
- 否则,跳出循环,因为总和(next + 1)不能由任何子序列生成 数组, arr[i]后面的所有值都大于该值。
- 完成上述步骤后,将next的值打印为K的结果最大值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum value
// of K such that for all value i over
// the range [1, K] there exists a
// subsequence whose value is i
void maximumK(vector& arr)
{
// Sort the given array
sort(arr.begin(), arr.end());
int N = arr.size();
// Stores the maximum value up to
// which subsequences can be
// generated by the array
int next = 0;
// Traverse the given array
for (int i = 0; i < N; i++) {
// Check if the current element
// is greater than next + 1
if (arr[i] > next + 1)
break;
// Add the contribution from
// the current element and
// update the next value
next += arr[i];
}
// Print the answer
cout << next << endl;
}
// Driver Code
int main()
{
vector arr = { 1, 2, 1, 3 };
maximumK(arr);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.Arrays;
class GFG
{
// Function to find the maximum value
// of K such that for all value i over
// the range [1, K] there exists a
// subsequence whose value is i
static void maximumK(int[] arr)
{
// Sort the given array
Arrays.sort(arr);
int N = arr.length;
// Stores the maximum value up to
// which subsequences can be
// generated by the array
int next = 0;
// Traverse the given array
for (int i = 0; i < N; i++) {
// Check if the current element
// is greater than next + 1
if (arr[i] > next + 1)
break;
// Add the contribution from
// the current element and
// update the next value
next += arr[i];
}
// Print the answer
System.out.println(next);
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 1, 2, 1, 3 };
maximumK(arr);
}
}
// This code is contributed by Potta Lokesh
Python3
# Python program for the above approach
# Function to find the maximum value
# of K such that for all value i over
# the range [1, K] there exists a
# subsequence whose value is i
def maximumK(arr):
# Sort the given array
arr.sort();
N = len(arr);
# Stores the maximum value up to
# which subsequences can be
# generated by the array
next = 0;
# Traverse the given array
for i in range(N):
# Check if the current element
# is greater than next + 1
if (arr[i] > next + 1):
break;
# Add the contribution from
# the current element and
# update the next value
next += arr[i];
# Print the answer
print(next);
# Driver Code
arr = [1, 2, 1, 3];
maximumK(arr);
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the maximum value
// of K such that for all value i over
// the range [1, K] there exists a
// subsequence whose value is i
static void maximumK(List arr)
{
// Sort the given array
arr.Sort();
int N = arr.Count;
// Stores the maximum value up to
// which subsequences can be
// generated by the array
int next = 0;
// Traverse the given array
for (int i = 0; i < N; i++) {
// Check if the current element
// is greater than next + 1
if (arr[i] > next + 1)
break;
// Add the contribution from
// the current element and
// update the next value
next += arr[i];
}
// Print the answer
Console.Write(next);
}
// Driver Code
public static void Main()
{
List arr = new List(){ 1, 2, 1, 3 };
maximumK(arr);
}
}
// This code is contributed by ipg2016107.
Javascript
输出:
7
时间复杂度: O(N* log N)
辅助空间: O(1)