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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:12:35+00:00 2026-05-19T14:12:35+00:00

I have some random integers like 99 20 30 1 100 400 5 10

  • 0

I have some random integers like

99 20 30 1 100 400 5 10

I have to find a sum from any combination of these integers that is closest(equal or more but not less) to a given number like

183

what is the fastest and accurate way of doing this?

  • 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-19T14:12:35+00:00Added an answer on May 19, 2026 at 2:12 pm

    If your numbers are small, you can use a simple Dynamic Programming(DP) technique. Don’t let this name scare you. The technique is fairly understandable. Basically you break the larger problem into subproblems.

    Here we define the problem to be can[number]. If the number can be constructed from the integers in your file, then can[number] is true, otherwise it is false. It is obvious that 0 is constructable by not using any numbers at all, so can[0] is true. Now you try to use every number from the input file. We try to see if the sum j is achievable. If an already achieved sum + current number we try == j, then j is clearly achievable. If you want to keep track of what numbers made a particular sum, use an additional prev array, which stores the last used number to make the sum. See the code below for an implementation of this idea:

    int UPPER_BOUND = number1 + number2 + ... + numbern //The largest number you can construct
    bool can[UPPER_BOUND + 1]; //can[number] is true if number can be constructed
    can[0] = true; //0 is achievable always by not using any number
    
    int prev[UPPER_BOUND + 1]; //prev[number] is the last number used to achieve sum "number"
    for (int i = 0; i < N; i++) //Try to use every number(numbers[i]) from the input file
    {
       for (int j = UPPER_BOUND; j >= 1; j--) //Try to see if j is an achievable sum
       {
          if (can[j]) continue; //It is already an achieved sum, so go to the next j
    
          if (j - numbers[i] >= 0 && can[j - numbers[i]]) //If an (already achievable sum) + (numbers[i]) == j, then j is obviously achievable
          {
              can[j] = true;
              prev[j] = numbers[i]; //To achieve j we used numbers[i]
          } 
       }
    }
    
    int CLOSEST_SUM = -1;
    for (int i = SUM; i <= UPPER_BOUND; i++)
       if (can[i])
       {
           //the closest number to SUM(larger than SUM) is i
           CLOSEST_SUM = i;
           break; 
       }
    
    int currentSum = CLOSEST_SUM;    
    do
    {
        int usedNumber = prev[currentSum];
        Console.WriteLine(usedNumber);
    
        currentSum -= usedNumber;
    } while (currentSum > 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have some function that receives N random 2D points. Is there any
So, I have a string array of some random saying and the like, but
Just random pick some methods that I have found: sforce.connection.describeSObject sforce.SearchResult sforce.sObject sforce.LeadConvert() sforce.connection.create()
I have discovered recently that's possible to call anoymous blocks from jdbc like this:
I have some random string, let's say : s = This string has some
I have the following string: const char *str = \This is just some random
okay, so i have a text file with example content below line1with some random
I have some files that are uuencoded, and I need to decode them, using
I have some script in my default page that redirects users to language specific
I would like an algorithm for a function that takes n integers and returns

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.