Skip to content

Instantly share code, notes, and snippets.

@ravichandrae
ravichandrae / IndexElement.java
Created February 8, 2023 13:41
Given a sorted array of unique positive and negative numbers, find if there is an index i such that A[i] = i
public class IndexElementArray {
public static void main(String[] args) {
System.out.println(indexElement(new int[] {-5, -3, 0, 3, 5})); //Expect True
System.out.println(indexElement(new int[] {-5, -3, 0, 1, 2})); //Expect false
System.out.println(indexElement(new int[] {0, 2, 3, 4, 5})); //Expect True
System.out.println(indexElement(new int[] {-1, 0, 1, 2, 4})); //Expect True
System.out.println(indexElement(new int[] {-10, 10, 20, 30, 40})); //Expect false
}
private static boolean indexElement(int []arr) {
@ravichandrae
ravichandrae / MaxUnimodalArray.java
Created February 8, 2023 12:34
Finding the maximum element in a unimodal array (elements increases to a maximum and decreases there after). Efficient algorithm which runs in O(log n) time.
public class Main {
public static void main(String[] args) {
System.out.println(maxElementInUnimodalArray(new int[]{1, 2, 5, 4, 3})); //Expected 5
System.out.println(maxElementInUnimodalArray(new int[]{1, 2, 3, 4, 5})); //Expected 5
System.out.println(maxElementInUnimodalArray(new int[]{5, 4, 3, 2, 1})); //Expected 5
System.out.println(maxElementInUnimodalArray(new int[]{6})); //Expected 6
}
private static int maxElementInUnimodalArray(int[] arr) {
if (arr == null || arr.length == 0) {
@ravichandrae
ravichandrae / ExpressionEvaluation.java
Created December 21, 2022 13:26
Given an expression like add( sub(3, 2), add(0, 1) ), evaluate their results.
import java.util.Stack;
public class ExpressionEvaluation {
public static void main(String[] args) {
String expression = "add( 1, 2)";
int result = evaluateExpression(expression);
System.out.println(result);
expression = "add(sub(2, 1), 3)";
System.out.println(evaluateExpression(expression));
expression = "add(3, sub(22, 20))";
@ravichandrae
ravichandrae / RomanNumerals.java
Created September 15, 2022 10:23
Program to convert integers to roman and vice versa
import java.util.HashMap;
import java.util.Map;
public class RomanNumbers {
public static void main(String[] args) {
for (int i = 1; i <= 50; i++) {
String roman = intToRoman(i);
System.out.println(roman + " = " + romanToInt(roman));
}
@ravichandrae
ravichandrae / RotatedSortedArray.java
Created September 8, 2022 14:44
Binary search in a rotated sorted array
public class RotatedSortedArray {
public static void main(String[] args) {
testFind();
}
public static void testFind() {
System.out.println(find(null, 10));// Expect -1
int[] arr1 = new int[]{};
System.out.println(find(arr1, 100));// Expect -1
int[] arr2 = new int[]{10};
@ravichandrae
ravichandrae / PairsTest.java
Created September 2, 2022 17:25
How to implement our own pair data structure in Java
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
public class PairsTest {
static class IntPair {
int x, y;
public IntPair(int a, int b) {
this.x = a;
@ravichandrae
ravichandrae / permutations.py
Created September 1, 2022 18:44
Given a list, generate all permutations of the items in it.
def permutations(lst):
return permutations_util(lst, 0)
def permutations_util(lst, index):
perms = []
if index == len(lst) - 1:
perms.append(lst.copy())
for i in range(index, len(lst)):
lst[i], lst[index] = lst[index], lst[i]
@ravichandrae
ravichandrae / LongestSubarrayUnique.java
Created April 13, 2022 17:59
Finding the length of the longest subarray with unique/distinct numbers
import java.util.HashMap;
import java.util.Map;
public class LongestSubarrayUnique {
public static void main(String[] args) {
System.out.println(longestDistinctSubarray(null)); //Expected 0
System.out.println(longestDistinctSubarray(new int[]{})); //Expected 0
System.out.println(longestDistinctSubarray(new int[]{1})); //Expected 1
System.out.println(longestDistinctSubarray(new int[]{1, 2, 3, 4, 5})); //Expected 5
System.out.println(longestDistinctSubarray(new int[]{23, 9, 36, 23, 4})); //Expected 4
@ravichandrae
ravichandrae / count_bst.py
Created March 23, 2022 14:08
Given a number of nodes, count how many BSTs we can construct
def countBST(n):
"""
:param n: a positive integer
:return: Number of all possible binary search trees (BST) with n unique numbers
The number is called a Catalan number F(n) = C(2*n, n) / (n+1) = (2*n)! / (n+1)! * n!
As the above formula involves calculating factorial which is laborious and can overflow in some cases,
We can use the following recurrence relation to make the calculation simple.
F(0) = 1
F(n+1) = (2*(2*n + 1) / (n+2)) * F(n)
"""
@ravichandrae
ravichandrae / LongestIncreasingSubsequence.java
Last active December 30, 2021 07:49
Find the length of longest increasing subsequence of a given array
import java.util.Arrays;
public class LongestIncreasingSubsequence {
public static void main(String[] args) {
System.out.println(longestIncreasingSubsequence(null)); //Expected 0
System.out.println(longestIncreasingSubsequence(new int[]{})); //Expected 0
System.out.println(longestIncreasingSubsequence(new int[]{1})); //Expected 1
System.out.println(longestIncreasingSubsequence(new int[]{1, 5, 10})); //Expected 3
System.out.println(longestIncreasingSubsequence(new int[]{1, 5, 2, 7, 6, 8})); //Expected 4
System.out.println(longestIncreasingSubsequence(new int[]{6, 1, 7, 3, 9, 4, 11, 13})); //Expected 5