📌  相关文章
📜  检查数组是否可以分成两个子数组,使得它们的绝对差为 K

📅  最后修改于: 2022-05-13 01:57:49.938000             🧑  作者: Mango

检查数组是否可以分成两个子数组,使得它们的绝对差为 K

给定一个数组arr[]和一个整数K ,任务是找出该数组是否可以分成两个子数组,使得两个子数组的元素之和的绝对差为K
例子:

方法:

  • 假设存在一个答案,设子数组(总和较小)的元素之和为S
  • 第二个数组的元素总和为S + K
  • 而且, S + S + K必须等于数组中所有元素的总和,比如totalSum = 2 *S + K
  • S = (totalSum – K) / 2
  • 现在,遍历数组,直到我们从第一个元素开始获得S的总和,如果不可能,则打印No
  • 否则打印Yes

下面是上述方法的实现:

C++
#include 
using namespace std;
 
// Function that return true if it is possible
// to divide the array into sub-arrays
// that satisfy the given condition
bool solve(int array[], int size, int k)
{
    // To store the sum of all the elements
    // of the array
    int totalSum = 0;
    for (int i = 0; i < size; i++)
        totalSum += array[i];
 
    // Sum of any sub-array cannot be
    // a floating point value
    if ((totalSum - k) % 2 == 1)
        return false;
 
    // Required sub-array sum
    int S = (totalSum - k) / 2;
 
    int sum = 0;
    for (int i = 0; i < size; i++) {
        sum += array[i];
        if (sum == S)
            return true;
    }
 
    return false;
}
 
// Driver Code
int main()
{
    int array[] = { 2, 4, 1, 5 };
    int k = 2;
    int size = sizeof(array) / sizeof(array[0]);
    if (solve(array, size, k))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}


Java
/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG
{
     
// Function that return true if it is possible
// to divide the array into sub-arrays
// that satisfy the given condition
static boolean solve(int array[], int size, int k)
{
    // To store the sum of all the elements
    // of the array
    int totalSum = 0;
    for (int i = 0; i < size; i++)
        totalSum += array[i];
 
    // Sum of any sub-array cannot be
    // a floating point value
    if ((totalSum - k) % 2 == 1)
        return false;
 
    // Required sub-array sum
    int S = (totalSum - k) / 2;
 
    int sum = 0;
    for (int i = 0; i < size; i++)
    {
        sum += array[i];
        if (sum == S)
            return true;
    }
    return false;
}
 
    // Driver Code
    public static void main (String[] args)
    {
        int array[] = { 2, 4, 1, 5 };
        int k = 2;
        int size = array.length;
         
        if (solve(array, size, k))
            System.out.println ("Yes");
        else
            System.out.println ("No" );
    }
}
 
// This Code is contributed by akt_mit


Python3
# Function that return true if it is possible
# to divide the array into sub-arrays
# that satisfy the given condition
def solve(array,size,k):
    # To store the sum of all the elements
    # of the array
    totalSum = 0
    for i in range (0,size):
        totalSum += array[i]
 
    # Sum of any sub-array cannot be
    # a floating point value
    if ((totalSum - k) % 2 == 1):
        return False
 
    # Required sub-array sum
    S = (totalSum - k) / 2
 
    sum = 0;
    for i in range (0,size):
        sum += array[i]
        if (sum == S):
            return True
     
 
    return False
 
 
# Driver Code
array= [2, 4, 1, 5]
k = 2
n = 4
if (solve(array, n, k)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by iAyushRaj.


C#
using System;
class GFG
{
 
// Function that return true if it is possible
// to divide the array into sub-arrays
// that satisfy the given condition
public static bool solve(int[] array, int size, int k)
{
    // To store the sum of all the elements
    // of the array
    int totalSum = 0;
    for (int i = 0; i < size; i++)
        totalSum += array[i];
 
    // Sum of any sub-array cannot be
    // a floating point value
    if ((totalSum - k) % 2 == 1)
        return false;
 
    // Required sub-array sum
    int S = (totalSum - k) / 2;
 
    int sum = 0;
    for (int i = 0; i < size; i++)
    {
        sum += array[i];
        if (sum == S)
            return true;
    }
 
    return false;
}
 
// Driver Code
public static void Main()
{
    int[] array = { 2, 4, 1, 5 };
    int k = 2;
    int size = 4;
     
    if (solve(array, size, k))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by iAyushRaj.


PHP


Javascript


输出:
No