Posts

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;   ...