Tag: datastructures
-
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/number-of-1-bits/ Solution: One way to solve this problem is to do AND operation…
-
Data Structures and Algorithms in Java – Arrays – Product of Array Except Self – faster than 100% of submissions on leetcode!
Problem: Given an array of integers , find an array of products where each product is the product of all the numbers except the element in that index. ie) For every number number[i] , find the product of all the numbers except number[i] Input: numbers = [1,2,3,4] Output: [24,12,8,6] Calculated as below: 24 = 2*3*4…