
I need to create a function / algorithm for my program
- or -
Post a project like this£100(approx. $135)
- Posted:
- Proposals: 7
- Remote
- #3335155
- Expired
TOP PRO - Excel, Word, PowerPoint, VBA, Google Sheet, Outlook, Access, Database, Scripting

126446352685321300971609896415198156324756243413




Description
Experience Level: Entry
Estimated project duration: 1 day or less
I am making a smart contract to deposit funds into different accounts.
When a user withdraws funds, instead of withdrawing from one account, we need to calculate which accounts hold more value that they should and which are under-funded and withdraw from the accounts in the best order and quantity so that they are as balanced as possible
The arguments I provide to the function are:
- Array1: an integer array of the current funds for each account. Example: [100000, 2000, 1000000]
- Array2: an array of integers denoting how much each account is over-funded or underfunded. Example: [0, -4000, 100000]
- withdrawValue: an integer of how much the user needs to withdraw from Array1. Example: 10000
The return value of f(a1, a2, x) should be an array of how much to withdraw from each account. Example: [100, 0, 0]
Thus, f(a1, a2, x) + Array1 should be similar in distribution to Array2 + Array1
Rules it needs to follow:
- Objects, Maps or dictionaries are not allowed. This needs to be implemented on solidity so the only types you can uses are int, arrays and bytes.
- Loops and recursion should be minimised or not used
- You cannot add to elements of the array, the only operation allowed to balance the array is subtraction
Explanation: the operation is done by a user withdrawing, so no value can change from one account to another. Funds can only be withdrawn
- Sum of the elements of f(a1, a2, x) should equal: sum(Array1) - withdrawValue
- All of the elements of Array1 - f(a1, a2, x) should be >=0.
Explanation: When we perform the withdraw of f(a1, a2, x) from the accounts array (Array1) no account can go bellow 0 value
- Array1 - f(a1, a2, x) should be balanced relative to the value of Array1 + Array2.
Examples:
Array1: [1001000,1000200,1000030,1000000,1000000,1000000,999600,998000 ]
Array2: [ 1000,200,30,0,0,0,-400,-2000]
Test#a1
withdrawValue: 100
result: [100,0,0,0,0,0,0,0]
Explanation: element[0] is 1,000 over-funded so we can cover a 100 withdraw with just that element.
A solution where it withdraws from element[0], element[1] and element[2] would also be acceptable
Test#a1
withdrawValue: 1500
Explanation: this result would need to be computed and the result should withdraw from element[0], element[1], element[2] and possibly element[3], element[4] and element[5].
-----------------------------------------------
Array1: [1000,500, 0]
Array2: [-2000, -1000, 0]
Test#b1
withdrawValue: 100
result: something like [50, 50, 0]
Explanation: Even if the value of Array1 + Array2 is negative, a user should be able to withdraw.
Test#b1
withdrawValue: 1500
result: [1000,500, 0]
Explanation: you should be able to withdraw completely
Test#b1
withdrawValue: 1600
result: Error
Explanation: error should be thrown if withdrawing more than the full amount.
When a user withdraws funds, instead of withdrawing from one account, we need to calculate which accounts hold more value that they should and which are under-funded and withdraw from the accounts in the best order and quantity so that they are as balanced as possible
The arguments I provide to the function are:
- Array1: an integer array of the current funds for each account. Example: [100000, 2000, 1000000]
- Array2: an array of integers denoting how much each account is over-funded or underfunded. Example: [0, -4000, 100000]
- withdrawValue: an integer of how much the user needs to withdraw from Array1. Example: 10000
The return value of f(a1, a2, x) should be an array of how much to withdraw from each account. Example: [100, 0, 0]
Thus, f(a1, a2, x) + Array1 should be similar in distribution to Array2 + Array1
Rules it needs to follow:
- Objects, Maps or dictionaries are not allowed. This needs to be implemented on solidity so the only types you can uses are int, arrays and bytes.
- Loops and recursion should be minimised or not used
- You cannot add to elements of the array, the only operation allowed to balance the array is subtraction
Explanation: the operation is done by a user withdrawing, so no value can change from one account to another. Funds can only be withdrawn
- Sum of the elements of f(a1, a2, x) should equal: sum(Array1) - withdrawValue
- All of the elements of Array1 - f(a1, a2, x) should be >=0.
Explanation: When we perform the withdraw of f(a1, a2, x) from the accounts array (Array1) no account can go bellow 0 value
- Array1 - f(a1, a2, x) should be balanced relative to the value of Array1 + Array2.
Examples:
Array1: [1001000,1000200,1000030,1000000,1000000,1000000,999600,998000 ]
Array2: [ 1000,200,30,0,0,0,-400,-2000]
Test#a1
withdrawValue: 100
result: [100,0,0,0,0,0,0,0]
Explanation: element[0] is 1,000 over-funded so we can cover a 100 withdraw with just that element.
A solution where it withdraws from element[0], element[1] and element[2] would also be acceptable
Test#a1
withdrawValue: 1500
Explanation: this result would need to be computed and the result should withdraw from element[0], element[1], element[2] and possibly element[3], element[4] and element[5].
-----------------------------------------------
Array1: [1000,500, 0]
Array2: [-2000, -1000, 0]
Test#b1
withdrawValue: 100
result: something like [50, 50, 0]
Explanation: Even if the value of Array1 + Array2 is negative, a user should be able to withdraw.
Test#b1
withdrawValue: 1500
result: [1000,500, 0]
Explanation: you should be able to withdraw completely
Test#b1
withdrawValue: 1600
result: Error
Explanation: error should be thrown if withdrawing more than the full amount.

Alejandro P.
0% (0)Projects Completed
-
Freelancers worked with
-
Projects awarded
0%
Last project
1 Oct 2025
United Kingdom
New Proposal
Login to your account and send a proposal now to get this project.
Log inClarification Board Ask a Question
-
Which programming language do you want? C/C++, Java, Python or etc.
I'm hearing your response.
990289
We collect cookies to enable the proper functioning and security of our website, and to enhance your experience. By clicking on 'Accept All Cookies', you consent to the use of these cookies. You can change your 'Cookies Settings' at any time. For more information, please read ourCookie Policy
Cookie Settings
Accept All Cookies