Monday, 13 July 2020

Convert Min Heap into Max Heap

public class ConvertMinHeapToMaxheap {
    public static void main(String[] args) {
        ConvertMinHeapToMaxheap convertMinHeapToMaxheap = new ConvertMinHeapToMaxheap();
        int arr[]  ={3 ,5 ,9 ,6, 8, 20, 10, 12, 18, 9};
        for(int i = arr.length/2-1;i>=0;i--){
            convertMinHeapToMaxheap.heapify(arr,arr.length,i);
        }
        System.out.println(Arrays.toString(arr));
    }
    void heapify(int[] arr, int n, int i){
        int l = 2*i+1;
        int r = 2*i+2;
        int largest = i;
        if(l<n){
            if(arr[l]>arr[largest]){
                largest = l;
            }
        }

        if(r<n){
            if(arr[r]>arr[largest]){
                largest = r;
            }
        }

        if(largest!=i){
            int temp = arr[largest];
            arr[largest] = arr[i];
            arr[i] = temp;
            heapify(arr,n,largest);
        }
    }
}

output: [20, 18, 10, 12, 9, 9, 3, 5, 6, 8]

Sunday, 12 July 2020

Find kth min element in array in java using MinHeap

public class findKthsmallestElement {
    public static void main(String[] args) {
        int[] arr = {4,1,7,2,3,8,10};
        findKthsmallestElement findKthsmallestElement = new findKthsmallestElement();
        findKthsmallestElement.findKth(arr,3);
    }

    void findKth(int[] arr,int kth){
        if(kth<=0 || kth>arr.length){
            System.out.println("no element found");
            return;
        }
        for(int i = (arr.length/2)-1;i>=0;i--){
            heapify(arr,arr.length,i);
        }

        System.out.println(Arrays.toString(arr));
        
        int length = arr.length;


//execute loop k times from end index, move root element to ith element and heapify again to maintain min heap
        for(int i= length-1;i>=length-kth ;i--){
            swap(arr,i,0);
            heapify(arr,i,0);

        }
        System.out.println(+kth+" kth:"+arr[arr.length-kth]);
        System.out.println(Arrays.toString(arr));
        

    }

    void heapify(int[] arr, int n,int i){
        int l = 2*i+1;
        int r = 2*i+2;
        int smallest = i;
        if(l<n){
            if(arr[l]<arr[smallest]){
                smallest = l;
            }
        }
        if(r<n){
            if(arr[r]<arr[smallest]){
                smallest = r;
            }
        }
        if(smallest!=i){

            swap(arr,i,smallest);
            heapify(arr,n,smallest);
        }

    }

