Posts

Showing posts from April, 2021

Array Operation

class ArrayOperations {     int[] array;     static int size;     void swap(int a, int b) {         array[a] = array[a] + array[b];         array[b] = array[a] - array[b];         array[a] = array[a] - array[b];     }     void displayArray() {         Arrays.stream(array).forEach(num -> System.out.print(num + " "));         System.out.println();     }     void displaySortedArray() {         for(int i = 0; i <= size; i++) {             System.out.print(array[i] + " ");         }         System.out.println();     }     int get(int index) {         if(index >= 0 && index < array.length) {             return array[index];         }         return -1;     }     void set(int index, int data) {         if(index >= 0 && index < array.length) {             array[index] = data;         }     }     int max() {         int max = array[0];         for(int i = 1; i < array.length; i++) {             if(array[i] > max) {                 max

Binary Search Java Program

Image
 Code :  class BinarySearch {     private int[] array;     int binarySearch_iterative (int low, int high, int key) {         while (low <= high){             int mid = (low+high)/2;             if(array[mid] == key) {                 return mid; // serach successful             } else if(key < array[mid]) {                 high = mid - 1;             } else {                 low = mid + 1;             }         }         return -1; // search unsuccessful     }     int binarySearch_recursive (int low, int high, int key) {         if(low <= high) {             int mid = (low+high)/2;             if(array[mid] == key) {                 return mid; // serach successful             } else if(key < array[mid]) {                 return binarySearch_recursive (low, mid-1, key); // search in left sub array             }              return binarySearch_recursive (mid+1, high, key); // search in right sub array         }         return -1;     } public static void main (String[] a

Linear Search Java program

 class LinearSearch {     int[] array;     // Linear search : Time Complexity O(n) and Space Complexity O(1)     int search(int key) {         for(int i = 0; i < array.length; i++) {             if(array[i] == key) {                 return i;             }         }         return -1;     }     // Transposition Method : Time Complexity O(n) and Space Complexity O(1)     int search_improved_transposition (int key) {         for(int i = 0; i < array.length; i++) {             if(array[i] == key) {                 if(i > 0) {                     int temp = array[i-1];                     array[i-1] = array[i];                     array[i] = temp;                     return i-1;                 }                 return i;             }         }         return -1;     }     // Move to head Method : Time Complexity O(n) and Space Complexity O(1)     int search_improved_moveToHead (int key) {         for(int i = 0; i < array.length; i++) {             if(array[i] == key) {      

Searching and Sorting

 1. Linear Search 2. Binary Search

Java program for Tower of Hanoi

 class TowerOfHanoi {     // Time Complexity O(n) and Space Complexity O(1)     void tower_recursive(int numberOfDisks, int sourceTower, int additionalTower, int destinationTower) {         if(numberOfDisks > 0){             tower_recursive(numberOfDisks - 1, sourceTower, destinationTower, additionalTower);             System.out.println("Moving disk from tower " + sourceTower + " to " + destinationTower);             tower_recursive(numberOfDisks - 1, additionalTower, sourceTower, destinationTower);         }     } public static void main (String[] args) {     TowerOfHanoi obj = new TowerOfHanoi();     obj.tower_recursive(3, 1, 2, 3); } }

Java program for combination | nCr

Image
  class NCR {     // Time Complexity O(n) and Space Complexity O(1)     int nCr (int n, int r) {         // Formula n!/(r!*(n-r)!)         int factorialN = factorial(n);         int factorialR = factorial(r);         int factorialNMinusR = factorial(n-r);         return factorialN / (factorialR * factorialNMinusR);     }         // Time Complexity O(n) and Space Complexity O(n)     int nCr_pascalTriangle (int n, int r) {         if(r == 0 || n == r) {             return 1;         }         return nCr_pascalTriangle(n-1, r-1) + nCr_pascalTriangle(n-1, r);     }     // Time Complexity O(n) and Space Complexity O(1)     int factorial (int n) {         if(n <= 1) { //To return 1st and 2nd term             return 1;         }         int result = 1;         for(int i = 1; i <= n; i++) {             result *= i;         }         return result;     } public static void main (String[] args) {     NCR obj = new NCR();     int n = 4, r = 2;     System.out.println(obj.nCr(n, r));

Java program for Fibonacci Series

 class FibonacciSeries {     int[] dp;     // Time Complexity O(2^n) and Space Complexity O(n)     int fibonacci_recursive (int n) {         if(n <= 1) { //To return 1st and 2nd term             return n;         }         return fibonacci_recursive(n-2) + fibonacci_recursive(n-1);     }     // Time Complexity O(n) and Space Complexity O(n)     int fibonacci_recursive_memoization (int n) {         if(n <= 1) { //To return 1st and 2nd term             return n;         }         if(dp[n-2] == -1) {             dp[n-2] = fibonacci_recursive(n-2);         }         if(dp[n-1] == -1) {             dp[n-1] = fibonacci_recursive(n-1);         }         dp[n] = dp[n-2] + dp[n-1];                  return dp[n];     }     // Time Complexity O(n) and Space Complexity O(1)     int fibonacci_iterative (int n) {         if(n <= 1) { //To return 1st and 2nd term             return n;         }         int term1 = 0, term2 = 1, sum = n;         for(int i = 2; i <=n; i++) {             sum

Java program for Taylor Series

Image
class TaylorSeries {     int powerOfX;     int factorialOfN;     double result;     // Time Complexity O(n) and Space Complexity O(n)     double taylor_recursive (int x, int n) {         if(x <= 0 || n < 0) { // Corner case             return -1;         }         if(n == 0) {              return 1;         }         double result = taylor_recursive(x, n - 1);         powerOfX *= x;         factorialOfN *= n;         return (double)powerOfX / factorialOfN + result;     }          // Time Complexity O(n) and Space Complexity O(1)     double taylor_iterative (int x, int n) {         if(x <= 0 || n < 0) { // Corner case             return -1;         }         if(n == 0) {              return 1;         }         double result = 0d;         for(int i = 1; i <= n; i++) {             powerOfX *= x;             factorialOfN *= n;             result =+ (double)powerOfX / factorialOfN;         }         return result;     }          // Time Complexity O(n) and Space Complexity O

Java program to calculate the power of a number | exponential (m^n)

 // Time Complexity O(n) and Space Complexity O(n) int pow_recursive (int m, int n) {     if(n == 0) {         return 1;     }     return pow_recursive(m, n - 1) * m; } // Time Complexity O(n/2) and Space Complexity O(n) int pow_recursive_improved(int m, int n) {     if(n < 0) {        return 0;     }     if(n == 0) {         return 1;     }     if(n % 2 == 0) { // Even         return pow_recursive_improved(m*m, n/2);     }     return m * pow_recursive(m*m, (n - 1) / 2); } // Time Complexity O(n) and Space Complexity O(1) int pow_iterative(int m, int n) {     if(n < 0) {         return 0;     }     if(n == 0) {         return 1;     }     int result = 1;     for(int i = 0; i < n; i++) {        result *= m;     }     return result; }

Write program to calculate factorial of a number

 0! = 1 1! = 1 //Time complexity O(n) and Space complexity O(n) int factorial_recursive(int n) {     if(n < 0) {          return 0;     }     if(n == 0) {         return 1;     }     return factorial_recursive(n - 1) * n; }    //Time complexity O(n) and Space complexity O(1) int factorial_iterative(int n) {     if(n < 0) {         return 0;     }     if(n == 0) {         return 1;     }     int result = 1;     for(int i = 1; i <= n; i++) {         result *= i;     }     return result; }

Sum of natural numbers

//Time complexity O(n) and Space complexity O(n) int sum_recursive(int n) {     if(n == 0) {         return 0;     }     return sum_recursive(n - 1) + n; } //Time complexity O(n) and Space complexity O(1) int sum_iterative(int n) {     int sum = 0;     for(int i = 1; i <= n; i++) {         sum += i;     }     return sum; }      //Time complexity O(1) and Space complexity O(1) int sum_usingFormula(int n) {     return n * (n + 1) / 2; }

Types of Recursion

 1. Tail Recursion : Function call itself is the last statement. Operations perform at calling time of the recursion. // Time complexity O(n) & Space complexity O(n) void tail_recursion(int n) {          if(n > 0) {             System.out.print(n + " ");             tail_recursion(n - 1);         }     } // Time complexity O(n) & Space complexity O(1) void tail_recursion_iterative(int n) {         System.out.println();         while(n > 0) {             System.out.print(n + " ");             n--;         }     } 2. Head Recursion : Function call itself is the first statement. Operations perform at returning time of the recursion. //Time complexity O(n) & Space complexity O(n) void head_recursion(int n) {         if(n > 0) {             head_recursion(n - 1);             System.out.print(n + " ");         }     } // Time complexity O(n) & Space complexity O(1) void head_recursion_iterative(int n) {         System.out.println();  

Data Types in Java

Image
 

What is KB, MB, GB, TB - Cheat sheet

Image
 

What is Data Structures

 A program is a set of instructions that perform operations on data. When program is dealing with data, the way you organising the data in the main (RAM) memory is called data structures. Arrangement of collection of data items so that operations on the data can be done efficiently. More Elaboration : When you start a program that deal with some data stored in any storage (Hard disk, cloud storage etc), data is loaded into the main memory. The way data is organised into the main memory for performing the operations by the program is called data structures. Data structure is part of the running program. Physical Data Structures : Array (They had collection of contagious memory locations), LinkedList(Dynamic nodes connected to each others). Logical Data Structures : These can be created using physical data structures. Stack, Queue, Tree, Graph, Hash Table

MultiStage Graph

Image
Explanation Video Useful for representing resource allocation problem. Time Complexity = O(n^2) Java program /* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* MultiStageGraph to find rout of shortest path. This program can be     used to print shortest rout or sum of shortest path. */ class MultiStageGraph {     // Find shortest path     private static String findShortestPath() {         // Get input graph         int[][] graph = populateGraph();                  // Size of the graph         int size = graph.length;                  // Store the result         StringBuilder result = new StringBuilder("1");                  // Store cost of choosing particular rout         int[] cost = new int[size];                  // Store distance         int[] distance = new int[size];                  // Store rout path         int[] path = new int[size];                  // Assuming the cost to reach last node from the