Skip to content
Related Articles

Related Articles

Improve Article

Minimum number of coins having value equal to powers of 2 required to obtain N

  • Last Updated : 29 Apr, 2021

Given an integer N, the task is to find the minimum number of coins of the form 2i required to make a change for N cents.

Examples:

Input: N = 5 
Output:
Explanation: 
Possible values of coins are: {1, 2, 4, 8, …} 
Possible ways to make change for N cents are as follows: 
5 = 1 + 1 + 1 + 1 + 1 
5 = 1 + 2 + 2 
5 = 1 + 4 (Minimum) 
Therefore, the required output is 2

Input: N = 4 
Output: 4

Naive Approach: The simplest approach to solve this problem is to store all possible values of the coins in an array and print the minimum count of coins required to make a change for N cents using Dynamic programming
Time Complexity: O(N2) 
Auxiliary Space: O(N)

Efficient Approach: The above approach can be optimized using the fact that any number can be represented in the form of a power of 2s. The idea is to count the set bits of N and print the count obtained. Follow the steps below to solve the problem:



  • Iterate over the bits in the binary representation of N and check if the current bit is set or not. If found to be true, then increment the count.
  • Finally, print the total count obtained.

Below is the implementation of the above approach:

C++




// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to count of set bit in N
void count_setbit(int N)
{
 
    // Stores count of set bit in N
    int result = 0;
 
    // Iterate over the range [0, 31]
    for (int i = 0; i < 32; i++) {
 
        // If current bit is set
        if ((1 << i) & N) {
 
            // Update result
            result++;
        }
    }
    cout << result << endl;
}
 
// Driver Code
int main()
{
    int N = 43;
 
    count_setbit(N);
    return 0;
}

C




// C program for above approach
#include <stdio.h>
 
// Function to count of set bit in N
void count_setbit(int N)
{
 
    // Stores count of set bit in N
    int result = 0;
 
    // Iterate over the range [0, 31]
    for (int i = 0; i < 32; i++) {
 
        // If current bit is set
        if ((1 << i) & N) {
 
            // Update result
            result++;
        }
    }
    printf("%d\n", result);
}
 
// Driver Code
int main()
{
    int N = 43;
 
    count_setbit(N);
    return 0;
}

Java




// Java program for above approach
public class Main {
 
    // Function to count of set bit in N
    public static void count_setbit(int N)
    {
        // Stores count of set bit in N
        int result = 0;
 
        // Iterate over the range [0, 31]
        for (int i = 0; i < 32; i++) {
 
            // If current bit is set
            if (((1 << i) & N) > 0) {
 
                // Update result
                result++;
            }
        }
 
        System.out.println(result);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int N = 43;
        count_setbit(N);
    }
}

Python




# Python program for above approach
 
# Function to count of set bit in N
def count_setbit(N):
 
    # Stores count of set bit in N
    result = 0
     
    # Iterate over the range [0, 31]
    for i in range(32):
     
       # If current bit is set  
       if( (1 << i) & N ):
            
           # Update result
           result = result + 1
     
    print(result)
 
if __name__ == '__main__':
 
    N = 43
    count_setbit(N)

C#




// C# program for above approach
using System;
class GFG {
 
    // Function to count of setbit in N
    static void count_setbit(int N)
    {
 
        // Stores count of setbit in N
        int result = 0;
 
        // Iterate over the range [0, 31]
        for (int i = 0; i < 32; i++) {
 
            // If current bit is set
            if (((1 << i) & N) > 0) {
 
                // Update result
                result++;
            }
        }
 
        Console.WriteLine(result);
    }
 
    // Driver Code
    static void Main()
    {
 
        int N = 43;
        count_setbit(N);
    }
}

Javascript




<script>
// Javascript program to implement
// the above approach
 
    // Function to count of set bit in N
    function count_setbit(N)
    {
        // Stores count of set bit in N
        let result = 0;
   
        // Iterate over the range [0, 31]
        for (let i = 0; i < 32; i++) {
   
            // If current bit is set
            if (((1 << i) & N) > 0) {
   
                // Update result
                result++;
            }
        }
        document.write(result);
    }
 
// Driver Code
        let N = 43;
        count_setbit(N); 
     
    // This code is contributed by souravghosh0416.
</script>
Output: 
4

 

Time Complexity: O(log2(N))
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 :