Open In App
Related Articles

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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)



Last Updated : 29 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads