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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:18:36+00:00 2026-05-25T10:18:36+00:00

Given an array (e.g. [1,2]) of n elements and a number ‘k’ (e.g. 6),

  • 0

Given an array (e.g. [1,2]) of n elements and a number ‘k’ (e.g. 6), find all possible ways to produce the sum = k

For given example answer would be 4 because

1 1 1 1 1 1
1 1 1 1 2
1 1 2 2
2 2 2

The algorithm I could think of is by brute force, we simulate all possible scenarios, and stop when from given state we can not reach result.

 arr[] = [1,2]
    k = 6
   globalCount =0;
   function findSum(arr,k)
   {
      if(k ==0)
         globalCount++
         return
      else if(k<0)
         return

      for each i in arr{
       arr.erase(i)
       tmp = k
       findSum(arr,tmp)
       while(k>=0){
          findSum(arr,tmp -= i)
      } 
   }

I am not sure if my solution is most efficient one out there. Please comment /correct or show pointers to better solutions.

EDIT : Would really appreciate if someone can give me runtime complexity of my code and their soln code. 🙂
Mine code complexity I think is Big-O( n^w ) w = k/avg(arr[0]..arr[n-1])

  • 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-05-25T10:18:37+00:00Added an answer on May 25, 2026 at 10:18 am

    If you’re willing to excuse the fancy linq tricks, you might find this C# solution useful. Fortunately linq reads kind of like english. The idea is to build up the solutions as k starts from 0 and increments until it reaches its correct value. Each value of k builds on the previous solutions. One thing you have to watch for though is to ensure that the new “ways” you’re finding are not re-orderings of others. I solved that by only counting them as valid if they’re sorted. (which was only a single comparison)

    void Main() {
        foreach (int[] way in GetSumWays(new[] {1, 2}, 6)) {
            Console.WriteLine (string.Join(" ", way));
        }
    }
    
    int[][] GetSumWays(int[] array, int k) {
        int[][][] ways = new int[k + 1][][];
        ways[0] = new[] { new int[0] };
    
        for (int i = 1; i <= k; i++) {
            ways[i] = (
                from val in array
                where i - val >= 0
                from subway in ways[i - val]
                where subway.Length == 0 || subway[0] >= val
                select Enumerable.Repeat(val, 1)
                    .Concat(subway)
                    .ToArray()
            ).ToArray();
        }
    
        return ways[k];
    }
    

    Output:

    1 1 1 1 1 1
    1 1 1 1 2
    1 1 2 2
    2 2 2
    

    It uses a dynamic programming approach and should be faster than a naive recursive approach. I think. I know it’s fast enough to count the number of ways to break a dollar in a few milliseconds. (242)

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

Sidebar

Related Questions

Given a generic array T[] , where T extends java.lang.Number , I would like
I have a string. I need to replace all instances of a given array
Given x number of arrays, each with a possibly different number of elements, how
Given an array of n Objects, let's say it is an array of strings
Given an array of characters which forms a sentence of words, give an efficient
Given an array of integers, what is the simplest way to iterate over it
Given an array like {one two, three four five}, how'd you calculate the total
Given an array of items, each of which has a value and cost ,
How do you do it? Given a byte array: byte[] foo = new byte[4096];
Given a date/time as an array of (year, month, day, hour, minute, second), how

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.