Posts

Showing posts from September, 2020

Nearest power of two

Image
 Suppose we are given a number n and we want to find the nearest number to n which is power of 2 . For Example:  n = 122 then answer is 64 i.e, 2 6 So, here is a cpp code which we can use     Output : 64    #bitmask

Dividing integer "n" into multiple groups where each group sum is equal to "n".

Image
  Let us suppose we are having a number "n" and we want to generate sequence of numbers where each sequence gives the sum is equal to "n". Let n = 5 and intially we can write as 1 + 1 + 1 + 1 + 1 = 5. Now there are 4  barrier (X) as shown in Figure-1 in between these "ones"where we can place our barrier.                                                                                                                  Figure-1 What I mean by barrier is like this (11|11|1) where | represents a barrier, hence our paritition will be (2, 2, 1) for 5 or we can place our barriers like this (1|111|1) where our partition will be (1, 3, 1)...