Position of Rightmost Bit with First Carry in Sum of Two Binary in C++

sudhir sharma
Updated on 17-Apr-2020 08:10:38

121 Views

In this problem, we are given two positive integers N and M. Our task is to print the rightmost bit that generates the first carry bit in the binary addition of the sum of N and M.Let’s take an example to understand the problem, Input − N = 5, M = 14Output − 3Explanation −(5)2 = 0101 , (14)2 = 1110 Sum 0101 +   1110     10011To solve this problem, we will consider some observations from boolean algebra.The sum will generate a carry when both numbers are 1. So, we will find all bits where carry is generated. ... Read More

Position of Rightmost Common Bit in Two Numbers in C++

sudhir sharma
Updated on 17-Apr-2020 08:08:26

219 Views

In this problem, we are given two numbers M and N. Our task is to print the position (index) of the rightmost common bit of the two numbers.Let’s take an example to understand the problem, Input − N = 4 , M = 7Output − 3Explanation − (4)2 = 100 , (7)2 = 111. The rightmost common bit is at index 3.To solve this problem, we will have to find all the same bits of the numbers. To find all same bits we will find xor of M and N. Then we will find the rightmost bit in the negation ... Read More

Position of Rightmost Different Bit in C++

sudhir sharma
Updated on 17-Apr-2020 08:06:06

378 Views

In this problem, we are given two numbers N and M. Our task is to find the index of the rightmost different bit in the binary representation of the number.Let’s take an example to understand the problem, Input − N = 12 , M = 9Output − 2Explanation − (12)2 = 1100 and (10)2 = 1010.The second bit from right is a different bit.To solve this problem, we will have to find all the different bits of the numbers. To find all different bits we will find xor of M and N. Then we will find the rightmost bit of M^N.This seems ... Read More

Position of Rightmost Set Bit in C++

sudhir sharma
Updated on 17-Apr-2020 08:03:27

404 Views

In this problem, we are given a number N. Our task is to print the index of the rightmost set bit of the number.Let’s take an example to understand the problem, Input − 4Output − 3Explanation − binary of 4 is 100, the index of the rightmost set bit is 3.To solve this problem, a simple solution would be shifting the number till a set bit is encountered but this could take a lot of computation if the number is large.A more efficient solution will be using boolean algebra. For this we will first calculate 2’s complement of the number, ... Read More

Position of Robot After Given Movements in C++

sudhir sharma
Updated on 17-Apr-2020 08:01:15

2K+ Views

In this problem, we are given a robot that moves in all four directions but only one move. The directions are up(‘U’), down(‘D’), left(‘L’), right(‘R’). And we are given a string that contains initials of directions of the number. Our task is to print the final position of the robot, given the initial position of the robot is (0, 0).Let’s take an example to understand the problemInput − input: ‘LDRRUL’Output − (0, 0)Explanation −L (left) : (0, 0) -> (-1, 0) D (down) : (-1, 0) -> (-1, -1) R (right) : (-1, -1) -> (0, -1) R (right) : ... Read More

Position of the K-th Set Bit in a Number in C++

sudhir sharma
Updated on 17-Apr-2020 07:58:52

559 Views

In this problem, we are given two integers N and K. Our task is to find the index of Kth set a bit of the number N, counted from right.Set bits are checked from the binary representation of the number. The indexing in binary representation starts from index 0 from the right direction and propagates towards left.Example − in the binary number ‘011101’, at index 0 from right we have 1, at index 1 from right we have 0, and so on.Now, let’s take an example to understand the problemInput − N = 6, K = 2Output − 2Explanation − ... Read More

Positive Elements at Even and Negative at Odd Positions in C++

sudhir sharma
Updated on 17-Apr-2020 07:55:04

352 Views

In this problem, we are given an array and our task is to convert the array in such a way that all positive numbers are at even index places and all negative numbers are at odd index places.There might be an unequal number of positive and negative values, in this case, we will not move extra values.Let’s take an example to understand the problem, Input − {3, 5, -1, 19, -7, -2}Output − {3, -1, 5, -7, 19, -2}To solve this problem, we will have to find the element which is out of order in the array. There can be ... Read More

Possibility of a Word from Given Set of Characters in C++

sudhir sharma
Updated on 17-Apr-2020 07:49:52

165 Views

In this problem, we are two string str1 and str2. Our task is to check whether all characters of str2 and present in str1.Let’s take an example to understand the problemInput −str1 = “Hello” str2 = “Hell”Output − yesExplanation − all characters of str2 are present in str1.To solve this problem, a simple solution will be checking each character of str2 in str1 and then returning the solution.But we need to create effective solutions. So, we will use a frequency array (length 256 for all valid characters) and then traverse str1 and increase the value in frequency array based on ... Read More

Possibility of Moving Out of Maze in C++

sudhir sharma
Updated on 17-Apr-2020 07:46:33

276 Views

In this problem, we are given a maze of n integers, each integer indicates the number of moves to be done. Along with the direction indicated using ‘>’ and ‘ < > >Output − YESExplanation − moving from start, we will move 2 positions ahead, then 1 position ahead, then 4 positions ahead. This will move our of the maze.To solve this problem, we will check whether moving out of the maze is possible or not. For this either we need to go below 0 or above n. Starting from 0, we will process the direction based on the sign ... Read More

Possible Cuts of a Number Divisible by 3 in C++

sudhir sharma
Updated on 17-Apr-2020 07:43:24

154 Views

In this problem, we are given a large integer value (with up to 105 digits). Our task is to print the total number of cuts required such that maximum parts are divisible by 3.Let’s take an example to understand the problemInput − 9216Output − 3Explanation − the number is divided as 9|21|6.To solve this problem, we will have to start for the least significant bit of the number i.e. the last digit of the number. For here, we will find the smallest number divisible by three. And then update count based on it.Example − if arr[i] makes a 3 divisible ... Read More

Advertisements