Skip to content

Instantly share code, notes, and snippets.

@KodeSeeker
KodeSeeker / GetClosestValueinBST.java
Last active December 15, 2015 21:59
Find the closest value to a given value in BST.** Interesting**
public class FindClosest
{
//now we create a test case
public static void main(String[] args)
{
//firstly create a test tree as
// 100
// 50 200
// 10 60 150 300
Tree myBST = new Tree(100);
public static int MaxSubsetSum(int[] nums)
{
//we need define two key variables before loop
//firstly we need a temp sum to keep track of the sum of numbers we processed so far
//also we need a maxSum to keep track of the currently max sum so far for return purpose
int tempSum = 0;
int maxSum = 0;
//we define the following three index variables to keep track of the sum's start and end indexes.
int tempSumStartIndex = 0;
@KodeSeeker
KodeSeeker / OptimalDivideWithoutOperator.java
Created April 8, 2013 02:02
Division without using divide operator.O(Log N) approach.*Important*
ublic static int OptDiv(int divident, int divisor)
{
int quotient = 0;
java.util.List<Integer> divisors = new java.util.ArrayList<Integer>();
java.util.List<Integer> quotients = new java.util.ArrayList<Integer>();
//now we need some supporting variables to keep track of the increased/decreased divisor and base
quotients.add(1);
divisors.add(divisor);
//now we start our loop, we will use the same criterial as naive div to determine continue or stop
int currentDivisorIndex = 0;
@KodeSeeker
KodeSeeker / gist:5391134
Created April 15, 2013 20:42
You are given a linked list and a parameter k. You will have to swap values in a certain fashion, swap value of node 1 with node k, then node (k+1) with node 2k and go on doing this in the similar fashion
void weirdSwap(Node head, int k){
Node dupHead=head;
int count=0;
//1-2-3-4-5-6-7-8;k=4;4-2-3-1-5-6-7-8
if(head==null) return;
int rootVal=head.value;//rootVal=1
count++;//1
while(head.next!=null){
@KodeSeeker
KodeSeeker / BSTtoDoublyLinkedList.java
Last active December 16, 2015 08:39
Convert BST to Doubly Linked List
/** Tricky problem Needs to be looked at a few times!!! **/
class Node {
int data;
Node small;// prev
Node large;// next
public Node(int data) {
this.data = data;
small = null;
@KodeSeeker
KodeSeeker / FindOccurrences.java
Created April 22, 2013 19:43
Given a sorted array arr[] and a number x, write a function that counts the occurrences of x in arr[]. Expected time complexity is O(Logn)
/* if x is present in arr[] then returns the count of occurrences of x,
otherwise returns -1. */
int count(int arr[], int x, int n)
{
int i; // index of first occurrence of x in arr[0..n-1]
int j; // index of last occurrence of x in arr[0..n-1]
/* get the index of first occurrence of x */
@KodeSeeker
KodeSeeker / TwoArraysSameSetofIntegersAlgo.java
Created April 22, 2013 20:41
Given 2 arrays of numbers find if each of the two arrays have the same set of integers Suggest an algorithm which can run faster than NlogN.
And the solution is to traverse both the arrays and calculate the sum and product of all the numbers in each array.
If the sum and product are same then the arrays contain the same elements.
@KodeSeeker
KodeSeeker / SortZeroandOnesinOnePass.java
Created April 24, 2013 04:04
Given an array containing just 0s and 1s sort it in place in one pass.
//My version of the solution is to count the no. of zeroes(say x) and ones(say y).
//Once you do that, place x zeroes in the array followed by y 1s. This makes it O(n).
int[] performSort(int[] inputArr){
int[] outArr=new int[inputArr.length];
int zeroCount=0;
for(int i=0;i<inputArr.length-1,i++){
if(inputArr[i]==0)
zeroCount++;
@KodeSeeker
KodeSeeker / FindElementinSortedRotatedArray.java
Created April 24, 2013 05:37
Searching in an sorted and rotated array
int search( arr[], key, low, high){
mid = (low + (high-low) ) / 2;
// key not present
if(low > high)
return -1;
// key found
if(arr[mid] == key)
return mid;
// if left half is sorted.
if(arr[low] <= arr[mid]) {
@KodeSeeker
KodeSeeker / ThreeWaypartitioning.c
Created April 24, 2013 19:35
Sort an array of 0s, 1s and 2s- Not reviewed
void swap(int *a, int *b);
void sort012(int a[], int arr_size)
{
/* Basically using 3 markers and swapping through the list while walking through it
1)Set lo and mid to 0 and hi to arr_size-1;
2) while mid <hi.
if ARR[mid]=0, swap with arr[low],mid++;low++
else if arr[mid]=1, mid++;
else if arr[mid]=2, swap with arr[high], low++, high--;