Coin Change | BFS Approach
Last Updated :
31 May, 2022
Given an integer X and an array arr[] of length N consisting of positive integers, the task is to pick minimum number of integers from the array such that they sum up to N. Any number can be chosen infinite number of times. If no answer exists then print -1.
Examples:
Input: X = 7, arr[] = {3, 5, 4}
Output: 2
The minimum number elements will be 2 as
3 and 4 can be selected to reach 7.
Input: X = 4, arr[] = {5}
Output: -1
Approach: We have already seen how to solve this problem using dynamic-programming approach in this article.
Here, we will see a slightly different approach to solve this problem using BFS.
Before that, let’s go ahead and define a state. A state SX can be defined as the minimum number of integers we would need to take from array to get a total of X.
Now, if we start looking at each state as a node in a graph such that each node is connected to (SX – arr[0], SX – arr[1], … SX – arr[N – 1]).
Thus, we have to find the shortest path from state N to 0 in an unweighted and this can be done using BFS. BFS works here because the graph is unweighted.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minNumbers( int x, int * arr, int n)
{
queue< int > q;
q.push(x);
unordered_set< int > v;
int d = 0;
while (q.size()) {
int s = q.size();
while (s--) {
int c = q.front();
if (!c)
return d;
q.pop();
if (v.find(c) != v.end() or c < 0)
continue ;
v.insert(c);
for ( int i = 0; i < n; i++)
q.push(c - arr[i]);
}
d++;
}
return -1;
}
int main()
{
int arr[] = { 3, 3, 4 };
int n = sizeof (arr) / sizeof ( int );
int x = 7;
cout << minNumbers(x, arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int minNumbers( int x, int []arr, int n)
{
Queue<Integer> q = new LinkedList<>();
q.add(x);
HashSet<Integer> v = new HashSet<Integer>();
int d = 0 ;
while (q.size() > 0 )
{
int s = q.size();
while (s-- > 0 )
{
int c = q.peek();
if (c == 0 )
return d;
q.remove();
if (v.contains(c) || c < 0 )
continue ;
v.add(c);
for ( int i = 0 ; i < n; i++)
q.add(c - arr[i]);
}
d++;
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 3 , 3 , 4 };
int n = arr.length;
int x = 7 ;
System.out.println(minNumbers(x, arr, n));
}
}
|
Python3
def minNumbers(x, arr, n) :
q = []
q.append(x)
v = set ([])
d = 0
while ( len (q) > 0 ) :
s = len (q)
while (s) :
s - = 1
c = q[ 0 ]
if (c = = 0 ) :
return d
q.pop( 0 )
if ((c in v) or c < 0 ) :
continue
v.add(c)
for i in range (n) :
q.append(c - arr[i])
d + = 1
return - 1
arr = [ 1 , 4 , 6 ]
n = len (arr)
x = 20
print (minNumbers(x, arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int minNumbers( int x, int []arr, int n)
{
Queue< int > q = new Queue< int >();
q.Enqueue(x);
HashSet< int > v = new HashSet< int >();
int d = 0;
while (q.Count > 0)
{
int s = q.Count;
while (s-- > 0)
{
int c = q.Peek();
if (c == 0)
return d;
q.Dequeue();
if (v.Contains(c) || c < 0)
continue ;
v.Add(c);
for ( int i = 0; i < n; i++)
q.Enqueue(c - arr[i]);
}
d++;
}
return -1;
}
public static void Main(String[] args)
{
int []arr = { 3, 3, 4 };
int n = arr.Length;
int x = 7;
Console.WriteLine(minNumbers(x, arr, n));
}
}
|
Javascript
<script>
function minNumbers(x, arr, n)
{
var q = [];
q.push(x);
var v = new Set();
var d = 0;
while (q.length!=0) {
var s = q.length;
while (s--) {
var c = q[0];
if (!c)
return d;
q.shift();
if (v.has(c) || c < 0)
continue ;
v.add(c);
for ( var i = 0; i < n; i++)
q.push(c - arr[i]);
}
d++;
}
return -1;
}
var arr = [3, 3, 4];
var n = arr.length;
var x = 7;
document.write(minNumbers(x, arr, n));
</script>
|
Time complexity: O(N * X)
Auxiliary Space: O(N), since N extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...