Minimum numbers needed to express every integer below N as a sum
Last Updated :
19 Jul, 2022
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 N=2^{m}-1 then the integers are [1, 2, 4..., 2^{m-1}] . Which is just another way of saying 2^0+2^1+2^2+...+2^{m-1} = \frac{2^{m}-1}{2-1} = 2^{m}-1 .So now we know for 2^{m}-1 minimum value of K is m.
Now we inspect what happens for 2^{m} .For 2^{m} we just add a new integer 1 to our list of integers. Realize that for every number from 2^{m} to 2^{m+1}-1 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;
}
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));
}
}
Python3
# 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
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
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
?>
JavaScript
<script>
// Javascript 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)
{
var count = 0;
while (n)
{
count++;
n >>= 1;
}
return count;
}
// Driver code
var n = 32;
document.write("Minimum value of K is = " +
countBits(n));
// This code is contributed by rrrtnx
</script>
output:
Minimum value of K is = 6
Time Complexity: O(log n)
Auxiliary Space: O(1)
Please see count set bits for more efficient methods to count set bits in an integer.
Similar Reads
Minimum count of numbers needed from 1 to N that yields the sum as K Given two positive integers N and K, the task is to print the minimum count of numbers needed to get the sum equal to K, adding any element from the first N natural numbers only once. If it is impossible to get the sum equal to K, then print "-1". Examples: Input: N = 5, K = 10Output: 3Explanation:
6 min read
Minimum number of elements having sum S and bitwise OR as X Given two integers X and S, the task is to find the minimum length of the sequence of positive integers such that the sum of their elements is S and the bitwise OR(^) of their elements is X. If there does not exist any sequence which satisfies the above conditions, print ?1. Examples: Input: X = 13,
9 min read
Find a number which give minimum sum when XOR with every number of array of integers Given an array arr[] of non-negative integers, the task is to find an integer X such that (arr[0] XOR X) + (arr[1] XOR X) + ... + arr[n - 1] XOR X is minimum possible.Examples: Input: arr[] = {3, 9, 6, 2, 4} Output: X = 2, Sum = 22Input: arr[] = {6, 56, 78, 34} Output: X = 2, Sum = 170 Approach: We
8 min read
Minimum integer required to do S ⤠N*X Given an array A[] of size N. Let us denote S as the sum of all integers present in the array. Among all integers present in the array, find the minimum integer X such that S ⤠N*X. Examples: Input: N = 3, A[] = [1, 2, 3]Output: 2Explanation: Total sum of the array is 6, let's check for all the numb
8 min read
Minimum count of elements that sums to a given number Given infinite number of elements of form 10^n and 25*100^n ( n >= 0 ). The task is to find the minimum count of elements chosen such that there sum is equal to K. Examples: Input : K = 48 Output : 6 elements chosen are: (1 + 1 + 1 + 10 + 10 + 25)Input : 69 Output : 9 elements chosen are: (1 + 1
7 min read
Minimum count of numbers required ending with 7 to sum as a given number Given an integer N, the task is to find the minimum count of numbers ending with 7 such that the sum of these numbers is N.Examples: Input: N = 38 Output: 4 7 + 7 + 7 + 17 Input: N = 46 Output: -1 46 cannot be represented as the sum of integers ending with 7. Input: N = 215 Output: 5 7 + 7 + 7 + 7 +
6 min read