Tug of war - Arrange elements of array in two equal parts with same sum

Reverse Array
Pair Sum Equals Number
Floor and Ceil
Largest Sum Subarray
Missing Number
Odd Occuring Number
Sort by Frequency
First and Second Smallest
Majority Element
Difference between elements
Arrange Even and Odd
Closest Sum Elements
Distinct Elements
First Repeating Number
Common Elements in Arrays
Smallest Missing Number
Max Adjacent Sum
Find Occurrences of Number
Rotate Image
Distance Between Numbers
Pair with given difference
Product Array Puzzle
Replace Elements
First Repeating Element
Move Zeroes to End
Pythagorean Triplets
Maximum Average Subarray
Subarray Sum Equal to X
Leaders in Array
Largest Pair Sum
Segregate 0s 1s and 2s
Find Duplicates
Consecutive Elements Check
Triplet with given Sum
Binary Search in Sorted Array
Fixed point in sorted array
Reorder Elements by Indexes
Merging two sorted arrays
Find next greater number
Reorder array using given indexes
Count of triplets with sum less than given value
Merge two sorted arrays
Subarray and Subsequence
Rearrange array maximum minimum form
Find the lost element from a duplicated array
Count minimum steps to get the given array
Maximum element in an array which is increasing and then decreasing
Minimum number of jumps to reach the end of an array
Subarray with given sum
Length of Longest Increasing Subsequence
Smallest positive number missing unsorted array
The Celebrity Problem
Sorted subsequence of size 3
Partition Problem
Find pair with given difference
Maximum length of chain pairs
Four elements that sum to given
Maximum circular subarray sum
Count possible triangles
Longest Increasing Subsequence
Petrol Bunks Tour
Tug of War
Counting Sort
Maximum Repeating Number
Positive Negative Arrangement
Find a Peak element
Elements More Than n/k
Max Product Subsequence
Longest Bitonic subarray in an array
Number of smaller elements on right side
Implement two stacks in an array
Maximum sum increasing subsequence
Find the two numbers with odd occurrences in an unsorted array
Largest subarray with equal number of 0's and 1's
Maximum product subarray
Replace every element with the greatest on the right side in an array
Sorting a k sorted array
Find the row with maximum number of 1's
Shuffle a given array
Iterative Implementation of quick sort
Arrange given numbers to form the biggest number
Pancake sorting
Pancake sorting Problem
Maximum Subarray Sum using Divide and Conquer
Merge Overlapping Intervals
Stock Buy Sell to Maximize Profit
Sort Elements by frequency
Print all possible combinations of r elements in a given array of size n
Monotonically increasing function
Minimum element in a sorted and rotated array
Merge k Sorted Arrays
Flip Zeroes for Consecutive 1's
Least Average Subarray
Longest span with same sum in two binary arrays
Form minimum number from given sequence of D's and I's
Number of strictly increasing subarrays
Minimum difference between any two elements in an array
Number of pairs with given sum
Make Array Palindrome
Dynamic Programming, Longest Bitonic SubSequence

In the given array of integers, divide the array into two subsets of size n/2 size each so that the difference of the sum of two subsets is as minimum as possible

If n is even each subset size is n/2. If n is odd one subset size must be n-1/2 and size of another is n+1/2. Sum of elements of both subsets should have approximately same sum.


a) Input array: [1, 4, 7, -3, 15, 11, 14, 13, 10, 8]
    subset1: [4, 7, 8, 11, 13]
    subset2:[10, 14, 6, 15, -3]
    Here, size is 10(even) each array should be of length 5
    (4 + 7 + 8 + 11 + 13) - (10 + 14 + 6 + 15 + -3) = 0, minimum difference.           

b) Input array: [-1, -3, -5, -6, 4, 11, 17, 23, 27, 19]
    subset1: [23, 17, -6, 4]
    subset2: [27, -1, -3, 19, -5]
    Here, size is 9(odd),one array size is 5 and other will be 4.
    (23 + 17 + -6 + 4) – (27 + -1 + -3 + 19 + -5) = -1   


Time complexity: O(n^2)

Try every subset of half size and compare with the remaining half.

a. We initialize current set as empty set and add elements one by one.

b. For all the elements it should be part of current set or part of remaining set.

c. For current set becomes n/2, we check whether this solution is better than the best solution available until   that point.

d. Update if it is the best solution. Print the final solution.

C++ Program

#include <iostream>
#include <stdlib.h>
#include <limits.h>

using namespace std;

void TugofWarRecur(int* array, int n, bool* current_elements, int selected_elements_count,bool* solution, int* min_diff, int sum, int current_sum, int current_position)
    if (current_position == n)
    if ((n/2 - selected_elements_count) > (n - current_position))
    TugofWarRecur(array, n, current_elements, selected_elements_count,solution, min_diff, sum, current_sum, current_position+1);
    current_sum = current_sum + array[current_position];
    current_elements[current_position] = true;
    if (selected_elements_count == n/2)
        if (abs(sum/2 - current_sum) < *min_diff)
            *min_diff = abs(sum/2 - current_sum);
            for (int i = 0; i<n; i++)
                solution[i] = current_elements[i];
        TugofWarRecur(array, n, current_elements, selected_elements_count, solution, min_diff, sum, current_sum, current_position+1);
    current_elements[current_position] = false;
void TugOfWar(int *array, int n)
    bool* current_elements = new bool[n];
    bool* solution = new bool[n];
    int min_diff = INT_MAX;
    int sum = 0;
    for (int i=0; i<n; i++)
        sum =sum + array[i];
        current_elements[i] =  solution[i] = false;
    TugofWarRecur(array, n, current_elements, 0, solution, &min_diff, sum, 0, 0);
    cout << "subset 1: ";
    for (int i=0; i<n; i++)
        if(solution[i] == true)
            cout << array[i] << " ";
    cout << "\nsubset 2: ";
    for (int i=0; i<n; i++)
        if(solution[i] == false)
            cout << array[i] << " ";
//Main function
int main()
    int input_array[] = {1, 4, 7, -3, 15, 11, 14, 13, 10, 8};
    int size = sizeof(input_array)/sizeof(input_array[0]);
    return 0;
Try It

C++ Online Test - Check Your Rank across the World

Next > < Prev
Scroll to Top