Question

In: Computer Science

// Given an int array of size elements, determine if there are k elements that add...

// Given an int array of size elements, determine if there are k elements that add up to sum.
// The array holds integers, both positive and negative and zero.
// It is not possible to add zero elements (that's when k==0) to any sum, not even zero.
// It is not possible to add any elements from an empty array.
// Must be recursive and not iterative
//bool K_element_sum(size_t k, int sum, int arr[], size_t size){}

Solutions

Expert Solution

In this program, we have to check if the given array contains k elements that sum up to the given sum

we will use a recursive implementation to do this.

The following are the base conditions:

1.) if k is 0 and sum is 0, return true

2.) if k is 0 and sum is not zero, return false

3.) if size if 0, return false

The recursive definition is as follows:

1.) if K_element_sum(k-1, sum-arr[size-1], arr, size-1) is true, then return true

2.) if K_element_sum(k,sum,arr,size-1) is true, then return true

3.) otherwise return false

function:

bool K_element_sum(size_t k, int sum, int arr[], size_t size){
if(k==0 && sum == 0)
   return true;
if(k==0)
   return false;
if(size==0)
   return false;
if(K_element_sum(k-1, sum-arr[size-1], arr, size-1))
   return true;
if(K_element_sum(k,sum,arr,size-1))
   return true;
return false;
}

sample program to test this function:

#include <iostream>

using namespace std;

bool K_element_sum(size_t k, int sum, int arr[], size_t size){
if(k==0 && sum == 0)
   return true;
if(k==0)
   return false;
if(size==0)
   return false;
if(K_element_sum(k-1, sum-arr[size-1], arr, size-1))
   return true;
if(K_element_sum(k,sum,arr,size-1))
   return true;
return false;
}

int main(){
int arr[] = {1,2,3,4,5};
int k = 2;
int sum = 6;
int size = sizeof(arr) / sizeof(arr[0]);
if (K_element_sum(k, sum, arr, size))
cout<<"True\n";
else
cout<<"False\n";
return 0;
}

output:


Related Solutions

A.) myNums is an array of 50 elements of type int and k is an int...
A.) myNums is an array of 50 elements of type int and k is an int variable. For which one we get the index of out of bounds? a. for (k = 0; k <= 49; k++)     cout << myNums[k] << " "; b. for (k = 1; k < 50; k++)     cout << myNums[k] << " "; c. for (k = 0; k <= 50; k++)     cout << myNums[k] << " "; d. for (k =...
This is C++ programming Given an int variable k, an int array incompletes that has been...
This is C++ programming Given an int variable k, an int array incompletes that has been declared and initialized, an int variable nIncompletes that contains the number of elements in the array, an int variable studentID that has been initialized, and an int variable numberOfIncompletes, Write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes. You may use only k, incompletes, nIncompletes, studentID, and numberOfIncompletes. I tried this code...
//   Given an array of size 9, with the values of 1-9, determine if the array...
//   Given an array of size 9, with the values of 1-9, determine if the array //   is valid or not. //   Display a message stating the row is VALId, or state its INVALID and what //   was the index number that determined the data invalid. // //   Use this java code as a start of your code. //   Test the array data by changing the values. //============================================================================= import java.util.*;    public class Soduko_ValidateRow    { public static void main(String...
Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int sum = 0; int i; for(i = 0; i < arr.length; i++){ sum += arr[1]; } return sum; } public int getAverageValueInArray(int[] arr){ // return the average value of array elements int value = 0; for(int i = 0; i < arr.length; i++){ double average = value/ arr.length; } return value; } public int getNumberOfEvens(int[] arr){ //return the number of even values found in...
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
The following pseudocode finds the maximum element in an array of size n. Int MAX (int...
The following pseudocode finds the maximum element in an array of size n. Int MAX (int A [ ], int n) { M = A[0]; for i = 1 to n – 1     if (A[i] > M)         M = A[i]        //Update the max return M; } Write a recursive version of this program. Let f(n) be the number of key comparisons performed by this algorithm. Write a recurrence equation for f(n). Prove by induction that the solution of...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file...
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int...
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int containsWithin(const int *arr,int size , int k,int i , int j); int *paddedCopy(const int *arr,int oleSize , int newSize); void reverse(int *arr,int size); int *reverseCopy(const int *arr,int size); //array_utils.c #include"array_utils.h" #include<stdlib.h> int contains(const int *arr,int size , int k){    int i=0;    for(i=0;i<size;i++){        if(arr[i] == k)return 1;    }    return 0; } int containsWithin(const int *arr,int size , int k,int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT