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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:47:50+00:00 2026-05-24T18:47:50+00:00

given an array of numbers, what’s a good way to adjust one number and

  • 0

given an array of numbers, what’s a good way to adjust one number and amortize the difference proportionally over certain of the other items in the array?

given:

var array = [10,20,30,40,50]

-> adjust the third item up by 30 and subtract proportionally across the first and last such that the sum of the items still totals 150 (second and fourth elements in the array are fixed or should remain the same).

= should come out to : [5,20,60,40,25]

(ideally javascript, but I could translate from C# / VB.net if that’s preferable)

  • 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-24T18:47:52+00:00Added an answer on May 24, 2026 at 6:47 pm

    Here’s a general purpose JS function to split the adjustment evenly across all the other items in the array:

    var array = [10,20,30,40,50];
    
    function amortize(list, index, amount) {
        if (list.length <= 1) return;    // can't amortize with 0 or 1 items in list
        var remaining = amount / (list.length - 1);
        list[index] += amount;
        for (var i = 0; i < list.length; i++) {
            if (i != index) {
                list[i] -= remaining;
            }
        }
    }
    

    If you really want a non-general function that just adjusts the ends and leaves the ones on either side of the middle untouched, then you would just do this:

    var array = [10,20,30,40,50];
    
    function amortizeEnds(list, amount) {
        var remaining = amount / 2;
        list[0] -= remaining;
        list[4] -= remaining;
        list[2] += amount;
    }
    

    I’m not sure I understand your proportional request, but here’s one that gives you the result you asked for:

    var array = [10,20,30,40,50];
    
    function amortizeEndsProportional(list, amount) {
        var endsTotal = list[0] + list[4];
        var reductionProportion = amount / endsTotal;
        list[0] = list[0] - list[0] * reductionProportion;
        list[4] -= list[4] - list[4] * reductionProportion;
        list[2] += amount;
    }
    

    You can see the last one work here: http://jsfiddle.net/jfriend00/QayQp/.

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

Sidebar

Related Questions

Given an array that has the numbers {0......2^k -1} except for one number ,
Possible Duplicate: Finding a single number in a list Given an array of numbers,
This is an interview question . Given an array of numbers and another number,
Given is a array of numbers: 1, 2, 8, 6, 9, 0, 4 We
Given an array of real-valued numbers, A[1..n,1..n] , I wish to find the sub-array
Given an unsorted integer array, and without making any assumptions on the numbers in
You are given as input an unsorted array of n distinct numbers, where n
Possible Duplicate: Interview Q: given an array of numbers, return array of products of
Given an array such as [10,42,45,45,61,61,75,90,1240], how do I find what numbers X is
I am given an array of real numbers, A. It has n+1 elements. It

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.