Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if array can be divided into two sub-arrays such that their absolute difference is Ks in Python
In this article we are going to check whether the given array can be divided into two sub-arrays in such a way that the absolute difference between the sums of these two sub-arrays is equal to the given value k.
The absolute difference is nothing but the non-negative gap between two numbers, calculated as abs(sum1-sum2). In this task the split must produce two non-empty sub-arrays i.e. at least one element should be present in each part.
Example Scenarios
Following are the example scenarios:
Scenario 1
<b>Input</b>: array = [3, 1, 4, 2, 2], K = 4 <b>Output</b>: YES <b>Explanation</b>: In the given array, the possible split is: Left sub-array = [3, 1] ? sum = 4 Right sub-array = [4, 2, 2] ? sum = 8 The Absolute difference = |4 - 8| = 4, which matches K. Hence, the answer is YES.
Scenario 2
<b>Input</b>: array = [3, 1, 4, 2, 2], K = 2 <b>Output</b>: NO <b>Explanation</b>: In the given array checking all possible splits: [3] and [1, 4, 2, 2] ? difference = 6 [3, 1] and [4, 2, 2] ? difference = 4 [3, 1, 4] and [2, 2] ? difference = 4 [3, 1, 4, 2] and [2] ? difference = 8 None of these differences equal K = 2, so the answer is NO.
Algorithm to Solve the Problem
The algorithm follows these steps:
- Step 1 ? At the start, we will calculate the total sum of the array.
- Step 2 ? Then, traverse through the array, maintaining a running left sum.
- Step 3 ? At each point: Calculate right_sum = total_sum - left_sum, then check if abs(left_sum - right_sum) == k
- Step 4 ? If the condition is satisfied, return True; otherwise False.
Implementation
Let's implement the solution to check if an array can be divided into two sub-arrays with absolute difference k ?
def can_divide_array(array, k):
total_sum = sum(array)
left_sum = 0
# Try each possible split point
for i in range(len(array) - 1):
left_sum += array[i]
right_sum = total_sum - left_sum
# Check if absolute difference equals k
if abs(left_sum - right_sum) == k:
return True
return False
# Test with example scenarios
array1 = [5, 1, 2, 2]
k1 = 0
print(f"Array {array1}, K = {k1}: {'YES' if can_divide_array(array1, k1) else 'NO'}")
array2 = [3, 1, 4, 2, 2]
k2 = 2
print(f"Array {array2}, K = {k2}: {'YES' if can_divide_array(array2, k2) else 'NO'}")
array3 = [3, 1, 4, 2, 2]
k3 = 4
print(f"Array {array3}, K = {k3}: {'YES' if can_divide_array(array3, k3) else 'NO'}")
The output of the above code is ?
Array [5, 1, 2, 2], K = 0: YES Array [3, 1, 4, 2, 2], K = 2: NO Array [3, 1, 4, 2, 2], K = 4: YES
How It Works
The algorithm works by trying all possible split points in the array. For each split point i, it calculates:
- Left sum ? Sum of elements from index 0 to i
- Right sum ? Sum of remaining elements (total_sum - left_sum)
- Absolute difference ? |left_sum - right_sum|
If any split point produces the desired absolute difference k, the function returns True.
Time and Space Complexity
- Time Complexity ? O(n) where n is the length of the array, as we traverse the array once
- Space Complexity ? O(1) as we only use a constant amount of extra space
Conclusion
This algorithm efficiently checks if an array can be divided into two sub-arrays with a given absolute difference. It uses a single pass approach with constant space complexity, making it optimal for this problem.
