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

  • Home
  • SEARCH
  • 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 8563583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:59:33+00:00 2026-06-11T16:59:33+00:00

I need to come up with an algorithm that does the following: Lets say

  • 0

I need to come up with an algorithm that does the following:

Lets say you have an array of positive numbers (e.g. [1,3,7,0,0,9]) and you know beforehand their sum is 20.

You want to abstract some average amount from each number such that the new sum would be less by 7.

To do so, you must follow these rules:

  • you can only subtract integers
  • the resulting array must not have any negative values
  • you can not make any changes to the indices of the buckets.

The more uniformly the subtraction is distributed over the array the better.

Here is my attempt at an algorithm in JavaScript + underscore (which will probably make it n^2):

function distributeSubtraction(array, goal){
    var sum = _.reduce(arr, function(x, y) { return x + y; }, 0);
    if(goal < sum){
      while(goal < sum && goal > 0){
         var less = ~~(goal / _.filter(arr, _.identity).length); //length of array without 0s
         arr = _.map(arr, function(val){ 
            if(less > 0){
                return (less < val) ? val - less : val; //not ideal, im skipping some! 
            } else {
                if(goal > 0){ //again not ideal. giving preference to start of array
                    if(val > 0) {
                        goal--;
                        return val - 1;
                    }
                } else {
                    return val;
                }
            }
         });
         if(goal > 0){
             var newSum = _.reduce(arr, function(x, y) { return x + y; }, 0);
             goal -= sum - newSum;
             sum = newSum;
         } else {
            return arr;
         }
      }
    } else if(goal == sum) {
      return _.map(arr, function(){ return 0; });
    } else {
      return arr;
    }
}
var goal = 7;
var arr = [1,3,7,0,0,9];
var newArray = distributeSubtraction(arr, goal);
//returned: [0, 1, 5, 0, 0, 7];

Well, that works but there must be a better way! I imagine the run time of this thing will be terrible with bigger arrays and bigger numbers.

edit: I want to clarify that this question is purely academic. Think of it like an interview question where you whiteboard something and the interviewer asks you how your algorithm would behave on a different type of a dataset.

  • 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-11T16:59:34+00:00Added an answer on June 11, 2026 at 4:59 pm

    It sounds like you want to subtract a weighted amount from each number. I.E you want to subtract X/sum * amount_to_subtract from each item. You would of course need to round the amount your subtracting. The problem is then making sure that you’ve subtracted the total correct amount. Also, this depends on your input: are you guaranteeing that that the amount you want to subtract can be subtracted? Here’s a rough python implementation, (I think):

    def uniform_array_reduction(inp, amount):
      total = sum(inp)
      if amount > total:
        raise RuntimeError('Can\'t remove more than there is')
    
      if amount == total: #special case
        return [0] * len(inp)
    
      removed = 0
      output = []
      for i in inp:
        if removed < amount:
          to_remove = int(round(float(i)/float(total)*float(amount)))
          output.append(i - to_remove)
          removed += to_remove
        else:
          output.append(i)
      # if we didn't remove enough, just remove 1 from
      # each element until we've hit our mark.
      # shouldn't require more than one pass
      while removed < amount:
        for i in range(len(output)):
          if output[i] > 0:
            output[i] -= 1
            removed += 1
            if removed == amount:
              break
      return output
    

    EDIT: I’ve fixed a few bugs in the code.

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

Sidebar

Related Questions

The algorithm I need to come up with is one that computes all the
Hii , I have a small php form. I need it to come in
I have a coding/maths problem that I need help translating into C#. It's a
I need an algorithm that can give me positions around a sphere for N
I have a 15 digit customer number that I need to calculate a check
Has anyone come across a web-safe colour algorithm? Perhaps to explain why I need
A friend was in need of an algorithm that would let him loop through
I need to come up with a naming scheme for the situation where some
I need to come up with a smarter/simpler way of doing ajax calls in
Currently we are working on a project in which we need to come up

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.