Created
September 17, 2024 14:29
-
-
Save psahni/3cb4f0b09bad44bfeb7476d58d7b32bf to your computer and use it in GitHub Desktop.
denominations.go
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
package main | |
import ( | |
"fmt" | |
"sort" | |
) | |
var notes = []int{1000, 500, 100} | |
var notesCount = map[int]int{ | |
1000: 2, | |
500: 4, | |
100: 10, | |
} | |
type Denominations struct { | |
Value int | |
Count int | |
} | |
func main() { | |
output := countDenominations2(5000) | |
fmt.Println(output) | |
} | |
func countDenominations2(amount int) []Denominations { | |
var notesArr []int | |
var denominations []Denominations | |
var totalBalance int | |
for note, value := range notesCount { | |
notesArr = append(notesArr, note) | |
totalBalance += note * value | |
} | |
if amount > totalBalance { | |
fmt.Println("[countDenominations2] Amount is not available") | |
return []Denominations{Denominations{Value: 0, Count: 0}} | |
} | |
sort.Sort(sort.Reverse(sort.IntSlice(notesArr))) | |
tempAmt := amount | |
for i := 0; i < len(notesArr); i++ { | |
note := notes[i] | |
count := (tempAmt / note) | |
rem := tempAmt % note | |
if notesCount[note] >= count { | |
tempAmt = rem | |
denominations = append(denominations, Denominations{Value: note, Count: count}) | |
} else { | |
denominations = append(denominations, Denominations{Value: note, Count: notesCount[note]}) | |
count = count - notesCount[note] | |
tempAmt = (count * note) + rem | |
} | |
} | |
if tempAmt > 0 { | |
fmt.Println("[countDenominations2] Can't be dispersed") | |
return []Denominations{Denominations{Value: 0, Count: 0}} | |
} | |
return denominations | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment