Repeated Element
N-Repeated Element in Size 2N Array k repeated element in size n array: Find the minimum size m, so that there exists at least one subarray with size m, and it is guaranteed to contain more than ...
N-Repeated Element in Size 2N Array k repeated element in size n array: Find the minimum size m, so that there exists at least one subarray with size m, and it is guaranteed to contain more than ...
Find All Numbers Disappeared in an Array Sort public List<Integer> findDisappearedNumbers(int[] nums) { for (int i = 0; i < nums.length; i++) { int index = nums[i] - 1; ...
Time complexity: Build: O(n) Maximize Sum Of Array After K Negations public int largestSumAfterKNegations(int[] A, int K) { Queue<Integer> pq = new PriorityQueue<>(); Arra...
Ordering https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/stream/package-summary.html Streams may or may not have a defined encounter order. Whether or not a stream has an e...
Representation Nodes from 0 to n: Map<Integer, List<Integer>>: unweighted Map<Integer, Map<Integer, Integer>>: weighted List<Integer>[]: unweighted List<i...
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...