x&(x-1) Explained

What is x - 1 wrt x x - 1 is derived from x by flipping all bits to the right of (and including) the rightmost set bit (1) in x to 0s. Examples x = 4 (100 in binary), x - 1 = 3 (011 in binary). x = 6 (110 in binary), x - 1 = 5 (101 in binary). What is X & (x-1)? x & (x - 1) results in a value with all bits from x remaining the same except for the rightmost set bit (1), which becomes 0. ...

Remove duplicates from Sorted List 2

Problem Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well. Examples Example 1: Input: head = [1,2,3,3,4,4,5] Output: [1,2,5] ...

Kth Largest Element in a Stream

Problem Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element. Implement KthLargest class: KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of integers nums. int add(int val) Appends the integer val to the stream and returns the element representing the kth largest element in the stream. Class Skeleton: ...

Number of 1 Bits Problem

Problem Write a function that takes the binary representation of a positive integer and returns the number of set bits it has (also known as the Hamming weight). Note: Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer’s internal binary representation is the same, whether it is signed or unsigned. In Java, the compiler represents the signed integers using 2’s complement notation. Therefore, in Example 3, the input represents the signed integer. -3. Examples Example 1: ...

Largest Number From Given Numbers

Problem Given a list of non-negative integers nums, arrange them such that they form the largest number and return it. Since the result may be very large, so you need to return a string instead of an integer. Example Example 1: Input: nums = [10,2] Output: "210" Example 2: Input: nums = [3,30,34,5,9] Output: "9534330" ...

Number Complement Problem

Problem The complement of an integer is the integer you get when you flip all the 0’s to 1’s and all the 1’s to 0’s in its binary representation. For example, The integer 5 is "101" in binary and its complement is "010" which is the integer 2. Given an integer num, return its complement. Examples Example 1: Input: num = 5 Output: 2 Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2. ...

Path with Maximum Probability Problem

Problem You are given an undirected weighted graph of n nodes (0-indexed), represented by an edge list where edges[i] = [a, b] is an undirected edge connecting the nodes a and b with a probability of success of traversing that edge succProb[i]. Given two nodes start and end, find the path with the maximum probability of success to go from start to end and return its success probability. ...

Convert BST to Greater Sum Tree

Problem Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST. Examples Example 1: ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy