Javatpoint Logo
Javatpoint Logo

Three Partition Problem in Java

It is very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to solve three partition problem in Java with different approaches and logic. Also, we will create Java programs for the same.

Problem Statement

An array of non-negative numbers is given. Our task is to check whether the input array can be partitioned into the three completely disjoint subsets or not, such that the sum of all the elements of one subset is equal to the sum of all the elements of the other subset. Note that all the elements of the input array have to be included. Display appropriate messages on the console accordingly.

Example 1:

Input

int inputArr[] = {3, 7, 1, 2, 4, 5, 8}

Output: The given input array can be partitioned into three disjoint subsets of an equal sum of elements.

Explanation: Consider the following subsets.

Subset1 = {3, 7} sum = 3 + 7 = 1=, Subset2 = {1, 4, 5} sum = 1 + 4 + 5 = 10,

Subset3 = {2, 8} sum = 2 + 8 = 10

Thus, we see that we got the three subsets of an equal sum of elements.

Example 2:

Input

int inputArr[] = {6, 2, 3, 9, 8, 18, 96}

Output: The given input array cannot be partitioned into three disjoint subsets of an equal sum of elements.

Explanation: Any set of three subsets will not give the equal sum of elements for the given input array.

Approach: Using Recursion

First of all, we will check whether the sum of all the elements of the input array is a multiple of 3 or not. If it is not the multiple of 3, then it is not possible to do the partition. If yes, then we have to check whether three disjoint sets exist or not whose sum of elements is equal to (total sum of elements of the input array) / 3 or not. It can be found by considering each element of the input array one by one. For every element, there are the following three possibilities:

1) Consider the element for the first subset, and apply recursion for the other elements.

2) Consider the element for the second subset, and apply recursion for the other elements.

3) Consider the element for the third subset, and apply recursion for the other elements.
Since we have to include every item of the input array, the base case of the recursion will be when all the items of the input array have been considered. The recursion will return true when three disjoint subsets are found.

FileName: ThreePartition.java

Output:

For the input array: 
3 7 1 2 4 5 8 
It is possible to partition it into three disjoint subsets.

For the input array: 
6 2 3 9 8 18 96 
It is not possible to partition it into three disjoint subsets.

Complexity Analysis: The time complexity of the above program is exponential.

The above program gives the correct output but will consume a lot of time giving the output. Thus, it is not suitable to handle an input of a large size.

Approach: Using Dynamic Programming

To reduce the time complexity, we can take the help of dynamic programming. Observe the following program.

FileName: ThreePartition.java

Output:

For the input array: 
3 7 1 2 4 5 8 
It is possible to partition it into three disjoint subsets.

For the input array: 
6 2 3 9 8 18 96 
It is not possible to partition it into three disjoint subsets.

Complexity Analysis: The time complexity of the above program is O(sum3 × n) and also consumes O(sum3 × n) extra space, where the sum is the sum of all the elements of the input array, and n is the total number of elements of the input array.

Extension to The Problem

Another thing that can be asked by an interviewer is to print those three disjoint subsets whose sum is equal. For that, an array will be required to keep track of all the disjoint subset elements. Observe the following program.

FileName: ThreePartition.java

Output:

For the input array: 
3 7 1 2 4 5 8 
It is possible to partition it into three disjoint subsets.
Partition 0 is 2 8 
Partition 1 is 1 4 5 
Partition 2 is 3 7 

For the input array: 
6 2 3 9 8 18 96 
It is not possible to partition it into three disjoint subsets.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA