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
| Approach 1: | |
| If tree balanced then the root is the median ! | |
| Approach 2: | |
| Store in order traversal of the tree in an array and find the median of the array | |
| Approach 3: | |
| Do an inorder traversal to find out the number of elements (say n) | |
| Do an inorder traversal again for n/2 times |
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
| inf f(int num, int car=0) | |
| { | |
| car *=10; | |
| if((num/10) == 0) | |
| return ( car + num ); | |
| else | |
| return f(num/10, car+(num%10) ); | |
| } |
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
| public static boolean isAnagram(String s1, String s2) | |
| { | |
| boolean result = false; | |
| //Basic check for the length | |
| if(s1.length()!=s2.length()) | |
| return result; | |
| char c1[]= s1.toLowerCase().toCharArray(); | |
| char c2[]= s2.toLowerCase().toCharArray(); | |
| HashMap<Character,Integer> h = new HashMap<Character,Integer>(); |
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.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| // References | |
| // - http://norvig.com/spell-correct.html | |
| // - http://raelcunha.com/spell-correct.php |
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
| public Node reverse(Node previous, Node current) { | |
| if(previous == null) | |
| return null; | |
| if(previous.equals(head)) | |
| previous.setNext(null); | |
| if(current == null) { // end of list | |
| head = previous; | |
| return head; | |
| } else { | |
| Node temp = current.getNext(); |
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
| //References- | |
| //1-http://meteatamel.wordpress.com/2012/03/21/deadlock-detection-in-java/ | |
| //2-http://stackoverflow.com/questions/1102359/programmatic-deadlock-detection-in-java | |
| private static void detectDeadlock() { | |
| ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); | |
| long[] threadIds = threadBean.findMonitorDeadlockedThreads(); | |
| int deadlockedThreads = threadIds != null? threadIds.length : 0; | |
| System.out.println("Number of deadlocked threads: " + deadlockedThreads); |
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
| int i= Integer.parseInt(hexStringinput,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
| public void quickSort(int array[]) | |
| // pre: array is full, all elements are non-null integers | |
| // post: the array is sorted in ascending order | |
| { | |
| quickSort(array, 0, array.length - 1); // quicksort all the elements in the array | |
| } | |
| public void quickSort(int array[], int start, int end) | |
| { |
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
| /*Suppose we have an input array a[] consisting of n integers, each in the range 0 to k-1. | |
| The counting-sort algorithm sorts using an auxiliary array of counters. It outputs a sorted version of a[] | |
| as an auxiliary array b[] .The idea behind counting-sort is simple: For each i on 0 to k-1 , count the number of occurrences | |
| of i in a[] and store this in c[i] . Now, after sorting, the output will look likec[0] occurrences of 0, followed by c[1] occurrences of 1, followed by c[2] occurrences of 2,... | |
| , followed by c[k-1] occurrences of k-1 . | |
| */ | |
| int[] countingSort(int[] a, int k) { |
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
| //Source code for Heap Sort | |
| public class HeapSort | |
| { | |
| private static int[] a; | |
| private static int n; | |
| private static int left; | |
| private static int right; | |
| private static int largest; | |
| public static void buildheap(int []a){ |