Sunday, 29 March 2020

How to be more productive Work from Home

This article is based on how to be more productive while working from home. We have seen that all of the world gone into a lockdown situation due to Corona Virus(Covid-19) and most of the companies have given their employee to work from home. 
1. Choose a separate space in your house it might be your balcony, your bedroom, your hall etc.
2. Always use table and chair it would be more comfortable.
3.It's very hard to shift your homespace to workspace  because we can't be stop net surfing, browsing youtube, watch movies etc. SO we can use site blocker.
In case if you are getting bored or need to refresh you can go thru some books or some reliable website.

Website: https://medium.com/
              http://highscalability.com/
              https://www.javaworld.com/category/spring-framework/
              https://www.infoworld.com/category/java/
You can also take on any  e-learning website.

Tuesday, 25 February 2020

Check a Robot end it's path to starting point

There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.
The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.

Method 1:-
import java.util.*;
class Solution {
    public boolean check(String moves) {
        Map<Character,Integer>mp=new HashMap<>();
        mp.put('U',0);
        mp.put('D',0);
        mp.put('L',0);
        mp.put('R',0);
        for(int i=0;i<moves.length();i++)
        {
            if(mp.containsKey(moves.charAt(i)))
                mp.put(moves.charAt(i),mp.get(moves.charAt(i))+1);
        }
        if((mp.get('U').equals(mp.get('D')))&&(mp.get('L').equals(mp.get('R'))))
            return true;
        return false;
    }
public static void main(String args[])

   {
       Scanner sc=new Scanner(System.in);
       String s=sc.next();
       System.out.println(check(s)); 
   }
}

Method 2:-

import java.util.*;
class Solution {
    public static boolean check(String moves) {
        if(moves == null || moves.length() == 0)
            return true;
        int up = 0, left = 0;
        for(int i=0;i<moves.length();i++)
        {
            if(moves.charAt(i) == 'U')
                up++;
            else if(moves.charAt(i) == 'D')
                up--;
            else if(moves.charAt(i) == 'L')
                left++;
            else
                left--;
        }
        return up == 0 && left == 0;
    }

   public static void main(String args[])

   {
       Scanner sc=new Scanner(System.in);
       String s=sc.next();
       System.out.println(check(s)); 
   }

 }

Sunday, 22 December 2019

Check Duplicate Parenthesis in String

Given a balanced expression, find if it contains duplicate parenthesis or not. A set of parenthesis are duplicate if the same subexpression is surrounded by multiple parenthesis.


 import java.util.*;

class GFG {
    static String check(String s)
    {
        Stack<Character>st=new Stack<>();
        for(int i=0;i<s.length();i++)
        {
            if(s.charAt(i)==')')
            {
                int ele=0;
                while(!st.isEmpty()&&st.peek()!='(')
                {
                    st.pop();
                    ele++;
                }
                if(ele<1)
                  return "Duplicate Parenthesis";
                else
                {
                    if(!st.isEmpty())
                     st.pop();
                }
            }
            else
            st.push(s.charAt(i));
        }
        return "Not Duplicate";
    }
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        String s=sc.next();
        System.out.println(check(s));
    }
}

Saturday, 23 November 2019

Maximum depth of left node in Binary Tree

Given a Binary tree, print the maximum depth of a left node.( the node needs to be a left child ) (if the node is right child of the left child of the root node then it wont count as a left node)

import java.io.*;
class Node
{
    int data;
    Node left,right;
    Node(int data)
    {
        this.data=data;
        left=right=null;
    }
}
class TreeQuestion {
    Node root;
    static int max=0;
    void maxDepth(Node root,int direction,int depth)
    {
        if(root==null)
          return;
        if(direction==1)
        {
            max=Math.max(max,depth);
        }
        maxDepth(root.left,1,depth+1);
        maxDepth(root.right,2,depth+1);
    }
    public static void main (String[] args) {
        TreeQuestion tree=new TreeQuestion();
        tree.root=new Node(1);
        tree.root.left=new Node(2);
        tree.root.left.left=new Node(3);
        tree.root.left.left.left=new Node(4);
        tree.maxDepth(tree.root,1,0);
        System.out.println(max+1);
       
    }
}

Tuesday, 5 November 2019

Balanced Binary Tree

Given a binary tree, determine whether or not it is height-balanced. A height-balanced binary tree can be defined as one in which the heights of the two subtrees of any node never differ by more than one.

Algorithm:-

 import java.util.*;
class Node
{
    int data;
    Node left,right;
    Node(int data)
    {
        this.data=data;
        left=right=null;
    }
}
class CheckBT {
    Node root;
    boolean isBalanced(Node root)
    {
        if(root==null)
          return true;
        int l,r;
        l=height(root.left);
        r=height(root.right);
        if(Math.abs(l-r)<=1&&isBalanced(root.left)&&isBalanced(root.right))
             return true;      
      return false;       
    }
    int height(Node node)
    {
        if(node==null)
         return 0;
        return 1+Math.max(height(node.left),height(node.right));
    }
    public static void main (String[] args) {
        CheckBT tree=new CheckBT();
        tree.root=new Node(1);
        tree.root.left=new Node(2);
        tree.root.left.left=new Node(3);
        if(tree.isBalanced(tree.root))
           System.out.println("Balanced");
         else
           System.out.println("Not Balanced");
    }
}

Saturday, 12 October 2019

Absolute Path Coding Problem with Solution

Given an absolute pathname that may have . or .. as part of it, return the shortest standardized path.
For example, given "/usr/bin/../bin/./scripts/../", return "/usr/bin/".


import java.util.*;
class GFG {
    static String getAbsolutePath(String s)
    {
        String path[]=s.split("/");
        ArrayList<String>al=new ArrayList<>();
        for(int i=0;i<path.length;i++)
        {
            if(path[i].equals("."))
              continue;
            else if(path[i].equals(".."))
              {
                  if(al.size()>0)
                   al.remove(al.size()-1);
              }
              else
              al.add("/"+path[i]);
        }
        if(al.size()>0)
          al.add("/");
        s="";
       for(int i=1;i<al.size();i++)
         s+=al.get(i);
       return s;        
    }
    public static void main (String[] args) {
            Scanner sc=new Scanner(System.in);
            String s=sc.next();
            System.out.println(getAbsolutePath(s));
    }
}

Sunday, 15 September 2019

Finding sum of digits of a number until sum becomes single digit

Given a number n we need to find the sum of it's digit until it becomes single digit and expected time complexity O(1).

Input:-  12345

Output-   6


Solution 1:- Brute Force

import java.util.*;

class GFG {
    static int getSum(int n)
    {
        int sum=0;
        while(n>0||sum>9)
        {
            if(n==0)
            {
                n=sum;
                sum=0;
            }
           
                sum+=n%10;
                n=n/10;
           
        }
        return sum;
    }
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        System.out.println(getSum(n));
    }
}


Solution 2:- Best Way


import java.util.*;
class GFG {
    static int getSum(int n)
    {
        if(n==0)
          return n;
        return (n%9==0?9:n%9); 
    }
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        System.out.println(getSum(n));
    }
}

Time Complexity- O(1)