Question

In: Computer Science

Given an array ? of ? integers. Divide ? into three subsets such that the total...

  1. Given an array ? of ? integers. Divide ? into three subsets such that the total absolute difference of subset sums between them is minimum. Provide python source code, time complexity, and pseudocode. thank you

Solutions

Expert Solution

The problem can be solved using dynamic programming when the sum of the elements is not too big. We can create a 2D array dp[n+1][sum+1] where n is a number of elements in a given set and sum is the sum of all elements. We can construct the solution in a bottom-up manner.

The task is to divide the set into two parts.

We will consider the following factors for dividing it.

Let

dp[n+1][sum+1] = {1 if some subset from 1st to i'th has a sum

equal to j

0 otherwise}

i ranges from {1..n}

j ranges from {0..(sum of all elements)}  

So   

dp[n+1][sum+1] will be 1 if

1) The sum j is achieved including i'th item

2) The sum j is achieved excluding i'th item.

Let sum of all the elements be S.  

To find Minimum sum difference, w have to find j such

that Min{sum - j*2 : dp[n][j] == 1 }

where j varies from 0 to sum/2

The idea is, sum of S1 is j and it should be closest

to sum/2, i.e., 2*j should be closest to sum.

def findMin(a, n):

su = 0

# Calculate sum of all elements

su = sum(a)

# Create an 2d list to store

# results of subproblems

dp = [[0 for i in range(su + 1)]

for j in range(n + 1)]

# Initialize first column as true.

# 0 sum is possible

# with all elements.

for i in range(n + 1):

dp[i][0] = True

# Initialize top row, except dp[0][0],

# as false. With 0 elements, no other

# sum except 0 is possible

for j in range(1, su + 1):

dp[0][j] = False

# Fill the partition table in

# bottom up manner

for i in range(1, n + 1):

for j in range(1, su + 1):

# If i'th element is excluded

dp[i][j] = dp[i - 1][j]

# If i'th element is included

if a[i - 1] <= j

dp[i][j] |= dp[i - 1][j - a[i - 1]]

# Initialize difference

# of two sums.

diff = sys.maxsize

# Find the largest j such that dp[n][j]

# is true where j loops from sum/2 t0 0

for j in range(su // 2, -1, -1):

if dp[n][j] == True:

diff = su - (2 * j)

break

return diff

# Driver code

a = [ 3, 1, 4, 2, 2, 1 ]

n = len(a)

print("The minimum difference between "

"2 sets is ", findMin(a, n))

Time Complexity = O(n*sum) where n is the number of elements and sum is the sum of all elements.


Related Solutions

Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find the index of the negative integer, and compute its average complexity.
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
Problem Definition: Problem: Given an array of integers print all pairs of integers a and b...
Problem Definition: Problem: Given an array of integers print all pairs of integers a and b where a + b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a + b = 16 A= [ 10, 4, 6, 15, 3, 5, 1, 13] The following are pairs that sum to 16: 13, 3 6, 10 15, 1 Your program should print these...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and then find the index of the largest element in the array. You are to write two functions, printArray() and getIndexLargest(), which are called from the main function. printArray() outputs integers to std::cout with a space in between numbers and a newline at the end. getIndexLargest () returns the index of the largest element in the array. Recall that indexes start at 0. If there...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Given an array of integers, and given a specific value k (not equal to 0), produce...
Given an array of integers, and given a specific value k (not equal to 0), produce all unique pairs of values in the array which differ by k. For example, if the array has [1,4,9,12, 6, 15, 5, 13,17] and k=3, the answer would be (1,4 ) ( 9,12), ( 9,6), (12,15). If k=4, the answer would be (1,5), (9,5), (13,17), (9.13). Notice that you do not print the same answer twice, for instance (9,13), and (13,9).
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.Input:    6    2 3 4 11 22 320    where:First line represents the number of elements in the array.Second line represents the elements in the array.Output:    2 3 4 11 22Explanation: Element of the array 320 is the only one in the array which is a multiple of 5, so it is removed from the array.Assumptions:Array can be of size...
Using the template given in ParallelMergeSort.c write the functions to divide the original array into equal...
Using the template given in ParallelMergeSort.c write the functions to divide the original array into equal fractions given the number of threads and perform Parallel MergeSort pThreads. Your algorithm should work for 2 threads. ParallelMergeSort.c #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #define SIZE 100 int array[SIZE]; void fillArrayWithRandomNumbers(int arr[SIZE]) { for(int i = 0; i<SIZE; i++) array[i] = rand()%100; } void printArray(int arr[SIZE]) { for(int i = 0; i<SIZE; i++) printf("%5d", arr[i]); printf("\n"); } typedef struct...
Using the template given in ParallelMergeSort.c write the functions to divide the original array into equal...
Using the template given in ParallelMergeSort.c write the functions to divide the original array into equal fractions given the number of threads and perform Parallel MergeSort pThreads. Your algorithm should work for 2 threads. ParallelMergeSort.c #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #define SIZE 100 int array[SIZE]; void fillArrayWithRandomNumbers(int arr[SIZE]) { for(int i = 0; i<SIZE; i++) array[i] = rand()%100; } void printArray(int arr[SIZE]) { for(int i = 0; i<SIZE; i++) printf("%5d", arr[i]); printf("\n"); } typedef struct...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT