Check if array can be divided into two sub-arrays such that their absolute difference is K
Last Updated :
15 Sep, 2022
Given an array arr[] and an integer K, the task is to find whether the array can be divided into two sub-arrays such that the absolute difference of the sum of the elements of both the sub-arrays is K.
Examples:
Input: arr[] = {2, 4, 5, 1}, K = 0
Output: Yes
{2, 4} and {5, 1} are the two possible sub-arrays.
|(2 + 4) – (5 + 1)| = |6 – 6| = 0
Input: arr[] = {2, 4, 1, 5}, K = 2
Output: No
Approach:
- Assume there exists an answer, let the sum of elements of the sub-array (with smaller sum) is S.
- Sum of the elements of the second array will be S + K.
- And, S + S + K must be equal to sum of all the elements of the array say totalSum = 2 *S + K.
- S = (totalSum – K) / 2
- Now, traverse the array till we achieve a sum of S starting from the first element and if its not possible then print No.
- Else print Yes.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool solve( int array[], int size, int k)
{
int totalSum = 0;
for ( int i = 0; i < size; i++)
totalSum += array[i];
if ((totalSum - k) % 2 == 1)
return false ;
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 ;
}
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
import java.io.*;
class GFG
{
static boolean solve( int array[], int size, int k)
{
int totalSum = 0 ;
for ( int i = 0 ; i < size; i++)
totalSum += array[i];
if ((totalSum - k) % 2 == 1 )
return false ;
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 ;
}
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" );
}
}
|
Python3
def solve(array,size,k):
totalSum = 0
for i in range ( 0 ,size):
totalSum + = array[i]
if ((totalSum - k) % 2 = = 1 ):
return False
S = (totalSum - k) / 2
sum = 0 ;
for i in range ( 0 ,size):
sum + = array[i]
if ( sum = = S):
return True
return False
array = [ 2 , 4 , 1 , 5 ]
k = 2
n = 4
if (solve(array, n, k)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
public static bool solve( int [] array, int size, int k)
{
int totalSum = 0;
for ( int i = 0; i < size; i++)
totalSum += array[i];
if ((totalSum - k) % 2 == 1)
return false ;
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 ;
}
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" );
}
}
|
PHP
<?php
function solve( $array , $size , $k )
{
$totalSum = 0;
for ( $i = 0; $i < $size ; $i ++)
$totalSum += $array [ $i ];
if (( $totalSum - $k ) % 2 == 1)
return false;
$S = ( $totalSum - $k ) / 2;
$sum = 0;
for ( $i = 0; $i < $size ; $i ++)
{
$sum += $array [ $i ];
if ( $sum == $S )
return true;
}
return false;
}
$array = array ( 2, 4, 1, 5 );
$k = 2;
$size = sizeof( $array );
if (solve( $array , $size , $k ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function solve(array, size, k)
{
let totalSum = 0;
for (let i = 0; i < size; i++)
totalSum += array[i];
if ((totalSum - k) % 2 == 1)
return false ;
let S = (totalSum - k) / 2;
let sum = 0;
for (let i = 0; i < size; i++)
{
sum += array[i];
if (sum == S)
return true ;
}
return false ;
}
let array = [ 2, 4, 1, 5 ];
let k = 2;
let size = array.length;
if (solve(array, size, k))
document.write( "Yes" );
else
document.write ( "No" );
</script>
|
Complexity Analysis:
- Time Complexity: O(n) where n is the size of the array.
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...