Inserting a Node in Linked List


public Node InsertInList(Node head,Node nodeToInsert,int position){
		if (head==null) return nodeToInsert;
		int size=length(head);
		if(position>size+1||position<1){
			System.out.println("Position invalid. Valid inputs are from 1 to"+(size+1));
			return head;
		}
		if (position==1){
			nodeToInsert.setNext(head);
			return nodeToInsert;
		}else{
			Node previousNode=head;
			int count=1;
			while(count
	

Find Maximum element in a Binary Tree


public int findMax(Node root) {
		int rootValue, left, right;
		int max = Integer.MIN_VALUE;

		if (root != null) {
			rootValue = root.getData();
			left =findMax(root.getLeft());
			right=findMax(root.getRight());
			if (left>right) max=left;
			else max=right;
			if(rootValue>max){
				max=rootValue;
			}
		}
		return max;
	}


Class

class Node {
	int data;
	Node left;
	Node right;

	public int getData() {
		return data;
	}

	public void setData(int data) {
		this.data = data;
	}

	public Node getLeft() {
		return left;
	}

	public void setLeft(Node left) {
		this.left = left;
	}

	public Node getRight() {
		return right;
	}

	public void setRight(Node right) {
		this.right = right;
	}
}

Kadane’s Algorithm – Maximum sum subarray

public class KadaneAlgo {
	public static void main(String args[]){
		int[] inputArray={-1, 3, -5, 4, 6, -1, 2, -7, 13, -3};
		maxSubArray(inputArray);
		
	}

	public static void maxSubArray(int[] inputArray) {
		int cumulativeSum = 0;
		int maxSum = Integer.MIN_VALUE;
		int startIndex = 0;
		int endIndex = 0;
		int startIndexUntilNow = 0;

		for (int currentIndex = 0; currentIndex < inputArray.length; currentIndex++) {
			cumulativeSum += inputArray[currentIndex];
			if (cumulativeSum > maxSum) {
				maxSum = cumulativeSum;
				startIndex = startIndexUntilNow;
				endIndex = currentIndex;
			}
                        if (cumulativeSum < 0) {
				startIndexUntilNow = currentIndex+1;
				cumulativeSum = 0;
			}
		}
		System.out.println("MaxSum : "+maxSum);
		System.out.println("Start Index : "+startIndex);
		System.out.println("End Index : "+endIndex);
	}

}


Towers of Hanoi

Towers of Hanoi

The minimum number of moves required to solve a Tower of Hanoi puzzle is 2^n – 1, where n is the number of disks.

Recursion

import java.util.Scanner;

public class TowersOfHanoi {
	private static int move = 1;

	public static void main(String args[]) {
		System.out.println("Enter number of Disks:");
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		solve(x, 'A', 'B', 'C');
	}

	public static void solve(int n, char srcPeg, char destPeg, char auxPeg) {
		if (n == 1) {
			System.out.println(move + ": Moved Disk from " + srcPeg + " to "
					+ destPeg);
			move++;
			return;
		}
		solve(n - 1, srcPeg, auxPeg, destPeg);

		System.out.println(move + ": Moved Disk from " + srcPeg + " to "
				+ destPeg);
		move++;
		solve(n - 1, auxPeg, destPeg, srcPeg);
	}
}


Recursion – Finding Factorial

Recursive
import java.util.Scanner;

public class Factorial {
	public static void main(String args[]) {
		System.out.println("Enter a number :");
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		System.out.println("Factorial of " + x + " is : " + factorial(x));
	}

	public static int factorial(int x) {
		if (x == 0 || x == 1) {
			return 1;
		} else {
			return x * factorial(x - 1);
		}
	}
}

Iterative

import java.util.Scanner;

public class IterativeFactorial {
	public static void main(String args[]){
		System.out.println("Enter a number :");
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		System.out.println("Factorial of " + x + " is : " + factorial(x));
	}
	public static int factorial(int x){
		int product=1;
		if (x == 0 || x == 1) {
			return product;
		}else{
			for(int i=1;i<=x;i++){
				product=product*i;
			}
			return product;
		}
	}
}