Skip to main content

Posts

Showing posts from December, 2020

Count Set Bits GFG problems solution

https://practice.geeksforgeeks.org/problems/set-bits0143/1 Problem statement-  Given a positive integer N, print count of set bits in it.  Example 1: Input: N = 6 Output: 2 Explanation: Binary representation is '110' So the count of the set bit is 2. Example 2: Input: 8 Output: 1 Explanation: Binary representation is '1000' So the count of the set bit is 1. Your Task:    You don't need to read input or print anything. Your task is to complete the function  setBits () which takes an Integer N and returns the count of number of set bits. Expected Time Complexity:  O(LogN) Expected Auxiliary Space:  O(1) Constraints: 1 ≤ N ≤ 10 9 SOlution - If we do AND operation  1&1=1 1&0=0 by using above property  we will perform and operation with current number if  1&currnumber gives it means we will increament our final ans by 1 we will right shift current number till current number become 0. follow code below. question link- https://practice.geeksforgeeks

Red and Blue codeforces problem code

 Problem Statement- Monocarp had a sequence  a a  consisting of  n + m n + m  integers  a 1 , a 2 , … , a n + m a 1 , a 2 , … , a n + m . He painted the elements into two colors, red and blue;  n n  elements were painted red, all other  m m  elements were painted blue. After painting the elements, he has written two sequences  r 1 , r 2 , … , r n r 1 , r 2 , … , r n  and  b 1 , b 2 , … , b m b 1 , b 2 , … , b m . The sequence  r r  consisted of all red elements of  a a   in the order they appeared in  a a ; similarly, the sequence  b b  consisted of all blue elements of  a a   in the order they appeared in  a a  as well . Unfortunately, the original sequence was lost, and Monocarp only has the sequences  r r  and  b b . He wants to restore the original sequence. In case there are multiple ways to restore it, he wants to choose a way to restore that maximizes the value of f ( a ) = max ( 0 , a 1 , ( a 1 + a 2 ) , ( a 1 + a 2 + a 3 ) , … , ( a 1 + a 2 + a 3 + ⋯ + a n + m ) ) f ( a ) = ma