查找从 1 开始的数字,总和最多为 K,不包括给定的数字
给定一个数组arr[]和一个整数K ,任务是找到从 1 开始且总和最多为 K的数字,不包括给定数组的元素
例子:
Input: arr[] = {4, 6, 8, 12}, K = 14
Output: {1, 2, 3, 5}
Explanation: Maximum possible sum is 11, with elements as 1, 2, 3 and 5
Input: arr[] = {1, 3, 4}, K = 7
Output: {2, 5}
Explanation: Maximum possible sum is 7, with elements as 2 and 5
方法:可以通过创建一个哈希图来存储给定数组的元素来解决该任务。从1开始迭代,并通过检查 hashmap 来跟踪当前总和和排除的元素。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the required elements
void solve(vector& arr, int K)
{
// Store the elements of arr[]
unordered_map occ;
for (int i = 0; i < (int)arr.size(); i++)
occ[arr[i]]++;
// Store the current sum
int curSum = 0;
// Start from 1
int cur = 1;
// Store the answer
vector ans;
while (curSum + cur <= K) {
// Exclude the current element
if (occ.find(cur) != occ.end()) {
cur++;
}
else {
curSum += cur;
// Valid element
ans.push_back(cur);
cur++;
}
}
for (int i = 0; i < (int)ans.size(); i++)
cout << ans[i] << " ";
}
// Driver Code
int main()
{
vector arr = { 4, 6, 8, 12 };
int K = 14;
solve(arr, K);
return 0;
}
Java
// Java program for the above approach
import java.util.ArrayList;
import java.util.HashMap;
class GFG {
// Function to find the required elements
public static void solve(ArrayList arr, int K) {
// Store the elements of arr[]
HashMap occ = new HashMap();
for (int i = 0; i < arr.size(); i++) {
if (occ.containsKey(arr.get(i))) {
occ.put(arr.get(i), occ.get(arr.get(i)) + 1);
} else {
occ.put(arr.get(i), 1);
}
}
// Store the current sum
int curSum = 0;
// Start from 1
int cur = 1;
// Store the answer
ArrayList ans = new ArrayList();
while (curSum + cur <= K) {
// Exclude the current element
if (occ.containsKey(cur)) {
cur++;
} else {
curSum += cur;
// Valid element
ans.add(cur);
cur++;
}
}
for (int i = 0; i < (int) ans.size(); i++)
System.out.print(ans.get(i) + " ");
}
// Driver Code
public static void main(String args[]) {
ArrayList arr = new ArrayList();
arr.add(4);
arr.add(6);
arr.add(8);
arr.add(12);
int K = 14;
solve(arr, K);
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# Python Program to implement
# the above approach
# Function to find the required elements
def solve(arr, K):
# Store the elements of arr[]
occ = {}
for i in range(len(arr)):
if (arr[i] in occ):
occ[arr[i]] += 1
else:
occ[arr[i]] = 1
# Store the current sum
curSum = 0
# Start from 1
cur = 1
# Store the answer
ans = []
while (curSum + cur <= K) :
# Exclude the current element
if (cur in occ):
cur += 1
else:
curSum += cur
# Valid element
ans.append(cur)
cur += 1
for i in range(len(ans)):
print(ans[i], end=" ")
# Driver Code
arr = [4, 6, 8, 12]
K = 14
solve(arr, K)
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG {
// Function to find the required elements
public static void solve(List arr, int K) {
// Store the elements of []arr
Dictionary occ = new Dictionary();
for (int i = 0; i < arr.Count; i++) {
if (occ.ContainsKey(arr[i])) {
occ.Add(arr[i], occ[arr[i]] + 1);
} else {
occ.Add(arr[i], 1);
}
}
// Store the current sum
int curSum = 0;
// Start from 1
int cur = 1;
// Store the answer
List ans = new List();
while (curSum + cur <= K) {
// Exclude the current element
if (occ.ContainsKey(cur)) {
cur++;
} else {
curSum += cur;
// Valid element
ans.Add(cur);
cur++;
}
}
for (int i = 0; i < (int) ans.Count; i++)
Console.Write(ans[i] + " ");
}
// Driver Code
public static void Main(String []args) {
List arr = new List();
arr.Add(4);
arr.Add(6);
arr.Add(8);
arr.Add(12);
int K = 14;
solve(arr, K);
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
1 2 3 5
时间复杂度:O(N)
辅助空间:O(N)