Prime
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...
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...
Greatest Common Divisor Euclidean algorithm public int gcd(int a, int b) { while (b != 0) { int tmp = b; b = a % b; a = tmp; } return a; } Variants Greatest...
Subtree of Another Tree Binary Lifting This is a good tutorial on Binary Lifting. Kth Ancestor of a Tree Node // dp[i][j] = j-th node's (2 ^ i)-th ancestor in the path private int[][] dp; priva...
Selection algorithm Heap Sort Kth Largest Element in an Array public int findKthLargest(int[] nums, int k) { // min heap Queue<Integer> pq = new PriorityQueue<>(); for (i...