Question

Subset Sum-2 Write an algorithm (in comments) and specify the big O, and a C program to solve the problern below. Read the in
0 0
Add a comment Improve this question Transcribed image text
Answer #1

as per HomeworkLib rules i cannot answer more than one question so please post again for the rest.

  • The problem is to tell whether there is a subset in a set in which the sum of all elements is less than given 'K'
  • we have to return true or false as answer as to whether such a set exists or not
  • What is important to note here that size of a subset of a set can be as small as a single element. eg:-{1,2,3,4,5} the subset of this set can be {1},{2},{3},{4},{5},{1,2},{1,3}... etc
  • so to test whether a subset exists whose sum of elements is less than 'K' we just need to find out if there is a single element in the parent set whose value is less than K.
  • eg: suppose a set {1,2,3}, here K=2, subsets of set - {1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}, now subset {1} is the only subset in which sum of all elements is less than K(2), bu the important point is that the subset {1} contains only a single element.
  • Hence In order to find out whether any subset of a set exists whose sum is less than K, we just need to check whether there is any single element in the parent set whose value is less than K, if there is then return TRUE otherwise return FALSE

ALGORITHM

// Take a set of elements as input

// Take K as input

// Iterate from first to last element of the set

// if while iterating any element in the set is found whose value is less than K, end loop and return TRUE from the function

// if the iteration ends and no element in the set is found whose value is less than K return FALSE

C CODE

#include <stdio.h>

// function which checks whether any element exists in the set which is less than K
// function takes three parameters
// arr- array of elements
// n- size of array
// K- value for comparison
int check(int *arr,int n,int K)
{

   // iterate through the array
   for(int i=0;i<n;i++)
   {

       // if any element exist which is less than K return true
       if(arr[i]<K)
           return 1;
   }

  
   // no element exist which is less than K so return false
   return 0;
}

int main(void) {

   // number of elements in the set
   int n;
   scanf("%d",&n);

  
   // array of numbers
   int arr[n];
  
   // input of set elements
   for (int i=0;i<n;i++)
       scanf("%d",&arr[i]);

   // value for comparison 'K'
int K;
   scanf("%d",&K);

  
   // call check() function and see whether it return true or false
   if(check(arr,n,K))
       printf("Yes there exists a subset of the given set whose sum of elements is less than K");
   else
       printf("No such subset exists");
  
   return 0;
}


OUTPUT:

5 //(number of elements)

1 2 3 4 5 //elements of set

2 //K

Yes there exists a subset of the given set whose sum of elements is less than K

BIG-O

The Big-O of above algorithm is O(n), where 'n' is number of elements in the set

In worst-case the algorithm will have to iterate from first element of the set to last element of the set to find out whether there exists any element whose value is less than K

Add a comment
Know the answer?
Add Answer to:
Subset Sum-2 Write an algorithm (in comments) and specify the big O, and a C program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in java 3) Sum. Write a program that prompts the user to read two integers and...

    in java 3) Sum. Write a program that prompts the user to read two integers and displays their sum. Your program should prompt the user to read the number again if the input is incorrect.

  • EVERYTHING IN C# Write a program that includes a method that returns the sum of all...

    EVERYTHING IN C# Write a program that includes a method that returns the sum of all the elements of an ArrayList of Integer Objects. Allow the user to enter the integers to be added to your ArrayList from the console (max of 10 integers). And also write a program that includes a method that returns the sum of all the elements of a Linked List of Integers. Allow the user to enter the integers to be added to your Linked...

  • Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers

    Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • Hi please help with this program C++ language thanks Write a recursive solution to solve the...

    Hi please help with this program C++ language thanks Write a recursive solution to solve the Partition problem. Partition: Given a set A of n integers a, a, .....a., can you find a subset A_1 of integers such that the sum of the Integers in A_1 is half of the Sum of all the n Integers (that Is, sigma_alpha 1 A_1 a_i)? Input: 1.Number of integers of set A: n 2.n Integers: a_1 a_2, a_n. Output: Print out yes if...

  • 1) A Java program that implements an insertion sort algorithm. (InsertionSort.java): Must include the proper comments...

    1) A Java program that implements an insertion sort algorithm. (InsertionSort.java): Must include the proper comments in the code. 2) A report in pdf. It contains: a) the pseudocode of the insertion sort algorithm and assertions between lines of the algorithm. b) As insertion sort has a nested-loop of two layers, you need to put one assertion in each loop. c) a proof of the partial correctness of the algorithm using mathematical induction c.1) prove the correctness of the two...

  • C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional...

    C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional arrays. Your program should include three functions: one for getting input data, one for calculating the sum of matrices, and one for printing the result. a) Function inputMatrix: This Function prompts the user to enter the data and stores data inside two-dimensional array. b) Function addMatrices: This function calculatesthe sum result of two matrices. c) Function printMatrix: This function prints the matrix to screen....

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

  • Write a program that collects the dimensions of two rectangles. For each rectangle, the program collects...

    Write a program that collects the dimensions of two rectangles. For each rectangle, the program collects the width and length. Then, the program should print one of the following messages depending on the relative values of the rectangle sizes: -          Rectangle 1 is bigger than Rectangle 2. -          Rectangle 1 is the same size as Rectangle 2. -          Rectangle 2 is bigger than Rectangle 1. For example, consider the sample output of the proposed program (the bold font represents user...

  • Having trouble coding this for C++. Please add comments for better understanding! Write a program that...

    Having trouble coding this for C++. Please add comments for better understanding! Write a program that draws two triangles by using a character 'X' as examples below. The program must prompts a user for an integer larger than 2. The user input will then be used to set the size of the output. If a user input an even value, the program will increase it by one before drawing the box. Example 1: Input: 3 Output: XXX XXX XX XX...

  • a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n...

    a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n positive integers and determines whether there are two distinct elements of the list that have as their sum a third element of the list. That is, whether there exists i, j.k such that iヂj, i关k,j关k and ai + aj = ak. The algorithm should loop through all triples of elements of the list checking whether the sum of the first two is the third...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT