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 9205699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:00:45+00:00 2026-06-18T00:00:45+00:00

Given an array say nums = { 1,2,5,3,6,-1,-2,10,11,12}, using max no of elements (say

  • 0

Given an array say nums = { 1,2,5,3,6,-1,-2,10,11,12}, using max no of elements (say maxNums=3)
find the elements whose sum (say sum =10) = K

so if maxNums to be used = 3
sum to find = 10
the the answer is

    {1  3  6}    
    {1  -1  10}
    {1  -2  11}
    {2  5  3}
    {2  -2  10}
    {5 6 -1}
    {-1  11}
    {-2  12}
    {10}

I wrote a recursive function which does the job. How do I do it without recursion?
and/or with less memory ?

class Program
{
        static Int32[] nums = { 1,2,5,3,6,-1,-2,10,11,12};
        static Int32 sum = 10;
        static Int32 maxNums = 3;


        static void Main(string[] args)
        {

            Int32[] arr = new Int32[nums.Length];
            CurrentSum(0, 0, 0, arr);

            Console.ReadLine();
        }

        public static void Print(Int32[] arr)
        {
            for (Int32 i = 0; i < arr.Length; i++)
            {
                if (arr[i] != 0)
                    Console.Write("  "   +arr[i]);
            }
            Console.WriteLine();
        }


        public static void CurrentSum(Int32 sumSoFar, Int32 numsUsed, Int32 startIndex, Int32[] selectedNums)
        {
            if ( startIndex >= nums.Length  || numsUsed > maxNums)
            {
                if (sumSoFar == sum && numsUsed <= maxNums)
                {
                    Print(selectedNums);
                }                    
                return;
            }

                       **//Include the next number and check the sum**
                    selectedNums[startIndex] = nums[startIndex];
                    CurrentSum(sumSoFar + nums[startIndex], numsUsed+1, startIndex+1, selectedNums);

                    **//Dont include the next number**
                    selectedNums[startIndex] = 0;
                    CurrentSum(sumSoFar , numsUsed , startIndex + 1, selectedNums);
        }
    }
  • 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-18T00:00:46+00:00Added an answer on June 18, 2026 at 12:00 am

    You function looks fine but possible a bit optimize:

    class Program
    {
        static Int32[] nums = { 1, 2, 5, 3, 6, -1, -2, 10, 11, 12 };
        static Int32 sum = 10;
        static Int32 maxNums = 3;
        static Int32[] selectedNums = new Int32[maxNums];
    
        static void Main(string[] args)
        {
            CurrentSum(0, 0, 0);
            Console.ReadLine();
        }
    
        public static void Print(int count)
        {
            for (Int32 i = 0; i < count; i++)
            {
                Console.Write(" " + selectedNums[i]);
            }
            Console.WriteLine();
        }
    
        public static void CurrentSum(Int32 sumSoFar, Int32 numsUsed, Int32 startIndex)
        {
            if (sumSoFar == sum && numsUsed <= maxNums)
            {
                Print(numsUsed);
            }
    
            if (numsUsed >= maxNums || startIndex >= nums.Length)
                return;
    
            for (int i = startIndex; i < nums.Length; i++)
            {
                // Include i'th number
                selectedNums[numsUsed] = nums[i];
                CurrentSum(sumSoFar + nums[i], numsUsed + 1, i + 1);
            }
        }
    }
    

    Also I fixed a bug in your function.
    It fails on following testcase:

    {10, 2, -2}
    Sum = 10
    K = 3
    

    Your functions returns only {10} instead of {10} and {10, 2, -2}

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an array. How can we find sum of elements in index interval (i,
So let's say I have this array: $requiredFruit= @(apple,pear,nectarine,grape) And I'm given a second
I have the array variable say $value, and it has the below given array
Given an array of integers, what is the maximum sum of a subset of
Suppose you pass an array as argument to a given method, say, public static
I have an interesting problem. I have an 2D array of say n elements.
Given a 1*N matrix or an array, how do I find the first 4
There is an array which can contain, say, upto 1000 elements. The range of
I have an array (of 9 elements, say) which I must treat as a
Given a large collection (let's call it 'a') of elements of type T (say,

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.