Tag: binary

Data Structures & Algorithms in Java – Binary – Missing Number
Problem: Given an array with values between 1 to n , find the missing number between 1 to n that is not present in the array Input: [9,6,4,2,3,5,7,0,1] Output: 8 In the above array of length 9 we have all the numbers from 1 to 9 except 8 , so output is 8 Try out…

Data Structures & Algorithms in Java – Binary – Counting Bits
Problem: Given an integer , find the number of 1 bits for every number (in binary form) starting from 0 to that integer. Input: 8 Output: [0,1,1,2,1,2,2,3,1] For the number 8 , you need to find the number of 1 bits in the numbers 0,1,2,3,4,5,6,7,8 and store them in an output array in their respective…

Data Structures & Algorithms in Java – Binary – Number of 1 bits in an integer
Problem: Given an integer , count the number of 1 bits in it when converted to a binary Input: 8 Output: 1 (since the binary representation of 8 is 1000 and it has a single 1 bit) Try out the solution here: https://leetcode.com/problems/numberof1bits/ Solution: One way to solve this problem is to do AND operation…

Data Structures & Algorithms in Java – Binary – Sum of two integers
Problem : Given two integers, add them without using + and – operators. Input: a = 3 b= 5 Output: 8 Try out the solution here: https://leetcode.com/problems/sumoftwointegers/ Hint: Use bitwise operators Solution: Let’s solve the problem using bitwise operators XOR, AND and SHIFT. Adding two binary numbers is equal to performing XOR operation between them…