Skip to content
Related Articles

Related Articles

Minimize coins required to obtain all possible values up to N
  • Difficulty Level : Hard
  • Last Updated : 03 May, 2021

Given an integer N, the task is to find the minimum count of {1, 2, 5}-valued coins such that changes of all possible values in the range [1, N] can be formed and it is not possible to obtain values N.

Examples:

Input: N = 8
Output: 
Count of 5 values coins: 0
Count of 2 rupees coins: 3
Count of 1 rupees coins: 2
Explanation: 
Coins required for 1 cent = 1 
Coins required for 2 cent = 1 
Coins required for 3 cent = 2 
Coins required for 4 cent = 2 
Coins required for 5 cent = 3 
Coins required for 6 cent = 3 
Coins required for 7 cent = 4 
Coins required for 8 cent = 5

Input: N = 17
Output: 
Count of 5 rupees coins: 2
Count of 2 rupees coins: 3
Count of 1 rupees coins: 1

Approach: The problem can be solved using the greedy technique. The idea is based on the following observations:



Any number, X from the range [1, N] can be represented as 
X = 5 * (Integer) + Y * (Integer) 
Y is one of the values from the range [0, 4] 
 

Follow the steps below to solve the problem:

  • Initialize three variables, say F, T, and O, to store the count of 5 , 2 and 1-valued coins.
  • Calculate count of 5-valued coins using F = (N – 4)/5.
  • If (N – 5 * F) is even, then count of one valued coins can be calculated as O = 1.
  • Otherwise, count of one valued coins can be calculated as O = 2.
  • Calculate count of two valued coins can be calculated as T = (N – 5 * F – O) / 2.
  • Finally, print values of F, T, and O.

Below is the implementation of the above approach:,

C++




#include <bits/stdc++.h>
using namespace std;
 
// Funtion to find minimum count of {1, 2, 5}
// valued coins required to make a change of
// all values in the range [1, N]
void find(int N)
{
  int T, F, O;
 
  // Number of 5 valueds coins required
  F = int((N - 4) / 5);
 
  // Number of 1 valued coins required
  if (((N - 5 * F) % 2) == 0)
  {
    O = 2;
  }
 
  else
  {
    O = 1 ;
  }
 
  // Number of 2 valued coins required
  T = floor((N - 5 * F - O)/2);
 
  cout<< "Count of 5 valueds coins: " << F << endl;
  cout<< "Count of 2 valueds coins: " << T<< endl;
  cout<< "Count of 1 valueds coins: " << O << endl;
}
 
// Driver Code
int main()
{
  int N = 8;
  find(N);
  return 0;
}
 
// This code is contributed by Jana_sayantan.

Java




// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
// Funtion to find minimum count of {1, 2, 5}
// valued coins required to make a change of
// all values in the range [1, N]
static void find(int N)
{
  int T, F, O;
 
  // Number of 5 valueds coins required
  F = (int)((N - 4) / 5);
 
  // Number of 1 valued coins required
  if (((N - 5 * F) % 2) == 0)
  {
    O = 2;
  }
 
  else
  {
    O = 1 ;
  }
 
  // Number of 2 valued coins required
  T = (int)Math.floor((N - 5 * F - O)/2);
 
   System.out.println("Count of 5 valueds coins: " + F);
   System.out.println("Count of 2 valueds coins: " + T);
   System.out.println("Count of 1 valueds coins: " + O);
}
 
// Driver Code
public static void main(String args[])
{
    int N = 8;
    find(N);
}
}
 
// This code is contributed by splevel62.

Python3




# Python Program for the above approach
 
# Funtion to find minimum count of {1, 2, 5}
# valued coins required to make a change of
# all values in the range [1, N]
def find(N):
     
    # Number of 5 valueds coins required
    F = int((N - 4) / 5)
 
    # Number of 1 valued coins required
    if ((N - 5 * F) % 2) == 0:
        O = 2
 
    else:
        O = 1
 
    # Number of 2 valued coins required
    T = (N - 5 * F - O)//2
 
    print("Count of 5 valueds coins: ", F)
    print("Count of 2 valueds coins: ", T)
    print("Count of 1 valueds coins: ", O)
 
if __name__ == '__main__':
     
    N = 8
    find(N)

C#




// C# program to implement
// the above approach
using System;
public class GFG
{
 
// Funtion to find minimum count of {1, 2, 5}
// valued coins required to make a change of
// all values in the range [1, N]
static void find(int N)
{
  int T, F, O;
 
  // Number of 5 valueds coins required
  F = (int)((N - 4) / 5);
 
  // Number of 1 valued coins required
  if (((N - 5 * F) % 2) == 0)
  {
    O = 2;
  }
 
  else
  {
    O = 1 ;
  }
 
  // Number of 2 valued coins required
  T = (int)Math.Floor((double)(N - 5 * F - O)/2);
 
   Console.WriteLine("Count of 5 valueds coins: " + F);
   Console.WriteLine("Count of 2 valueds coins: " + T);
   Console.WriteLine("Count of 1 valueds coins: " + O);
}
 
// Driver Code
public static void Main(String []args)
{
    int N = 8;
    find(N);
}
}
 
// This code is contributed by 29AjayKumar

Javascript




<script>
// Javascript program to implement
// the above approach
 
 
// Funtion to find minimum count of {1, 2, 5}
// valued coins required to make a change of
// all values in the range [1, N]
function find(N)
{
  var T, F, O;
 
  // Number of 5 valueds coins required
  F = parseInt((N - 4) / 5);
 
  // Number of 1 valued coins required
  if (((N - 5 * F) % 2) == 0)
  {
    O = 2;
  }
 
  else
  {
    O = 1 ;
  }
 
  // Number of 2 valued coins required
  T = Math.floor((N - 5 * F - O)/2);
 
  document.write( "Count of 5 valueds coins: " + F + "<br>");
  document.write( "Count of 2 valueds coins: " + T + "<br>");
  document.write( "Count of 1 valueds coins: " + O + "<br>");
}
 
var N = 8;
find(N);
 
// This code is contributed by SoumikMondal.
</script>
Output: 
Count of 5 valueds coins:  0
Count of 2 valueds coins:  3
Count of 1 valueds coins:  2

 

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :