is there any way that my program can make subsets of any given size?

FocusBTW.

Hellow there programmers I am facing a issue

#include<iostream>
using namespace std;
bool Check(int arr[], int size, int con)
{
    for (int i = 0; i < size; i++)
    {
        if (arr[i] == con)
        {
            return true;
        }
        for (int j = i+1; j < size;j++)
        {
            if (arr[i]+arr[j] == con)
            {
                return true;
            }
            for (int k = j + 1; k < size; k++)
            {
                if (arr[i] + arr[j] + arr[k] == con)
                {
                    return true;
                }
                for (int l = k + 1; l < size;l++)
                {
                    if (arr[i] + arr[j] + arr[k] + arr[l] == con)
                    {
                        return true;
                    }
                }
            }
        }   
    }
}
int main()
{
    int size;
    int con;
    cout << "Enter desire size of array" << endl;
    cin >> size;
    cout << "ENter number" << endl;
    cin >> con;
    int *arr = new int[size];
    for (int i = 0; i < size; i++)
    {
        cin >> *(arr + i);
    }
    if (Check(arr, size, con) == true)
    {
        cout << "YESSS!!";
    }
    else
    {
        cout << "NOOO!!";
    }
}

I have to make subsets of an array and add them individually({1,2} = 1+2) if i get the result match with user input(con) output will be YES or NO. now the problem I am facing is I dont know how much size will user input give if he/she puts size of array is 4, 4 loops will be needed. Is thr any one my subset program works on every size of array p.s: sorry for my bad english

scohe001

Here's a simple example of a recursive implementation of your function:

bool Check(int *arr, int size, int con, int curr_sum = 0)
{       
    for (int i = 0; i < size; i++)
    {
        int new_sum = curr_sum + arr[i];
        if (new_sum == con
            || Check(arr + i, size - i, con, new_sum))
        {
            return true;
        }
    }
    return false;
}

Here's how it works...

We pass around a curr_sum parameter that holds the sum from the parent recursion. The current recursion will go through adding all of its indexes to it, looking for curr_sum + arr[i] == con. If it doesn't, then we'll take the new sum (curr_sum + arr[i]) and put it through another round of recursion starting on the index after the one we're currently looking at.

BEWARE: this is an O(n^2) implementation that you're working with, so it'll be extremely slow (and since this is recursion, liable to stack overflow) as you deal with larger sized arrays.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is there any way I can speed up my python program?

how can I make my box respond to any screen size

Is there any way I can make my program panic rather than deadlock if I have lock a mutex twice in one thread?

Is there any way to make this Haskell program faster?

Is there any way so I can convert my given code to one line?

Is there any way i can make my function add a row to my table?

Is there any way I can make this code faster?

Will any of these crash my program?

Is there any way to make the text comes below the given headline in flutter?

Is there any way to redirect my page on given href link without loading

Is there any way that I can make packet injection to work on my MBA 2015 11"s Wireless NIC (Broadcom)?

Is there any way I can make my python number guesser more efficient?

Is there a way to know if any program is listening to my microphone. Windows 10

Is there any way to embed MongoDB charts in my kivy program using PyMongo?

Js/jQuery - This code sets a size to a variable, any way to make it responsive?

Is there any way to make a card container size square using CSS Grid?

Is there any way I can run a program at the exact start of a minute?

Is there any way I can use GLUT to meet this requirement in a Haskell program?

How can I make a library face to any other program language?

I'm getting a lot of "System program problem detected" error dialogs, is there any way I can make them go away?

Is there any way to make a call to linux kernel with my own softirq

Is there any way to make a live CD from my current installation?

Is there any way to make my Mcp3008 sampling uniform?

Is there any way to make my code loop itself with an else statement?

Is there any way to make output of my SQLPLUS script look better?

Is there any way I can make this small piece of code shorter? (Python)

Is there any way to make a text file that no users can read?

Is there any way where I can make a CNN output pixel locations?

Is there any way I can make my discord bot send same messages(without skipping the older messages) in different servers