# Minimum numbers needed to express every integer below N as a sum

We have an integer N. We need to express N as a sum of K integers such that by adding some(or all) of these integers we can get all the numbers in the range[1, N]. What is the minimum value of K?

**Examples:**

Input : N = 7 Output : 3 Explanation : Three integers are 1, 2, 4. By adding some(or all) of these groups we can get all number in the range 1 to N. 1; 2; 1+2=3; 4; 1+4=5; 2+4=6; 1+2+4=7 Input : N = 32 Output : 6 Explanation : Six integers are 1, 2, 4, 8, 16, 1.

1st we solve the problem for small numbers by hand.

n=1 : 1

n=2 : 1, 1

n=3 : 1, 2

n=4 : 1, 2, 1

n=5 : 1, 2, 2

n=6 : 1, 2, 3

n=7 : 1, 2, 4

n=8 : 1, 2, 4, 1

If we inspect this closely we can see that if then the integers are . Which is just another way of saying .So now we know for minimum value of K is m.

Now we inspect what happens for .For we just add a new integer 1 to our list of integers. Realize that for every number from we can increase the newly added integer by 1 and that will be the optimal list of integers. To verify look at N=4 to N=7, minimum K does not change; only the last integer is increased in each step.

Of course we can implement this in iterative manner in **O(log N)** time (by inserting successive powers of 2 in the list and the last element will be of the form N-(2^n-1)). But this is exactly same as finding the length of binary expression of N which also can be done in **O(log N)** time.

## C++

`// CPP program to find count of integers needed ` `// to express all numbers from 1 to N. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to count length of binary expression of n ` `int` `countBits(` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` `while` `(n) { ` ` ` `count++; ` ` ` `n >>= 1; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 32; ` ` ` `cout << ` `"Minimum value of K is = "` ` ` `<< countBits(n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find count of integers needed ` `// to express all numbers from 1 to N ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// function to count length of binary expression of n ` `static` `int` `countBits(` `int` `n) ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` `while` `(n>` `0` `) { ` ` ` `count++; ` ` ` `n >>= ` `1` `; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `n = ` `32` `; ` ` ` `System.out.println(` `"Minimum value of K is = "` `+ ` ` ` `countBits(n)); ` ` ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python3 program to find count of integers ` `# needed to express all numbers from 1 to N. ` ` ` `# function to count length of ` `# binary expression of n ` `def` `countBits(n): ` ` ` ` ` `count ` `=` `0` `; ` ` ` `while` `(n): ` ` ` `count ` `+` `=` `1` `; ` ` ` `n >>` `=` `1` `; ` ` ` ` ` `return` `count; ` ` ` `# Driver code ` `n ` `=` `32` `; ` `print` `(` `"Minimum value of K is ="` `, ` ` ` `countBits(n)); ` ` ` `# This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find count of ` `// integers needed to express all ` `// numbers from 1 to N ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// function to count length of ` `// binary expression of n ` `static` `int` `countBits(` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` `count++; ` ` ` `n >>= 1; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `n = 32; ` ` ` `Console.WriteLine(` `"Minimum value of K is = "` `+ ` ` ` `countBits(n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Sach_Code ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find count of integers ` `// needed to express all numbers from 1 to N. ` ` ` `// function to count length of ` `// binary expression of n ` `function` `countBits(` `$n` `) ` `{ ` ` ` `$count` `= 0; ` ` ` `while` `(` `$n` `) ` ` ` `{ ` ` ` `$count` `++; ` ` ` `$n` `>>= 1; ` ` ` `} ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver code ` `$n` `= 32; ` `echo` `"Minimum value of K is = "` `, ` ` ` `countBits(` `$n` `), ` `"\n"` `; ` ` ` `// This code is contributed by Sachin ` `?> ` |

*chevron_right*

*filter_none*

**output:**

Minimum value of K is = 6

Please see count set bits for more efficient methods to count set bits in an integer.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Minimum decrements to make integer A divisible by integer B
- Minimum number of letters needed to make a total of n
- Minimum minutes needed to make the time palindromic
- Find minimum number of Log value needed to calculate Log upto N
- Find minimum operations needed to make an Array beautiful
- Minimum steps needed to cover a sequence of points on an infinite grid
- Minimum number of palindromes required to express N as a sum | Set 2
- Minimum number of palindromes required to express N as a sum | Set 1
- Express a number as sum of consecutive numbers
- Express an odd number as sum of prime numbers
- Count ways to express a number as sum of exactly two numbers
- Count ways to express a number as sum of consecutive numbers
- Minimum positive integer value possible of X for given A and B in X = P*A + Q*B
- Minimum positive integer divisible by C and is not in range [A, B]
- Count the minimum steps to reach 0 from the given integer N
- Minimum integer with at most K bits set such that their bitwise AND with N is maximum
- Minimum positive integer to divide a number such that the result is an odd
- Minimum integer such that it leaves a remainder 1 on dividing with any element from the range [2, N]
- Find the minimum sum of distance to A and B from any integer point in a ring of size N
- Minimum integer that can be obtained by swapping adjacent digits of different parity

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.