Nitin Agrawal
Contact -
  • Home
  • Interviews
    • Secret Receipe
    • InterviewFacts
    • Resume Thoughts
    • Daily Coding Problems
    • BigShyft
    • CompanyInterviews >
      • InvestmentBanks >
        • ECS
        • Bank Of America
        • WesternUnion
        • WellsFargo
      • ProductBasedCompanies >
        • CA Technologies
        • Model N India
        • Verizon Media
        • Oracle & GoJek
        • IVY Computec
        • Nvidia
        • ClearWaterAnalytics
        • ADP
        • ServiceNow
        • Pubmatic
        • Expedia
        • Amphora
        • CDK Global
        • CDK Global
        • Epic
        • Sincro-Pune
        • Whiz.AI
        • ChargePoint
      • ServiceBasedCompanies >
        • Altimetrik
        • ASG World Wide Pvt Ltd
        • Paraxel International & Pramati Technologies Pvt Ltd
        • MitraTech
        • Intelizest Coding Round
        • EPAM
    • Interviews Theory
  • Programming Languages
    • Java Script >
      • Tutorials
      • Code Snippets
    • Reactive Programming >
      • Code Snippets
    • R
    • DataStructures >
      • LeetCode Problems
      • AnagramsSet
    • Core Java >
      • Codility
      • Program Arguments OR VM arguments & Environment variables
      • Java Releases
      • Threading >
        • ThreadsOrder
        • ProducerConsumer
        • Finalizer
        • RaceCondition
        • Executors
        • Future Or CompletableFuture
      • Important Points
      • Immutability
      • Dictionary
      • URL Validator
    • Julia
    • Python >
      • Decorators
      • String Formatting
      • Generators_Threads
      • JustLikeThat
    • Go >
      • Tutorial
      • CodeSnippet
      • Go Routine_Channel
      • Suggestions
    • Methodologies & Design Patterns >
      • Design Principles
      • Design Patterns >
        • TemplatePattern
        • Adapter Design Pattern
        • Decorator
        • Proxy
        • Lazy Initialization
        • CombinatorPattern
        • RequestChaining
        • Singleton >
          • Singletons
  • Frameworks
    • Apache Velocity
    • Spring >
      • Spring Boot >
        • CustomProperties
        • ExceptionHandling
        • Issues
      • Quick View
    • Rest WebServices >
      • Interviews
      • Swagger
    • Cloudera BigData >
      • Ques_Ans
      • Hive
      • Apache Spark >
        • ApacheSpark Installation
        • SparkCode
        • Sample1
        • DataFrames
        • RDDs
        • SparkStreaming
        • SparkFiles
    • Integration >
      • Apache Camel
    • Testing Frameworks >
      • JUnit >
        • JUnit Runners
      • EasyMock
      • Mockito >
        • Page 2
      • TestNG
    • Blockchain >
      • Ethereum Smart Contract
      • Blockchain Java Example
    • Microservices >
      • Messaging Formats
      • Design Patterns
    • AWS >
      • Honeycode
    • Dockers >
      • GitBash
      • Issues
  • Databases
    • MySql
    • Oracle >
      • Interview1
      • SQL Queries
    • Elastic Search
  • Random issues
    • TOAD issue
    • Architect's suggestions
  • Your Views

​CoinChanging problem

​One another interesting interview problem is to find the minimum number of coins required to get some amount.
You will be having a list of available denominations of coins with unlimited numbers.
Like coins denominations is - {1, 2, 5, 10} with each of unlimited counts and we have to find the minimum number of coins required to get 13.
Here answer will be 3 with 1 coin of 10, 1 of 2 & 1 of 1.
We can have limit on available number of coins for each denomination later.
Below code will give you the result like - {3={1=1, 2=1, 10=1}}

import java.util.Arrays;
import java.util.HashMap;

public class CoinChanging {

 public static void main(String[] args) {
  int[] coins = {1, 2, 5, 10};
  int total = 13;
  System.out.println(count(coins, total));
 }

 public static HashMap<Integer, HashMap<Integer, Integer>> count(int[] coins, int total) {

  Arrays.sort(coins);
  int[] totals = new int[total+1];
  int[] track = new int[total+1];

  int i = 0;
  for(int coin : coins) {
   int k = 1;
   while(k <= total) {
    if(k - coin == 0) {
     totals[k] = 1;
     track[k] = i;
    } else if(k-coin >= 0 && totals[k] > totals[k-coin] + 1 || totals[k] == 0){
     totals[k] = totals[k-coin] + 1;
     track[k] = i;
    }
    k++;
   }
   i++;
  }
  int sum = total;
  HashMap<Integer, Integer> counts = new HashMap<>();
  while(sum > 0) {
   int temp = sum - coins[track[sum]];
   if(counts.get(coins[track[sum]]) == null) {
    counts.put(coins[track[sum]], 1);
   } else {
    int cur = counts.get(coins[track[sum]]);
    counts.put(coins[track[sum]], cur+1);
   }
   sum = temp;
  }
  
  HashMap<Integer, HashMap<Integer, Integer>> result = new HashMap<>();
  result.put(totals[total], counts);
  return result;
 }
}
Powered by Create your own unique website with customizable templates.