what can be the best algorithm to find numbers with only single set bit and whose sum is equal to given number?

GeekyJ

Suppose i have one number 0b10110010,

I want to parse it in different numbers whose sum is equal to above number, All parsed numbers should have only single bit set.

 10000000  <-- num1
 + 100000  <-- num2
 +  10000  <-- num3
 +     10  <-- num4
----------
 10110010  <-- num1 + num2 + num3 + num4

What can be the best algorithm for this?

fuz

Basically, something like this:

int i;
for (i = 1; i <= num && i != 0; i <<= 1) {
    if ((i & num) == 0)
        continue;

    /* i is part of the decomposition, do something with it */
    printf("0x%4x\n", i);
}

This iterates through all possible numbers with one bit set and ignores those for which the corresponding bit in num isn't set. The complexity is O(log num). There is also an O(k) solution where k is the number of 1 bits in num, the algorithm goes like this:

int i, n = num;
while (n != 0) {
    i = ((n - 1) & ~n) + 1;
    /* i is part of the decomposition */
    printf("%4x\n", i);
    n &= n - 1;
}

Consider the following diagram to understand how this works:

n             101101101010100000000
n - 1         101101101010011111111
~n            010010010101011111111
~n & (n - 1)  000000000000011111111
i             000000000000100000000
n & (n - 1)   101101101010000000000

In the last line, n &= ~i could also be used but that would force the compiler to retain the i variable for a bit longer than needed which may be less optimal for speed. Benchmark when in doubt.

My personal guess is that the second method is faster if num is sparse, i.e. only few bits are set in num. Due to its lower number of operations, you should use the first method if num is known to not be sparse.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find permutations of a given set of numbers whose sum APPROACH a target number

From Dev

Find numbers of subarray of an array whose sum is divided by given number

From Dev

How to find out the best combination of a given vector whose sum is closest to a given number

From Dev

java based algorithm to find if there are any 2 numbers in the array whose sum is equal to x

From Dev

find a combination of numbers that equal a given sum from a single column in mysql table

From Dev

Algorithm to take the best set of numbers to sum

From Dev

Given 4 numbers of array of 1 to 10 elements. Find 3 numbers whose sum can generate all the four numbers?

From Dev

Find the largest subset of a set of numbers such that the sum of any 2 numbers is not divisible by a given number

From Dev

Find Pair Of Integers in Array whose Sum is Given Number in PHP

From Dev

Find the minimum set of integers whose sum is greater than a given integer

From Dev

program to find perfect number : error in output . perfect number is number whose sum of factors equals the given number

From Dev

find the sum of all the numbers in the Fibonacci series that are smaller or equal to that number

From Dev

Check if in a set of numbers, a number n is equal to sum of its subset

From Dev

Returning the numbers in a list whose sum is a given target

From Dev

find optimal sum of elements in list of numbers that are > a given number

From Dev

numpy - Given a number, find numbers that sum to it, with fuzzy weights

From Dev

Find triplets in an array such that sum of two numbers is also a number in the given array

From Dev

How can I pair my user's input number in an array whose sum is equal to specified input number?

From Dev

Given two unsorted arrays A and B ,finding some pair of elements whose sum (or difference) equal to a given k - by sorting only one of the arrays

From Dev

How to find continuous sub-array whose sum is equal to a particular number in O(n) time complexity

From Dev

smallest subset of array whose sum is equal to key. Condition : Values can be used any number of times

From Dev

how to find the length of the longest contiguous subarray whose sum is divisible by a given number

From Dev

Find minimum pair of numbers whose sum is 15

From Dev

What is the best algorithm for sorting biggest X number in a set

From Dev

Given a number N how many pairs of numbers have square sum less than or equal to N?

From Dev

Find a sum of two distinct numbers from the set, closest to the query number

From Dev

Given a sorted array and a parameter k, find the count of sum of two numbers greater than or equal to k

From Dev

Find elements that sum to a given number

From Dev

Algorithm to find subset that is equal to sum with lowest cost

Related Related

  1. 1

    Find permutations of a given set of numbers whose sum APPROACH a target number

  2. 2

    Find numbers of subarray of an array whose sum is divided by given number

  3. 3

    How to find out the best combination of a given vector whose sum is closest to a given number

  4. 4

    java based algorithm to find if there are any 2 numbers in the array whose sum is equal to x

  5. 5

    find a combination of numbers that equal a given sum from a single column in mysql table

  6. 6

    Algorithm to take the best set of numbers to sum

  7. 7

    Given 4 numbers of array of 1 to 10 elements. Find 3 numbers whose sum can generate all the four numbers?

  8. 8

    Find the largest subset of a set of numbers such that the sum of any 2 numbers is not divisible by a given number

  9. 9

    Find Pair Of Integers in Array whose Sum is Given Number in PHP

  10. 10

    Find the minimum set of integers whose sum is greater than a given integer

  11. 11

    program to find perfect number : error in output . perfect number is number whose sum of factors equals the given number

  12. 12

    find the sum of all the numbers in the Fibonacci series that are smaller or equal to that number

  13. 13

    Check if in a set of numbers, a number n is equal to sum of its subset

  14. 14

    Returning the numbers in a list whose sum is a given target

  15. 15

    find optimal sum of elements in list of numbers that are > a given number

  16. 16

    numpy - Given a number, find numbers that sum to it, with fuzzy weights

  17. 17

    Find triplets in an array such that sum of two numbers is also a number in the given array

  18. 18

    How can I pair my user's input number in an array whose sum is equal to specified input number?

  19. 19

    Given two unsorted arrays A and B ,finding some pair of elements whose sum (or difference) equal to a given k - by sorting only one of the arrays

  20. 20

    How to find continuous sub-array whose sum is equal to a particular number in O(n) time complexity

  21. 21

    smallest subset of array whose sum is equal to key. Condition : Values can be used any number of times

  22. 22

    how to find the length of the longest contiguous subarray whose sum is divisible by a given number

  23. 23

    Find minimum pair of numbers whose sum is 15

  24. 24

    What is the best algorithm for sorting biggest X number in a set

  25. 25

    Given a number N how many pairs of numbers have square sum less than or equal to N?

  26. 26

    Find a sum of two distinct numbers from the set, closest to the query number

  27. 27

    Given a sorted array and a parameter k, find the count of sum of two numbers greater than or equal to k

  28. 28

    Find elements that sum to a given number

  29. 29

    Algorithm to find subset that is equal to sum with lowest cost

HotTag

Archive