Skip to content

Instantly share code, notes, and snippets.

@sdpatil
sdpatil / RectangleIntersection.java
Created August 8, 2017 16:33
Rectangle intersection
/**
* Problem: Given 2 rectangle find out the intersecting rectangle
*/
public class RectangleIntersection {
public static class Rectangle{
int x, y, width, height;
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
@sdpatil
sdpatil / PalindromeNumber.java
Created August 8, 2017 16:33
Check if a decimal integer is a palindrome
/**
* Problem: Check if the given number is palindromic. Ex. 1001 is palindromic but 17 is not
*/
public class PalindromeNumber {
/*
Solution: You can find out number of digits in the of the decimal number by calculating
Math.log10(x).
Then you can use this information to calculate most significant and least significant digits
and check if they are equal if not return false
@sdpatil
sdpatil / ReverseDigits.java
Created August 8, 2017 16:30
Reverse digits
/**
* Problem: Given a number reverse it. Ex. input 321 output should be 123
*/
public class ReverseDigits {
/*
Solution:- Take % of x with 10 at a time and add it to result.
*/
public long revereseDigits(int x){
long result = 0;
@sdpatil
sdpatil / ClosestInSameBitCount.java
Created August 8, 2017 16:26
Find a closest integer with the same weight
/**
* Problem: Given a number return another number closest to the input which has same number of 1 bits
*/
public class ClosestInSameBitCount {
public static final int NUM_UNSIGN_BITS = 63;
/*
Solution :- Start from the end and check if last and but one last digit match if not then
build a bit mask to reverse those 2 bits
*/
public long closestInSameBitCount(long x) {
@sdpatil
sdpatil / ReverseBits.java
Created August 8, 2017 16:24
Reverse Bits
import java.util.HashMap;
import java.util.Map;
/**
* Problem: Given a number reverse its bits. Ex. given
1011 you should return 1101
*/
public class ReverseBits {
long[] precomputedReverse = new long[1 << 16];
@sdpatil
sdpatil / SwapBits.java
Created August 8, 2017 16:21
Swap Bits
/**
* Problem: Swap bits at position i and j in given number.
Ex. swap(85,0,3). 85 looks like this in binary 1010101 swapping will convert it to 1011100
*/
public class SwapBits {
/*
Solution: Basic idea is simple first check the bits i and j if they are actually different
if not we dont need to swap anything
If the two bits are actually different create a bit mask with i and jth bit set to 1 and
@sdpatil
sdpatil / Parity.java
Created August 8, 2017 15:40
Computer parity of words
import java.util.HashMap;
import java.util.Map;
/**
* Problem: Calculate parity of a long, Parity is 0 if the number
has even number of 1's in it, and 1 if it has odd number of 1's in it
*/
public class Parity {
public static void main(String[] argv) {
Parity parity = new Parity();
@sdpatil
sdpatil / GenerateBinaryTree.java
Created August 8, 2017 15:18
Generate a program which returns all distinct binary trees with specified number of nodes
import com.eip.chapter15.BSTNode;
import java.util.ArrayList;
import java.util.List;
/**
* Problem: Generate a program which returns all distinct binary trees with specified number of
* nodes
*/
public class GenerateBinaryTree {
@sdpatil
sdpatil / CombinationSum240.java
Created August 8, 2017 14:43
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination.
import java.util.*;
/**
* Problem: Given a collection of candidate numbers (C) and a target number (T),
* find all unique combinations in C where the candidate numbers sums to T.
* Each number in C may only be used once in the combination.
* Note:
* All numbers (including target) will be positive integers.
* The solution set must not contain duplicate combinations.
* For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
@sdpatil
sdpatil / CombinationSum30.java
Last active August 8, 2017 14:41
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Problem: Given a set of candidate numbers (C) (without duplicates) and a target number (T),
* find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note: