
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Distributing Bananas Problem in JavaScript
Problem
Suppose there are n people standing in a queue, we wish to distribute bananas to the people in the following way −
We give 1 banana to the first person, 2 bananas to the second person, and so on until we give n bananas to the last person.
Then, we go back to the start of the row, giving n + 1 bananas to the first person, n + 2 bananas to the second person, and so on until we give 2 * n bananas to the last person.
This process repeats (with us giving one more banana each time, and moving to the start of the row after we reach the end) until we run out of candies. The last person will receive all of our remaining bananas.
We are required to write a JavaScript function that takes in the number of people, num, as the first argument, and the number of bananas, m, as the second argument.
Our function should return an array (of length num sum m) that represents the final distribution of bananas.
For example, if the input to the function is −
const num = 3; const m = 10;
Then the output should be −
const output = [5, 2, 3];
Output Explanation:
On the first turn, res[0] += 1, and the array is [1,0,0].
On the second turn, res[1] += 2, and the array is [1,2,0].
On the third turn, res[2] += 3, and the array is [1,2,3].
On the fourth turn, res[0] += 4, and the final array is [5,2,3].
Example
The code for this will be −
const num = 3; const m = 10; const distributeBananas = (num = 1, m = 1) => { const res = new Array(num).fill(0); let curr = 1; while(true){ for(let i = 0; i < num; i++){ if(m < curr){ res[i] += m return res }; res[i] += curr; m -= curr; curr++; }; }; }; console.log(distributeBananas(num, m));
Output
And the output in the console will be −
[5, 2, 3]
- Related Articles
- Snail Trail Problem in JavaScript
- Recursive Staircase problem in JavaScript
- Recursion problem Snail Trail in JavaScript
- 2 Key keyboard problem in JavaScript
- Meeting Rooms 2 problem in JavaScript
- Expressive words problem case in JavaScript
- Crack Alphabets fight problem in JavaScript
- Distributing all balls without repetition in C++ Program
- Koko Eating Bananas in C++
- The algorithm problem - Backtracing pattern in JavaScript
- Combination sum problem using JavaScript
- Two sum problem in linear time in JavaScript
- Solution to the clumsy factorial problem in JavaScript
- Solve the Sherlock and Array problem in JavaScript
- Parse and balance angle brackets problem in JavaScript
