Skip to content

Commit

Permalink
feat ProgramOptimized.c
Browse files Browse the repository at this point in the history
  • Loading branch information
AE-Hertz authored Nov 7, 2024
1 parent ce7bb15 commit 36fb6bd
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Greedy Algorithms/Indian denomination problem/ProgramOptimized.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main() {
// Array of currency notes
int arr[] = {1, 2, 5, 10, 20, 50, 100, 200, 500, 2000};
int n = sizeof(arr) / sizeof(arr[0]); // Number of currency notes

// Amount to be made
int amt;
printf("Enter amount: ");
scanf("%d", &amt);

// dp[i] will store the minimum number of notes required to make amount i
int dp[amt + 1];
int note[amt + 1]; // To store the last note used for each amount

// Initialize dp array with a large value (INT_MAX)
for (int i = 0; i <= amt; i++) {
dp[i] = INT_MAX;
}
dp[0] = 0; // Base case: 0 amount requires 0 notes

// Dynamic Programming to find minimum number of notes for each amount
for (int i = 1; i <= amt; i++) {
for (int j = 0; j < n; j++) {
if (arr[j] <= i && dp[i - arr[j]] != INT_MAX) {
if (dp[i] > dp[i - arr[j]] + 1) {
dp[i] = dp[i - arr[j]] + 1;
note[i] = arr[j]; // Store the note used
}
}
}
}

if (dp[amt] == INT_MAX) {
printf("No solution\n");
} else {
printf("Minimum number of notes: %d\n", dp[amt]);

// To print the notes used
printf("Notes used: ");
while (amt > 0) {
printf("%d ", note[amt]);
amt -= note[amt]; // Reduce the amount by the note used
}
printf("\n");
}

return 0;
}

0 comments on commit 36fb6bd

Please sign in to comment.