This file contains 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
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); |
This file contains 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
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; |
This file contains 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
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; |
This file contains 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
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){ |
This file contains 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
/** 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; |
This file contains 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
/* 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 */ |
This file contains 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
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. |
This file contains 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
//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++; | |
This file contains 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
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]) { |
This file contains 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
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--; |