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
| /** | |
| * 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; |
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
| /** | |
| * 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; |
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
| /** | |
| * 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) { |
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.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]; |
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
| /** | |
| * 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 |
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.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(); |
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 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 { |
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.*; | |
| /** | |
| * 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, |
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.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: |