Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8140315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:07:20+00:00 2026-06-06T12:07:20+00:00

I am implementing the algorithm for subset sum : SUBSET SUM(X[1 .. n], T

  • 0

I am implementing the algorithm for subset sum :

SUBSET SUM(X[1 .. n], T ):
if T = 0
return T RUE
else if T < 0 or n = 0
return FALSE
else
return SUBSET SUM(X[2 .. n], T ) ∨ SUBSET SUM(X[2 .. n], T − X[1])

Please help me out in how can I pass the reduced array X[2…n] when recursing ?

This is the code I wrote and it causes a segmentation fault:

#include <stdio.h>

    int subsetsum(int a[], int sum, int size)
    {
            if(sum==0)
            return 1;
            else if (sum<0 || size <0)
            return  0;
            else
            return (subsetsum(a+1 , sum, size-1) || subsetsum(a+1, sum - *a, size-1));
    }
    `

    int main(int argc, char **argv)
    {
            int a[]={2,4,1,3,5},x;
            x=subsetsum(a,6,5);
            printf("%d",x);
            return 0;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:07:23+00:00Added an answer on June 6, 2026 at 12:07 pm

    Array’s in C/C++ are implicitly decayed into pointers to the original memory buffer representing the array when used as arguments to functions. So in order to pass X[2...n], you simply can increment the array pointer argument by 1. For instance, you could do something like the following:

    bool subset_sum(const int* array, const int array_size, int* sum)
    {
        //...your code for the rest of the algorithm
    
        subset_sum(array+1, array_size, sum) //one of the recursive calls
    }
    

    Passing the argument array+1 on the next recursive call will increment the array pointer by one, and take a pointer pointing to the array X[1...n] and now make it point to the array X[2...n]. You would use the array_size argument to detect the end of the array.

    Finally, you would call subset_sum like so:

    int array_numbers = {1, 2, 3, 4, 5, 6, 7};
    int sum = 0;
    
    subset_sum(array_numbers, sizeof(array_numbers)/sizeof(int), &sum);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on implementing a single pass depth-peeling algorithm by using atomic textures in
Implementing the basic algorithm using last array as a pivot in Java, is it
I am implementing a recursive algorithm: def induct(arg): if doStuff(arg) == 0: return #
I am implementing a display algorithm where we can have multiple layers of windows
I am implementing the complex algorithm which part is sorting array of ordered sequences
I am implementing a message passing algorithm. Messages pass between adjacent nodes when they
I am implementing a quicksort algorithm and have succesfully partitioned the input array around
I am implementing an algorithm to determine whether an undirected graph is bipartite or
I am working on implementing BFSK algorithm on a DSP processor and need to
I'm having some problems implementing an algorithm to read a foreign process' memory. Here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.