Posts

Showing posts from July, 2014

Fibonacci series using recursion - Java

public class Fibonacci {     public static int fibonacci(int num){         if(num == 1 || num == 2)             return 1;             return fibonacci(num - 1) + fibonacci(num - 2);     }     public static void main(String[] args) {         for (int i = 1; i <= 7; i++) {             System.out.println(fibonacci(i) + " ");         }     } } o/p: 1 1 2 3 5 8 13

Factorial using recursion - Java

public class Factorial {     public static int factorial(int num){         if(num == 0 || num == 1)             return 1;         else             return num * factorial(num-1);     }     public static void main(String[] args) {         System.out.println(factorial(5));     } } o/p:  120

Array Implementation of Stack - Java

public class ArrayImplementation {     private int maxSize;     private int stack[];     private int top;     public ArrayImplementation(int maxSize) {         this.maxSize = maxSize;         this.stack = new int[maxSize];         this.top = -1;     }     public void push(int data){         if(!isFull())         stack[++top] = data;     }     public int pop(){         if(!isEmpty())             return stack[top--];         else             return -1;     }     public boolean isEmpty(){         return (top == -1);     }     public boolean isFull(){         return (top == maxSize-1);     } ...

Search in 2D array - Java

public class SearchInSorted2D {     public static boolean search(int[][] sorted, int k) {         int x = sorted.length-1;         int y = 0;         boolean found = false;         while(x>=0 && y<=sorted[0].length-1){             if(sorted[x][y] > k)                 x--;             else if(sorted[x][y] < k)                 y++;             else {                     found = true;                     break;                 }             }         return found;     }     public static v...

Rotate LinkedList - Java

public class LLNode {     private LLNode next;     private Object value;     public LLNode getNext() {         return next;     }     public LLNode setNext(LLNode next) {         this.next = next;         return this;     }     public Object getValue() {         return value;     }     public LLNode setValue(Object value) {         this.value = value;         return this;     } } public class Rotate {     public static LLNode getRotated(LLNode root, int rotatePosition){         LLNode current = null;         LLNode newStart;        if(root != null){             current = root;        }         for (in...

Middle Element of Linked List - Java

public class LLNode {     private LLNode next;     private Object value;     public LLNode getNext() {         return next;     }     public LLNode setNext(LLNode next) {         this.next = next;         return this;     }     public Object getValue() {         return value;     }     public LLNode setValue(Object value) {         this.value = value;         return this;     } } public class LLMid {     public static LLNode getMid(LLNode root){         LLNode midNode = null;         LLNode helpingNode = null;         if(root!=null){             midNode = root;             helpingNode = midNode;     ...

Longest Palindrome - Java

public class LongestPalindrome {     public static String getLongestString(String input){         int maxStrLen = Integer.MIN_VALUE;         int startIndex = Integer.MIN_VALUE;         int endIndex = Integer.MIN_VALUE;         for (int i = 1; i <= input.length(); i++) {             for (int j = 0; j < i; j++) {                 if(isPalindrome(input.substring(j,i))){                     if(input.substring(j,i).length() > maxStrLen){                         maxStrLen = input.substring(j,i).length();                         startIndex = j;                         endIndex = i;   ...

Maximum single sell profit - Java

Order O(n) public class BuySellMaxProfit { public int max ( int arr[]){ int maxProfit = arr[ 1 ] - arr[ 0 ] ; int min = arr[ 0 ] ; for ( int i = 0 ; i < arr. length ; i++){ if (arr[i] - min > maxProfit) maxProfit = arr[i] - min ; if (arr[i] < min) min = arr[i] ; } return maxProfit ; } } ------------------------------------------------------------------------------------------------------------- public class MaxSingleSellProfit {     public static Map<String,Double> getMaxSingleSellProfit(List<Double> weekValues){         Map<String,Double> result = new HashMap<String,Double>();         double maxValue = Double.MIN_VALUE;         for(int i=0; i< weekValues.size(); i++){             for (int j = 0; j < i; j++) {       ...

Reverse String using Recursion - Java

public class ReverseUsingRecursion {     public static String reverse(String s){         if(s.length() == 0)             return s;         else         return reverse(s.substring(1)) + s.charAt(0);     }     public static void main(String[] args) {         System.out.println(reverse("abc"));     } } Output: cba

String Palindrome using Recursion - Java

public class IsPalindromeUsingRecursion {     public static boolean isPal(String s){         if(s.length() == 1 || s.length() == 0)             return true;         if(s.charAt(0) != s.charAt(s.length()-1)){             return false;         }         return isPal(s.substring(1, s.length() - 1));     }     public static void main(String[] args) {         System.out.println(isPal("manam"));     } } Output: true

Mirror binay tree - Java

public class TreeNode {     private TreeNode left;     private TreeNode right;     private Object value;     public TreeNode getLeft() {         return left;     }     public TreeNode setLeft(TreeNode left) {         this.left = left;         return this;     }     public TreeNode getRight() {         return right;     }     public TreeNode setRight(TreeNode right) {         this.right = right;         return this;     }     public Object getValue() {         return value;     }     public TreeNode setValue(Object value) {         this.value = value;         return this;     } } public class Mirror {     public stat...

Number of leaf nodes - Java

public class NumberOfLeafNodes {     public static int getLeavesCount(TreeNode root){         if(root == null){             return 0;         }         if(root.getLeft() == null && root.getRight() == null)             return 1;         else         return getLeavesCount(root.getLeft()) + getLeavesCount(root.getRight());     }     public static void main(String[] args) {         TreeNode t1 = new TreeNode();         TreeNode t2 = new TreeNode();         TreeNode t3 = new TreeNode();         TreeNode t4 = new TreeNode();         TreeNode t5 = new TreeNode();         TreeNode t6 = new TreeNode();         t3.setLeft(t2).setRight(t4).setVa...

Number of nodes in a tree - Java

public class TreeNode {     private TreeNode left;     private TreeNode right;     private Object value;     public TreeNode getLeft() {         return left;     }     public TreeNode setLeft(TreeNode left) {         this.left = left;         return this;     }     public TreeNode getRight() {         return right;     }     public TreeNode setRight(TreeNode right) {         this.right = right;         return this;     }     public Object getValue() {         return value;     }     public TreeNode setValue(Object value) {         this.value = value;         return this;     } } public class NumberOfNodes {     public ...

n the last element in a linked list - Java

public class NthLastLL {     public static void main(String[] args) {         LLNode n1 = new LLNode();         LLNode n2 = new LLNode();         LLNode n3 = new LLNode();         LLNode n4 = new LLNode();         LLNode n5 = new LLNode();         LLNode n6 = new LLNode();         n1.setNext(n2).setValue(1);         n2.setNext(n3).setValue(2);         n3.setNext(n4).setValue(3);         n4.setNext(n5).setValue(4);         n5.setNext(n6).setValue(5);         n6.setNext(null).setValue(6);         LLNode nthLLNode = nthNode(n1,2);         System.out.println(nthLLNode.getValue());     }     public static LLNode nthNode(LLNode root, int nthValue){     ...