Skip to content

Instantly share code, notes, and snippets.

public class RearrageArrayPositiveNegative {
int[] arrA;
public RearrageArrayPositiveNegative(int[] arrA) {
this.arrA = arrA;
}
public void divideGroups(int low, int high) {
if (low >= high)
return;
public class CountAllPaths {
int rowCount;
int colCount;
int[][] arrA;
public CountAllPaths(int arrA[][]) {
this.arrA = arrA;
rowCount = arrA.length;
colCount = arrA[0].length;
//Find the Loop
//Take two pointers, both starts from head
//move one pointer with normal speed and another with double speed
//if both pointers meets at some point, we have found the loop
//Now find the loop length
//at the point where both pointers have met, stop one pointer and keep moving the nother one
//when another pointer meets the first pointer, stop.
//keep counting number of hops, that will your loop length
//Now To break the loop
//move one pointer by the loop length
//find the length of both the linked lists say : a_len and b_len
//find the lenDiff = (a_len ~ b_len)
//traverse the longer linked list by lenDiff
//Now traverse both the lists at the same time
//check whether nodes are same, if yes then we have found the intersection point
//if we reach the end of the link lists then there is no intersection point.
public class FindIntersectionOfLinkedLists {
public LinkedListIntersection a;
public LinkedListIntersection b;
public class KthToLastElementofLL {
public int kthByRecursion(Node head, int k){
if(head==null){
return 0;
}
int i = kthByRecursion(head.next, k)+1;
if(i==k){
System.out.println(head.data);
}
public class BinaryRotate {
public int rotateBinary(int number){
int res = 0;
while(number>0){
res=res<<1;
res = res|(number & 1);
number=number>>1;
}
return res;
public class LinkedListAddtionReverseOrder {
public Node add(Node h1, Node h2){
int carry = 0;
Node newHead = null;
Node curr=null;
while(h1!=null && h2!=null){
int a = h1.data;
int b = h2.data;
int total = a+b+carry;
if(total>=10){
public class NodeHeight {
public int getNodeHeight(Node root, Node x, int height){
if(root==null) return 0;
if(root==x) return height;
//check if the node is present in the left sub tree
int level = getNodeHeight(root.left,x,height+1);
//System.out.println(level);
if(level!=0) return level;
public class BalancedTree {
public boolean checkBalance(Node root){
int result = isBalanced(root);
if(result>0){
return true;
}else{
return false;
}
}
public int isBalanced(Node root){
public class SortedArrayToBST {
public BSTNode convert(int [] arrA, int start, int end){
if(start>end){
return null;
}
int mid = (start + end)/2;
BSTNode root = new BSTNode(arrA[mid]);
root.left = convert(arrA, start, mid-1);
root.right =convert(arrA, mid+1, end);
return root;