Timer is Running

Associated Course(s):
Amazon SDE Test Series

Given an integer array A[] of N elements, the task is to complete the function which returns true if the array A[] could be divided into K non-empty subsets such that the sum of elements in every subset is same.

**Note:** All elements of this array should be part of exactly one partition.

**Input:**

The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains 3 lines. The first line of each test case contains an integer denoting the size of the array. In the next line are N space-separated values of the array A[ ]. In the next line is an integer K.

**Output:**

The output for each test case will be 1 if the array could be divided into k subsets else 0 .

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **isKPartitionPossible()** which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).

**Expected Time Complexity:** O(K^{N-K}).

**Expected Auxiliary Space:** O(K).

**Constraints:**

1<=T<=100

1<=N,K<=100

1<=A[ ] <=100

**Example(To be used only for expected output):
Input:**

2

5

2 1 4 5 6

3

5

2 1 5 5 6

3

1

0

**Explanation:**

Input : A[] = [2, 1, 4, 5, 6], K = 3

Output : 1, as we can divide above array into 3 parts with equal sum as (2, 4), (1, 5), (6)

Input : A[] = [2, 1, 5, 5, 6], K = 3

Output : 0, as it is not possible to divide above array into 3 parts with equal sum.

Author: Shubham Joshi 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

abnandi04 | 801 |

KshamaGupta | 412 |

srivastavad270 | 223 |

bhavyapratap56 | 206 |

anshusingh20112001 | 181 |

abnandi04 | 776 |

KshamaGupta | 296 |

srivastavad270 | 223 |

bhavyapratap56 | 206 |

afreensartaj131 | 170 |

blackshadows | 5362 |

Ibrahim Nash | 5244 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4748 |

Login to report an issue on this page.