给定两个阵列A []和B []分别选自N和M的整数,和整数K,该任务是通过选择从所述数组A恰好一个要素[],并从一个元件到查找最近K个可能的总和数组B[] ,最多两次。
例子:
Input: A[] = {1, 7}, B[] = {3, 4}, K = 10
Output: 10
Explanation:
Sum obtained by selecting A[0] and A[1] = 3 + 7 = 10, which is closest to the value K(= 10).
Input: A[] = {2, 3}, B[] = {4, 5, 30}, K = 18
Output: 17
方法:给定的问题可以通过使用递归来解决,通过为每个数组元素A[i] 找到总和最接近(K – A[i])的数组B[]的子集元素的总和。请按照以下步骤解决问题:
- 初始化两个变量,比如mini作为INT_MAX和ans作为INT_MAX来存储最小绝对差和最接近K的值。
- 定义一个递归函数,比如findClosest(arr, i, currSum)以找到最接近K的数组的子集总和,其中i是数组B[] 中的索引, currSum存储子集的总和。
- 如果i的值至少为 M ,则从函数返回。
- 如果(currSum – K)的绝对值小于迷你,然后更新为ABS(currSum – K)小的值,并更新ANS作为currSum的值。
- 如果(currSum – K)的绝对值等于mini ,则将ans的值更新为ans和currSum的最小值。
- 将不包括元素B[i]的递归函数调用为findClosest(i + 1, currSum) 。
- 将包含元素B[i]的递归函数调用一次为findClosest(i + 1, currSum + B[i]) 。
- 调用包含元素B[i]的递归函数两次findClosest(i + 1, currSum + 2*B[i]) 。
- 遍历给定的数组A[]并对每个元素调用函数findClosest(0, A[i]) 。
- 完成上述步骤后,打印ans的值作为结果和。
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
// Stores the sum closest to K
int ans = INT_MAX;
// Stores the minimum absolute difference
int mini = INT_MAX;
// Function to choose the elements
// from the array B[]
void findClosestTarget(int i, int curr,
int B[], int M,
int K)
{
// If absolute difference is less
// then minimum value
if (abs(curr - K) < mini) {
// Update the minimum value
mini = abs(curr - K);
// Update the value of ans
ans = curr;
}
// If absolute difference between
// curr and K is equal to minimum
if (abs(curr - K) == mini) {
// Update the value of ans
ans = min(ans, curr);
}
// If i is greater than M - 1
if (i >= M)
return;
// Includes the element B[i] once
findClosestTarget(i + 1, curr + B[i],
B, M, K);
// Includes the element B[i] twice
findClosestTarget(i + 1, curr + 2 * B[i],
B, M, K);
// Excludes the element B[i]
findClosestTarget(i + 1, curr, B, M, K);
}
// Function to find a subset sum
// whose sum is closest to K
int findClosest(int A[], int B[],
int N, int M, int K)
{
// Traverse the array A[]
for (int i = 0; i < N; i++) {
// Function Call
findClosestTarget(0, A[i], B,
M, K);
}
// Return the ans
return ans;
}
// Driver Code
int main()
{
// Input
int A[] = { 2, 3 };
int B[] = { 4, 5, 30 };
int N = sizeof(A) / sizeof(A[0]);
int M = sizeof(B) / sizeof(B[0]);
int K = 18;
// Function Call
cout << findClosest(A, B, N, M, K);
return 0;
}
Java
// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
// Stores the sum closest to K
static int ans = Integer.MAX_VALUE;
// Stores the minimum absolute difference
static int mini = Integer.MAX_VALUE;
// Function to choose the elements
// from the array B[]
static void findClosestTarget(int i, int curr, int B[],
int M, int K)
{
// If absolute difference is less
// then minimum value
if (Math.abs(curr - K) < mini) {
// Update the minimum value
mini = Math.abs(curr - K);
// Update the value of ans
ans = curr;
}
// If absolute difference between
// curr and K is equal to minimum
if (Math.abs(curr - K) == mini) {
// Update the value of ans
ans = Math.min(ans, curr);
}
// If i is greater than M - 1
if (i >= M)
return;
// Includes the element B[i] once
findClosestTarget(i + 1, curr + B[i], B, M, K);
// Includes the element B[i] twice
findClosestTarget(i + 1, curr + 2 * B[i], B, M, K);
// Excludes the element B[i]
findClosestTarget(i + 1, curr, B, M, K);
}
// Function to find a subset sum
// whose sum is closest to K
static int findClosest(int A[], int B[], int N, int M,
int K)
{
// Traverse the array A[]
for (int i = 0; i < N; i++) {
// Function Call
findClosestTarget(0, A[i], B, M, K);
}
// Return the ans
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Input
int A[] = { 2, 3 };
int B[] = { 4, 5, 30 };
int N = A.length;
int M = B.length;
int K = 18;
// Function Call
System.out.print(findClosest(A, B, N, M, K));
}
}
// This code is contributed by Kingash.
Python3
# Python3 program of the above approach
# Stores the sum closest to K
ans = 10**8
# Stores the minimum absolute difference
mini = 10**8
# Function to choose the elements
# from the array B[]
def findClosestTarget(i, curr, B, M, K):
global ans, mini
# If absolute difference is less
# then minimum value
if (abs(curr - K) < mini):
# Update the minimum value
mini = abs(curr - K)
# Update the value of ans
ans = curr
# If absolute difference between
# curr and K is equal to minimum
if (abs(curr - K) == mini):
# Update the value of ans
ans = min(ans, curr)
# If i is greater than M - 1
if (i >= M):
return
# Includes the element B[i] once
findClosestTarget(i + 1, curr + B[i], B, M, K)
# Includes the element B[i] twice
findClosestTarget(i + 1, curr + 2 * B[i], B, M, K)
# Excludes the element B[i]
findClosestTarget(i + 1, curr, B, M, K)
# Function to find a subset sum
# whose sum is closest to K
def findClosest(A, B, N, M, K):
# Traverse the array A[]
for i in range(N):
# Function Call
findClosestTarget(0, A[i], B, M, K)
# Return the ans
return ans
# Driver Code
if __name__ == '__main__':
# Input
A = [2, 3]
B = [4, 5, 30]
N = len(A)
M = len(B)
K = 18
# Function Call
print (findClosest(A, B, N, M, K))
# This code is contributed by mohit kumar 29.
C#
// C# program of the above approach
using System;
class GFG
{
// Stores the sum closest to K
static int ans = Int32.MaxValue;
// Stores the minimum absolute difference
static int mini = Int32.MaxValue;
// Function to choose the elements
// from the array B[]
static void findClosestTarget(int i, int curr, int[] B,
int M, int K)
{
// If absolute difference is less
// then minimum value
if (Math.Abs(curr - K) < mini) {
// Update the minimum value
mini = Math.Abs(curr - K);
// Update the value of ans
ans = curr;
}
// If absolute difference between
// curr and K is equal to minimum
if (Math.Abs(curr - K) == mini) {
// Update the value of ans
ans = Math.Min(ans, curr);
}
// If i is greater than M - 1
if (i >= M)
return;
// Includes the element B[i] once
findClosestTarget(i + 1, curr + B[i], B, M, K);
// Includes the element B[i] twice
findClosestTarget(i + 1, curr + 2 * B[i], B, M, K);
// Excludes the element B[i]
findClosestTarget(i + 1, curr, B, M, K);
}
// Function to find a subset sum
// whose sum is closest to K
static int findClosest(int[] A, int[] B, int N, int M,
int K)
{
// Traverse the array A[]
for (int i = 0; i < N; i++) {
// Function Call
findClosestTarget(0, A[i], B, M, K);
}
// Return the ans
return ans;
}
// Driver Code
public static void Main()
{
// Input
int[] A = { 2, 3 };
int[] B = { 4, 5, 30 };
int N = A.Length;
int M = B.Length;
int K = 18;
// Function Call
Console.WriteLine(findClosest(A, B, N, M, K));
}
}
// This code is contributed by ukasp.
Javascript
输出:
17
时间复杂度: O(N * 3 M )
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live