- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Check if array can be divided into two sub-arrays such that their absolute difference is Ks in Python

Suppose, we are provided with an array "input_list" containing integer numbers. The problem given to us is to check if the given array can be divided into two halves, where the difference of the sum of two halves is equal to a number n. The number n will be provided beforehand.

So, if the input is like input_list= [9,2,5,6], n = 0, then the output will be “Possible”.

To solve this, we will follow these steps −

- list_total := sum of the values of input_list
- if (list_total - n) mod 2 is same as 1, then
- return "Not Possible"

- val := (list_total - n) / 2
- temp_sum := 0;
- for i in range 0 to size of input_list, do
- temp_sum := temp_sum + input_list[i]
- if temp_sum is same as val, then
- return "Possible"

- return "Not Possible"

Let us see the following implementation to get better understanding −

## Example

def solve(input_list,n): list_total = sum(input_list) if (list_total - n) % 2 == 1: return "Not Possible" val = (list_total - n) / 2 temp_sum = 0; for i in range (0,len(input_list)): temp_sum += input_list[i] if (temp_sum == val): return "Possible" return "Not Possible" input_list= [9,2,5,6] n = 0 print(solve(input_list, n))

## Input

[9,2,5,6], 0

## Output

Possible

- Related Articles
- Check if an array can be divided into pairs whose sum is divisible by k in Python
- Check if any square (with one colored cell) can be divided into two equal parts in Python
- Find sub-arrays from given two arrays such that they have equal sum in Python
- Find if array can be divided into two subarrays of equal sum in C++
- Check if a sorted array can be divided in pairs whose sum is k in Python
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts in Python
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum in Python
- Check if a large number can be divided into two or more segments of equal sum in C++
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts in C++
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M in Python
- Can array be divided into n partitions with equal sums in JavaScript
- Check if reversing a sub array make the array sorted in Python
- Check if absolute difference of consecutive nodes is 1 in Linked List in Python
- Check if array can be sorted with one swap in Python
- Find maximum number of elements such that their absolute difference is less than or equal to 1 in C++

Advertisements