This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class KthSmallestInSortedMatrix { | |
public static int findKthSmallest(int[][] matrix, int k) { | |
int n = matrix.length; | |
int start = matrix[0][0], end = matrix[n - 1][n - 1]; | |
while (start < end) { | |
int mid = start + (end - start) / 2; | |
// first number is the smallest and the second number is the largest | |
int[] smallLargePair = { matrix[0][0], matrix[n - 1][n - 1] }; | |
int count = countLessEqual(matrix, mid, smallLargePair); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ArrayReader { | |
int[] arr; | |
ArrayReader(int[] arr) { | |
this.arr = arr; | |
} | |
public int get(int index) { | |
if (index >= arr.length) | |
return Integer.MAX_VALUE; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BinarySearch { | |
public static int search(int[] arr, int key) { | |
int start = 0, end = arr.length - 1; | |
boolean isAscending = arr[start] < arr[end]; | |
while (start <= end) { | |
// calculate the middle of the current range | |
int mid = start + (end - start) / 2; | |
if (key == arr[mid]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PairWithTargetSum { | |
public static int[] search(int[] arr, int targetSum) { | |
int left = 0, right = arr.length - 1; | |
while (left < right) { | |
// comparing the sum of two numbers to the 'targetSum' can cause integer overflow | |
// so, we will try to find a target difference instead | |
int targetDiff = targetSum - arr[left]; | |
if (targetDiff == arr[right]) | |
return new int[] { left, right }; // found the pair |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TreeNode { | |
int val; | |
TreeNode left; | |
TreeNode right; | |
TreeNode(int x) { | |
val = x; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class Subsets { | |
public static List<List<Integer>> findSubsets(int[] nums) { | |
List<List<Integer>> subsets = new ArrayList<>(); | |
// start by adding the empty subset | |
subsets.add(new ArrayList<>()); | |
for (int currentNumber : nums) { | |
// we will take all existing subsets and insert the current number in them to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class Point { | |
int x; | |
int y; | |
public Point(int x, int y) { | |
this.x = x; | |
this.y = y; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WaterContainer { | |
public static int findMaxWater(int[] buildingHeights) { | |
// use two pointer approach to find the maximum area | |
int left = 0, right = buildingHeights.length - 1; | |
int maxArea = 0, currentArea = 0; | |
while (left < right) { | |
if (buildingHeights[left] < buildingHeights[right]) { | |
currentArea = (right - left) * buildingHeights[left]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MaxInBitonicArray { | |
public static int findMax(int[] arr) { | |
int start = 0, end = arr.length - 1; | |
while (start < end) { | |
int mid = start + (end - start) / 2; | |
if (arr[mid] > arr[mid + 1]) { | |
end = mid; | |
} else { | |
start = mid + 1; |