`{"id":"7bad0817-f4b5-4b8e-b43c-71d7416858ea","name":"K Subsets With Equal Sum","description":"1. You are given an array of n distinct integers.\r\n2. You have to divide these n integers into k non-empty subsets such that sum of integers of every \r\n subset is same.\r\n3. If it is not possible to divide, then print \"-1\".\r\n\r\nNote -> Check out the question video and write the recursive code as it is intended without \r\n changing signature. The judge can't force you but intends you to teach a concept.","inputFormat":"A number n\r\nn distinct integers \r\nA number k","outputFormat":"Check the sample ouput and question video.","constraints":"1 &lt;= n &lt;= 20\r\n1 &lt;= arr[i] &lt;= 100\r\n1 &lt;= k &lt;= n","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n\r\n\tpublic static void solution(int[] arr, int vidx,int n , int k,int[] subsetSum,int ssssf, ArrayList<ArrayList<Integer>> ans) {\r\n\t\t//write your code here\r\n\t}\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tint n = scn.nextInt();\r\n\t\tint[] arr = new int[n];\r\n\t\tint sum = 0;\r\n\t\tfor(int i = 0 ; i < arr.length; i++) {\r\n\t\t\tarr[i] = scn.nextInt();\r\n\t\t\tsum += arr[i];\r\n\t\t}\r\n\t\tint k = scn.nextInt();\r\n\t\t// if k is equal to 1, then whole array is your answer \r\n\t\tif(k == 1) {\r\n\t\t\tSystem.out.print(\"[\");\r\n\t\t\tfor(int i = 0 ; i < arr.length; i++) {\r\n\t\t\t\tSystem.out.print(arr[i] + \", \");\r\n\t\t\t}\r\n\t\t\tSystem.out.println(\"]\");\r\n\t\t\treturn;\r\n\t\t}\r\n\t\t//if there are more subsets than no. of elements in array or sum of all elements is not divisible by k\r\n\t\tif(k > n || sum % k != 0) {\r\n\t\t\tSystem.out.println(\"-1\");\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tint[] subsetSum = new int[k];\r\n\t\tArrayList<ArrayList<Integer>> ans = new ArrayList<>();\r\n\t\tfor(int i = 0; i < k; i++) {\r\n\t\t\tans.add(new ArrayList<>());\r\n\t\t}\r\n\t\tsolution(arr,0,n,k,subsetSum,0,ans);\r\n\t}\r\n\t\r\n\t\r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"6\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n3","sampleOutput":"[1, 6] [2, 5] [3, 4] \r\n","questionVideo":"https://www.youtube.com/embed/rszwy53vaP0?end=45","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"082986ae-d618-4a59-9ab3-6d79056679a4","name":"Recursion and Backtracking For Intermediate","slug":"recursion-and-backtracking-for-intermediate-330","type":0},{"id":"c858bebf-d123-4dab-b995-47c64f7c60a3","name":"K Subsets With Equal Sum","slug":"k-subsets-with-equal-sum","type":1}],"next":{"id":"2fac67f3-b404-475b-87a7-b93bc06832e6","name":"K Subsets With Equal Sum MCQ","type":0,"slug":"k-subsets-with-equal-sum-mcq"},"prev":{"id":"11761899-e2f2-437b-a7cf-0348fd6d53e8","name":"All Palindromic Permutations MCQ","type":0,"slug":"all-palindromic-permutations-mcq"}}}`

# K Subsets With Equal Sum

1. You are given an array of n distinct integers. 2. You have to divide these n integers into k non-empty subsets such that sum of integers of every subset is same. 3. If it is not possible to divide, then print "-1". Note -> Check out the question video and write the recursive code as it is intended without changing signature. The judge can't force you but intends you to teach a concept.

`{"id":"7bad0817-f4b5-4b8e-b43c-71d7416858ea","name":"K Subsets With Equal Sum","description":"1. You are given an array of n distinct integers.\r\n2. You have to divide these n integers into k non-empty subsets such that sum of integers of every \r\n subset is same.\r\n3. If it is not possible to divide, then print \"-1\".\r\n\r\nNote -> Check out the question video and write the recursive code as it is intended without \r\n changing signature. The judge can't force you but intends you to teach a concept.","inputFormat":"A number n\r\nn distinct integers \r\nA number k","outputFormat":"Check the sample ouput and question video.","constraints":"1 &lt;= n &lt;= 20\r\n1 &lt;= arr[i] &lt;= 100\r\n1 &lt;= k &lt;= n","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n\r\n\tpublic static void solution(int[] arr, int vidx,int n , int k,int[] subsetSum,int ssssf, ArrayList<ArrayList<Integer>> ans) {\r\n\t\t//write your code here\r\n\t}\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tint n = scn.nextInt();\r\n\t\tint[] arr = new int[n];\r\n\t\tint sum = 0;\r\n\t\tfor(int i = 0 ; i < arr.length; i++) {\r\n\t\t\tarr[i] = scn.nextInt();\r\n\t\t\tsum += arr[i];\r\n\t\t}\r\n\t\tint k = scn.nextInt();\r\n\t\t// if k is equal to 1, then whole array is your answer \r\n\t\tif(k == 1) {\r\n\t\t\tSystem.out.print(\"[\");\r\n\t\t\tfor(int i = 0 ; i < arr.length; i++) {\r\n\t\t\t\tSystem.out.print(arr[i] + \", \");\r\n\t\t\t}\r\n\t\t\tSystem.out.println(\"]\");\r\n\t\t\treturn;\r\n\t\t}\r\n\t\t//if there are more subsets than no. of elements in array or sum of all elements is not divisible by k\r\n\t\tif(k > n || sum % k != 0) {\r\n\t\t\tSystem.out.println(\"-1\");\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tint[] subsetSum = new int[k];\r\n\t\tArrayList<ArrayList<Integer>> ans = new ArrayList<>();\r\n\t\tfor(int i = 0; i < k; i++) {\r\n\t\t\tans.add(new ArrayList<>());\r\n\t\t}\r\n\t\tsolution(arr,0,n,k,subsetSum,0,ans);\r\n\t}\r\n\t\r\n\t\r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"6\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n3","sampleOutput":"[1, 6] [2, 5] [3, 4] \r\n","questionVideo":"https://www.youtube.com/embed/rszwy53vaP0?end=45","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"082986ae-d618-4a59-9ab3-6d79056679a4","name":"Recursion and Backtracking For Intermediate","slug":"recursion-and-backtracking-for-intermediate-330","type":0},{"id":"c858bebf-d123-4dab-b995-47c64f7c60a3","name":"K Subsets With Equal Sum","slug":"k-subsets-with-equal-sum","type":1}],"next":{"id":"2fac67f3-b404-475b-87a7-b93bc06832e6","name":"K Subsets With Equal Sum MCQ","type":0,"slug":"k-subsets-with-equal-sum-mcq"},"prev":{"id":"11761899-e2f2-437b-a7cf-0348fd6d53e8","name":"All Palindromic Permutations MCQ","type":0,"slug":"all-palindromic-permutations-mcq"}}}` Editor

# K Subsets With Equal Sum

medium

1. You are given an array of n distinct integers. 2. You have to divide these n integers into k non-empty subsets such that sum of integers of every subset is same. 3. If it is not possible to divide, then print "-1". Note -> Check out the question video and write the recursive code as it is intended without changing signature. The judge can't force you but intends you to teach a concept.

## Constraints

1 <= n <= 20 1 <= arr[i] <= 100 1 <= k <= n

## Format

### Input

A number n n distinct integers A number k

### Output

Check the sample ouput and question video.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}6 1 2 3 4 5 6 3```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}[1, 6] [2, 5] [3, 4] ```

Question Video

Discussions

Show Discussion

Related Resources 