{"id":"b8b1da99-6ca9-4c27-9533-57f254abe717","name":"Tug Of War","description":"1. You are given an array of n integers.\r\n2. You have to divide these n integers into 2 subsets such that the difference of sum of two subsets \r\n is as minimum as possible.\r\n3. If n is even, both set will contain exactly n/2 elements. If is odd, one set will contain (n-1)/2 and \r\n other set will contain (n+1)/2 elements.\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 integers ","outputFormat":"Check the sample ouput and question video.","constraints":"1 &lt;= n &lt;= 20\r\n1 &lt;= arr[i] &lt;= 100","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tint[] arr = new int[scn.nextInt()];\r\n\t\tfor (int i = 0; i < arr.length; i++) {\r\n\t\t\tarr[i] = scn.nextInt();\r\n\t\t}\r\n\t\tsolve(arr, 0, new ArrayList<>(), new ArrayList<>(), 0, 0);\r\n\t\tSystem.out.println(ans);\r\n\t}\r\n\r\n\tstatic int mindiff = Integer.MAX_VALUE;\r\n\tstatic String ans = \"\";\r\n\r\n\tpublic static void solve(int[] arr, int vidx, ArrayList<Integer> set1, ArrayList<Integer> set2, int soset1,\r\n\t\t\tint soset2) {\r\n\t\t//write your code here\r\n\r\n\t}\r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"6\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n","sampleOutput":"[1, 3, 6] [2, 4, 5]\r\n","questionVideo":"https://www.youtube.com/embed/Q1fLW_zQr3M?end=108","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":"5f373e24-d00f-40b7-a9ea-f53928d82e4a","name":"Tug Of War","slug":"tug-of-war","type":1}],"next":{"id":"77df6048-2bda-4964-848a-54951d4e10d8","name":"Tug Of War MCQ","type":0,"slug":"tug-of-war-mcq"},"prev":{"id":"3139d7a9-db06-4d00-afd9-01d4eaeea162","name":"Largest number possible after at most k swaps MCQ","type":0,"slug":"largest-number-possible-after-at-most-k-swaps-mcq"}}}

Tug Of War

1. You are given an array of n integers. 2. You have to divide these n integers into 2 subsets such that the difference of sum of two subsets is as minimum as possible. 3. If n is even, both set will contain exactly n/2 elements. If is odd, one set will contain (n-1)/2 and other set will contain (n+1)/2 elements. 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":"b8b1da99-6ca9-4c27-9533-57f254abe717","name":"Tug Of War","description":"1. You are given an array of n integers.\r\n2. You have to divide these n integers into 2 subsets such that the difference of sum of two subsets \r\n is as minimum as possible.\r\n3. If n is even, both set will contain exactly n/2 elements. If is odd, one set will contain (n-1)/2 and \r\n other set will contain (n+1)/2 elements.\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 integers ","outputFormat":"Check the sample ouput and question video.","constraints":"1 &lt;= n &lt;= 20\r\n1 &lt;= arr[i] &lt;= 100","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tint[] arr = new int[scn.nextInt()];\r\n\t\tfor (int i = 0; i < arr.length; i++) {\r\n\t\t\tarr[i] = scn.nextInt();\r\n\t\t}\r\n\t\tsolve(arr, 0, new ArrayList<>(), new ArrayList<>(), 0, 0);\r\n\t\tSystem.out.println(ans);\r\n\t}\r\n\r\n\tstatic int mindiff = Integer.MAX_VALUE;\r\n\tstatic String ans = \"\";\r\n\r\n\tpublic static void solve(int[] arr, int vidx, ArrayList<Integer> set1, ArrayList<Integer> set2, int soset1,\r\n\t\t\tint soset2) {\r\n\t\t//write your code here\r\n\r\n\t}\r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"6\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n","sampleOutput":"[1, 3, 6] [2, 4, 5]\r\n","questionVideo":"https://www.youtube.com/embed/Q1fLW_zQr3M?end=108","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":"5f373e24-d00f-40b7-a9ea-f53928d82e4a","name":"Tug Of War","slug":"tug-of-war","type":1}],"next":{"id":"77df6048-2bda-4964-848a-54951d4e10d8","name":"Tug Of War MCQ","type":0,"slug":"tug-of-war-mcq"},"prev":{"id":"3139d7a9-db06-4d00-afd9-01d4eaeea162","name":"Largest number possible after at most k swaps MCQ","type":0,"slug":"largest-number-possible-after-at-most-k-swaps-mcq"}}}
plane

Editor


Loading...

Tug Of War

easy

1. You are given an array of n integers. 2. You have to divide these n integers into 2 subsets such that the difference of sum of two subsets is as minimum as possible. 3. If n is even, both set will contain exactly n/2 elements. If is odd, one set will contain (n-1)/2 and other set will contain (n+1)/2 elements. 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

Format

Input

A number n n integers

Output

Check the sample ouput and question video.

Example

Sample Input

6 1 2 3 4 5 6

Sample Output

[1, 3, 6] [2, 4, 5]

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode