Last active
August 11, 2021 14:46
-
-
Save roshanadh/53f3447f96382e802e259217941a2cbd to your computer and use it in GitHub Desktop.
Find the distinct denominations required for a transaction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Find the distinct denominations required for a transaction | |
*/ | |
// list of denominations | |
const denoms = [1000, 500, 100, 50, 20, 10, 5, 2, 1]; | |
// return array of denoms smaller than or equal to the passed remainder | |
const getValidDenoms = (remainder) => { | |
return denoms.filter((el) => { | |
return el <= remainder; | |
}); | |
}; | |
let cost = 253; | |
let paid = 1000; | |
let remainder = paid - cost; | |
// number of different denominations to be returned | |
let numOfDenoms = 0; | |
// list of different denominations to be returned | |
let returnedDenoms = []; | |
let i = 0; | |
while (1) { | |
let validDenoms = getValidDenoms(remainder); | |
remainder = remainder % validDenoms[i]; | |
returnedDenoms.push(validDenoms[i]); | |
numOfDenoms++; | |
if (remainder == 0) break; | |
} | |
console.log(`Number of denominations used: ${numOfDenoms}`); | |
console.log(`Denominations used: ${returnedDenoms}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment