Bit Manipulation
Properties n ^ 0 = n n ^ n = 0 2k ^ (2k + 1) = 1 n & -n // clears all but rightmost set bit n & (n - 1) // zeros out rightmost set bit, Brian Kernighan's Algorithm (n & (n - 1)) == ...
Properties n ^ 0 = n n ^ n = 0 2k ^ (2k + 1) = 1 n & -n // clears all but rightmost set bit n & (n - 1) // zeros out rightmost set bit, Brian Kernighan's Algorithm (n & (n - 1)) == ...
Reverse Integer public int reverse(int x) { int res = 0; while (x != 0) { int d = x % 10; int tmpRes = 10 * res + d; // avoid overflow if ((tmpRes - d) / 10...
Fundamentals Backtracking = DFS + pruning private void backtrack(var i) { for (var i : space) { backtrack(); } } Permutations public List<List<Integer>> permute(int...
Theorems Euclid-Euler Theorem The Euclid-Euler theorem relates perfect numbers to Mersenne primes. It states that an even number is perfect if and only if it has the form \(2^{p−1}(2^p − 1)\), wh...
Palindrome String public boolean isPalindrome(String s) { int left = 0, right = s.length() - 1; while (left < right) { if (s.charAt(left++) != s.charAt(right--)) { r...
int[] -> Integer[] int[] a = {0, 1, 2}; Integer[] b = Arrays.stream(a).boxed().toArray(Integer[]::new); Integer[] -> int[] Integer[] a = {0, 1, 2}; int[] b = Arrays.stream(a).mapToInt(Integ...
Java implementations of Stack: Deque (Stack) StringBuilder Array Binary Searchable Numbers in an Unsorted Array public int binarySearchableNumbers(int[] nums) { int n = nums.length; ...
Number of Zero-Filled Subarrays public long zeroFilledSubarray(int[] nums) { long count = 0; for (int i = 0, j = 0; j < nums.length; j++) { if (nums[j] != 0) { // i ...
Definition A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree. Find the Maximum Sum of ...
Theorem Lagrange’s four-square theorem Lagrange’s four-square theorem, also known as Bachet’s conjecture, states that every natural number can be represented as the sum of four integer squares. T...