    void swap(int[] arr, int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

output: 
[1, 2, 7, 4, 3, 8, 10]
3 kth:3
[4, 8, 7, 10, 3, 2, 1]

Thursday, 9 July 2020

Arranging Coins : LeetCode

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.
Given n, find the total number of full staircase rows that can be formed.
n is a non-negative integer and fits within the range of a 32-bit signed integer.
Example 1:
n = 5

The coins can form the following rows:
¤
¤ ¤
¤ ¤

Because the 3rd row is incomplete, we return 2.
Example 2:
n = 8

The coins can form the following rows:
¤
¤ ¤
¤ ¤ ¤
¤ ¤

Because the 4th row is incomplete, we return 3.
Solution:
class Solution {
    public int arrangeCoins(int n) {
        long start =0;
        long end = n;
        while(start<=end){
            long k = start+(end-start)/2;
            long total = k*(k+1)/2;
            if(total==n){
                return (int)k;
            }
            if(n<total){
                end = k-1;
            }else{
                start = k+1;
            }
        }
        return (int)end;
    }
}

Plus One : LeetCode

Given a non-empty array of digits representing a non-negative integer, increment one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1:
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Example 2:
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
Solution:
class Solution {
    public int[] plusOne(int[] digits) {
       int length = digits.length;
        int i=length -1;
        while(i>=0){
            if(digits[i]!=9){
                digits[i] = digits[i]+1;
                return digits;
            }
             digits[i]=0;
            i--;
            
        }
        int[] finalArr = new int[length+1];
        finalArr[0]=1;
        return finalArr;
    }
}

Friday, 22 May 2020

check Array elements are consecutive


public class CheckArrayIsConsecutive {

  static boolean checkConsecutive(int[] arr){

        int min = Integer.MAX_VALUE;
        for(int i=0;i<arr.length;i++){
            if(min>arr[i]){
                min = arr[i];
            }
        }

        for(int i=0;i<arr.length;i++){
            if(Math.abs(arr[i])-min>=arr.length){ //means array size small than required consecutive element
                return false;
            }
            if(arr[Math.abs(arr[i])-min]<0){ //if we want to store negative at index there already a negative value
                return false;
            }

            arr[Math.abs(arr[i])-min] = -   arr[Math.abs(arr[i])-min];

        }


        return true;
    }
    public static void main(String[] args) {
        int[] arr = {77,78,76,75,72,73,74};
        boolean bool = checkConsecutive(arr);
        System.out.println(bool);
    }
}


output: true


if input below then output false:
 int[] arr = {77,78,76,75,72,73,79};

ref: https://github.com/mission-peace/interview/blob/master/src/com/interview/array/CheckIfArrayElementsAreConsecutive.java

Friday, 1 May 2020

Kruskal Minimum spinning tree

Kruskal MST is find a tree with minimum weight and connect all the vertex of graph.

A minimum spanning tree has (V – 1) edges where V is the number of vertices in the given graph.

Steps:
1.Get all edges of graph
2.sort all edges by weight in ascending order
3.pick all edges in loop one by one
  a.get vertex of edge
  b.find root of both vertex using disjoint set algo
  c.if both vertex root is same then continue otherwise
  d.add into result list and do union of both vertex using disjoint set.


public class KruskalMST {

   public static class EdgeComparator implements Comparator<Graph<Integer>.Edge<Integer>> {

       @Override
       public int compare(Graph<Integer>.Edge<Integer> t1, Graph<Integer>.Edge<Integer> t2) {
           return t2.weight >= t1.weight ? -1 : 0;
       }
   }
     public List<Graph<Integer>.Edge<Integer>> mst(List<Graph<Integer>.Edge<Integer>> list, Collection<Graph<Integer>.Vertex<Integer>> vertexList){
           Collections.sort(list,new EdgeComparator());
           DisjointSet disjointSet = new DisjointSet();
           for(Graph<Integer>.Vertex<Integer> vertex: vertexList){
               disjointSet.make((int)vertex.id);
           }
           List<Graph<Integer>.Edge<Integer>> result = new ArrayList<>();
           for(Graph<Integer>.Edge<Integer> edge: list){
             int root1 = disjointSet.findSet((int)edge.v1.id);
             int root2 = disjointSet.findSet((int)edge.v2.id);
             if(root1==root2){
                 continue;
             }
             else{
                 result.add(edge);
                 disjointSet.union((int)edge.v1.id,(int)edge.v2.id);
             }
           }
           return result;
       }

    public static void main(String[] args) {
        Graph<Integer> graph = new Graph<Integer>(false);
        graph.addEdge(1, 2, 4);
        graph.addEdge(1, 3, 1);
        graph.addEdge(2, 5, 1);
        graph.addEdge(2, 6, 3);
        graph.addEdge(2, 4, 2);
        graph.addEdge(6, 5, 2);
        graph.addEdge(6, 4, 3);
        graph.addEdge(4, 7, 2);
        graph.addEdge(3, 4, 5);
        graph.addEdge(3, 7, 8);
        System.out.println(graph);

        List<Graph<Integer>.Edge<Integer>> list = graph.getAllEdges();

        KruskalMST kruskalMST = new KruskalMST();
        list = kruskalMST.mst(list,graph.getAllVertex());


         for(Graph<Integer>.Edge<Integer> e: list){
            System.out.print("vertex:"+e.getV1().id);
            System.out.println(" vertex: "+e.getV2().id);
            System.out.println("weight:"+e.getWeight());
            System.out.println("*******");
        }
    }
}


output:
vertex:2 vertex: 5
weight:1
*******
vertex:1 vertex: 3
weight:1
*******
vertex:4 vertex: 7
weight:2
*******
vertex:6 vertex: 5
weight:2
*******
vertex:2 vertex: 4
weight:2
*******
vertex:1 vertex: 2
weight:4
*******


Graph Class:

public class Graph<T> {
    private List<Edge<T>> allEdges;
    private Map<Long,Vertex<T>> allVertex;
    private boolean isDirected;

    Graph(boolean isDirected){
        this.isDirected = isDirected;
        allEdges = new ArrayList<>();
        allVertex = new HashMap<>();
    }

    void addEdge(long v1, long v2, int weight){
        Vertex<T> vertex1 = null;
        if(allVertex.containsKey(v1)){
            vertex1 = allVertex.get(v1);
        }else{
            vertex1 = new Vertex<>( v1);
            allVertex.put(v1,vertex1);
        }
        Vertex<T> vertex2 = null;
        if(allVertex.containsKey(v2)){
            vertex2 = allVertex.get(v2);
        }else{
            vertex2 = new Vertex<>( v2);
            allVertex.put(v2,vertex2);
        }

        Edge edge = new Edge(vertex1,vertex2,isDirected,weight);
        if(!allEdges.contains(edge)){
            allEdges.add(edge);
        }
        vertex1.addAdjecent(edge);
        if(!isDirected){
            vertex2.addAdjecent(edge);
        }

    }
    public List<Edge<T>> getAllEdges(){
        return allEdges;
    }

    public Collection<Vertex<T>> getAllVertex(){
        return allVertex.values();
    }

    class Edge<T>{
        Vertex<T> v1;
        Vertex<T> v2;
        boolean isDirected;
        int weight;
        Edge(Vertex v1, Vertex v2, boolean isDirected, int weight){
            this.v1 = v1;
            this.v2 = v2;
            this.isDirected = isDirected;
            this.weight = weight;
        }

        public Vertex<T> getV1() {
            return v1;
        }

        public void setV1(Vertex<T> v1) {
            this.v1 = v1;
        }

        public Vertex<T> getV2() {
            return v2;
        }

        public void setV2(Vertex<T> v2) {
            this.v2 = v2;
        }

        public boolean isDirected() {
            return isDirected;
        }

        public void setDirected(boolean directed) {
            isDirected = directed;
        }

        public int getWeight() {
            return weight;
        }

        public void setWeight(int weight) {
            this.weight = weight;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Edge<?> edge = (Edge<?>) o;
            return Objects.equals(v1, edge.v1) &&
                    Objects.equals(v2, edge.v2);
        }

        @Override
        public int hashCode() {
            return Objects.hash(v1, v2);
        }
    }
    class Vertex<T>{
        long id;
        T data;
        List<Edge<T>> allAdjecent;

        void addAdjecent(Edge<T> edge){
            allAdjecent.add(edge);
        }

        Vertex(long id){
            this.id = id;
            allAdjecent = new ArrayList<>();
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Vertex<?> vertex = (Vertex<?>) o;
            return id == vertex.id;
        }

        @Override
        public int hashCode() {
            return Objects.hash(id);
        }
    }
}

DisjointSet :

public class DisjointSet {

    Map<Integer,Node> map = new HashMap<>();
    class Node{
        int rank;
        int data;
        Node parent;
    }

    Node make(int val){
        Node node = new Node();
        node.data = val;
        node.rank = 0;
        node.parent = node;
        map.put(val,node);
        return node;
    }
    boolean union(int n1, int n2){
        Node node1 = map.get(n1);
        Node node2 = map.get(n2);

        Node parent1 = findSet(node1);
        Node parent2 = findSet(node2);
        if(parent1.data==parent2.data){
            return false;
        }
        if(parent1.rank>=parent2.rank){
            parent1.rank = (parent1.rank==parent2.rank)? parent1.rank+1:parent1.rank;
            parent2.parent = parent1;
        }else {
            parent1.parent = parent2;
        }
        return true;
    }
    Node findSet(Node node){
        Node parent = node.parent;
        if(node==parent){
            return parent;
        }else{
            node.parent = findSet(node.parent);
        }
        return node.parent;
    }

    int findSet(int val){
        return findSet(map.get(val)).data;
    }

    public static void main(String[] args) {
        DisjointSet ds= new DisjointSet();
        ds.make(1);
        ds.make(2);
        ds.make(3);
        ds.make(4);
        ds.make(5);
        ds.make(6);
        ds.make(7);

        ds.union(1, 2);
        ds.union(2, 3);
        ds.union(4, 5);
        ds.union(6, 7);
        ds.union(5, 6);
        ds.union(3, 7);

        System.out.println(ds.findSet(1));
        System.out.println(ds.findSet(2));
        System.out.println(ds.findSet(3));
        System.out.println(ds.findSet(4));
        System.out.println(ds.findSet(5));
        System.out.println(ds.findSet(6));
        System.out.println(ds.findSet(7));

    }
}

output:

How to use Log4j and MDC in Java Spring Boot Application?

links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...