Question

In: Computer Science

Print all subset sums of a given array recursively and iteratively. ex) if the input is...

Print all subset sums of a given array recursively and iteratively.

ex) if the input is {1, 2, 3}, it should return {0,1,2,3,3,4,5,6}.

public int[] subsetSum1(int[] arr) { *recursive*}

public int[] subsetSum2(int[] arr) {*iterative*}

Solutions

Expert Solution

A. Recursive Approach to print all subset sums of a given array -

//Global variable to keep track of the current index of the result array
int index;

void subsetSumRecursive(int[] arr, int left, int right, int sum, int[] result) {    
    // Add the sum of the current subset to the result array
    if (left > right) { 
        result[index] = sum;
        index++;
        return; 
    } 
      
    // Subset that includes arr[left] 
    subsetSumRecursive(arr, left + 1, right, sum + arr[left], result); 
      
    // Subset that excludes arr[left] 
    subsetSumRecursive(arr, left + 1, right, sum, result); 
} 

public int[] subsetSum1 (int[] arr) {
    //Initialize necessary variables
    index = 0;
    int left = 0;
    int right = arr.length-1;
    int sum = 0;
    int index = 0;
    int[] result = new int[1000];

    //call the recursive method
    subsetSumRecursive(arr, left, right, sum, result);

    return result;
}

B. Iterative Approach to print all subset sums of a given array -

public int[] subsetSum2(int[] arr)  {  
    int n = arr.length;
    int[] result = new result[1000];
    int index = 0;

    // There are total 2^n subsets  possible
    int total = 1 << n;  
  
    // Considering all numbers from 0 to 2^n - 1  
    for(int i = 0; i < total; i++)  {  
       int sum = 0;  
         
       // Consider the binary form of the current number for deciding which of the array elements to select  
       for(int j = 0; j < n; j++)  
          if ((i & (1 << j)) != 0)  
              sum += arr[j];  
         
       // Add the sum of the selected elements to the result array 
       result[index] = sum;
       index++;
    }  

    return result;
}  
 

Related Solutions

Given a 2D array a, sum up ALL the edges of the array. Ex. int a[...
Given a 2D array a, sum up ALL the edges of the array. Ex. int a[ ][ ] = { {1, 2, 3, 4},                        {5, 6, 7, 8},                        {9, 10, 11, 12} }; OUTPUT: Sum of the edges = 65
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...
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++....
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output examples for duplicate-free arrays...
How to print the element in the fifth row and seventh column given this array declaration...
How to print the element in the fifth row and seventh column given this array declaration in dev c: int hello[5][10];
Print "Censored" if userInput contains the word "darn", else print userInput. End with newline. Ex: If...
Print "Censored" if userInput contains the word "darn", else print userInput. End with newline. Ex: If userInput is "That darn cat.", then output is: Censored Ex: If userInput is "Dang, that was scary!", then output is: Dang, that was scary! Note: If the submitted code has an out-of-range access, the system will stop running the code after a few seconds, and report "Program end never reached." The system doesn't print the test case that caused the reported message. #include <iostream>...
Print "Censored" if userInput contains "darn", else print iserInput. End with newline. Ex: If userInput is...
Print "Censored" if userInput contains "darn", else print iserInput. End with newline. Ex: If userInput is "That darn cat", then output is:
Using an array and a function, print the values of an array backwards. Please follow these...
Using an array and a function, print the values of an array backwards. Please follow these guidelines: - Setup your array manually (whichever values you want, as many as you want and whichever datatype you prefer). - Call your function. You should send two parameters to such function: the array’s length and the array. - Inside the function, go ahead and print the array backwards. - Your function shouldn’t return anything.
In C Write a program to read a one-dimensional array, print sum of all elements using...
In C Write a program to read a one-dimensional array, print sum of all elements using Dynamic Memory Allocation.
** IN C++ ** Ex: If the input is: April 11 the output is: Spring In...
** IN C++ ** Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring: March 20 - June 20 Summer: June 21 - September 21 Autumn: September 22 - December 20 Winter: December 21 - March 19 My code isn't working and I can't figure out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT