Skip to main content

Posts

Showing posts with the label 30 Days Of Code Hackerrank

Day 22: Binary Search Trees Hackerrank Solution Java

 Day 22: Binary Search Trees Hackerrank Solution Java For Explanation Check Video: Sample Input 7 3 5 2 1 4 6 7 Sample Output 3 Code: import  java.util.*; import  java.io.*; class  Node{     Node left,right;      int  data;     Node( int  data){          this .data=data;         left=right=null;     } } class  Solution{      public   static   int  getHeight(Node root){        if (root==null){            return  - 1 ;       }        int  leftHeight = getHeight(root.left);        int  rightHeight = getHeight(root.right);        return  Math.max(leftHeight,rightHeight)+ 1 ;     }      public   static  Node insert(Node root, int  data){          if (root==null){              return   new  Node(data);         }          else {             Node cur;              if (data<=root.data){                 cur=insert(root.left,data);                 root.left=cur;             }              else {                 cur=insert(root.right,data);                 root.right=cur;             }         

Day 28: RegEx, Patterns, and Intro to Databases Hackerrank Solution Java

 Day 28: RegEx, Patterns, and Intro to Databases  For Explanation Check Video: Sample Input 6 riya riya@gmail.com julia julia@julia.me julia sjulia@gmail.com julia julia@gmail.com samantha samantha@gmail.com tanya tanya@gmail.com Sample Output julia julia riya samantha tanya Code: import  java.io.*; import  java.math.*; import  java.security.*; import  java.text.*; import  java.util.*; import  java.util.concurrent.*; import  java.util.regex.*; public   class  Solution {      private   static   final  Scanner scanner =  new  Scanner(System.in);      public   static   void  main(String[] args) {          int  N = scanner.nextInt();         scanner.skip( "(\r\n|[\n\r\u2028\u2029\u0085])?" );         String reg =  ".+@gmail\\.com$" ;         Pattern p = Pattern.compile(reg);         ArrayList<String> al =  new  ArrayList<>();          for  ( int  NItr =  0 ; NItr < N; NItr++) {             String[] firstNameEmailID = scanner.nextLine().split( " "

Day 27: Testing Hackerrank Solution Java

 Day 27: Testing Hackerrank Solution Java For Explanation Check Video: Code: import  java.util.*; public   class  Solution {      public   static   int  minimum_index( int [] seq) {          if  (seq.length ==  0 ) {              throw   new  IllegalArgumentException( "Cannot get the minimum value index from an empty sequence" );         }          int  min_idx =  0 ;          for  ( int  i =  1 ; i < seq.length; ++i) {              if  (seq[i] < seq[min_idx]) {                 min_idx = i;             }         }          return  min_idx;     }      static   class  TestDataEmptyArray {          public   static   int [] get_array() {              return   new   int [ 0 ];         }     }      static   class  TestDataUniqueValues {          public   static   int [] get_array() {              return   new   int [] { 1 , 2 , 3 };         }          public   static   int  get_expected_result() {              return   0 ;         }     }      static   class  TestDataExactlyT

Day 26: Nested Logic Hackerrank Solution Java

 Day 26: Nested Logic Hackerrank Solution Java For Explanation Check Video: Sample Input STDIN Function ----- -------- 9 6 2015 day = 9, month = 6, year = 2015 (date returned) 6 6 2015 day = 6, month = 6, year = 2015 (date due) Sample Output 45 Code: import  java.io.*; import  java.util.*; public   class  Solution {      public   static   void  main(String[] args) {         Scanner scn =  new  Scanner(System.in);          int  actualDay = scn.nextInt();          int  actualMonth = scn.nextInt();          int  actualYear = scn.nextInt();          int  exceptedDay = scn.nextInt();          int  exceptedMonth = scn.nextInt();          int  exceptedYear = scn.nextInt();          int  fine;          if (actualYear>exceptedYear){             fine =  10000 ;         } else   if (actualMonth>exceptedMonth &&actualYear==exceptedYear ){             fine =  500 *(actualMonth-exceptedMonth);         } else   if (actualDay>exceptedDay && actualMonth==excep

Day 25: Running Time and Complexity Hackerrank Solution Java

 Day 25: Running Time and Complexity  For Explanation Check Video: Sample Input 3 12 5 7 Sample Output Not prime Prime Prime Code: import  java.io.*; import  java.util.*; public   class  Solution {      public   static   void  main(String[] args) {         Scanner scn =  new  Scanner(System.in);          int  t = scn.nextInt();          while (t--> 0 ){              int  n = scn.nextInt();              if (n== 1  || n< 1 ){                 System.out.println( "Not prime" );             } else {                  boolean  prime =  true ;                  for ( int  i= 2 ;i*i<=n;i++){                      if (n%i== 0 ){                         prime =  false ;                          break ;                     }                 }                  if (prime){                     System.out.println( "Prime" );                 } else {                      System.out.println( "Not prime" );                 }             }         }     } }

Day 24: More Linked Lists Hackerrank Solution Java

 Day 24: More Linked Lists Hackerrank  For Explanation Check Video: Sample Input 6 1 2 2 3 3 4 Sample Output 1 2 3 4 Code: import  java.io.*; import  java.util.*; class  Node{      int  data;     Node next;     Node( int  d){         data=d;         next=null;     }      } class  Solution {      public   static  Node removeDuplicates(Node head) {        //Write your code here        if (head==null){            return  null;       }       Node temp = head;        while (temp.next!=null){            if (temp.data!=temp.next.data){               temp = temp.next;           } else {               temp.next = temp.next.next;           }       }        return  head;     }       public   static   Node insert(Node head, int  data)      {         Node p= new  Node(data);                    if (head==null)             head=p;          else   if (head.next==null)             head.next=p;          else         {             Node start=head;              while (start.next!=null)                 sta

Day 23: BST Level-Order Traversal Hackerrank Solution Java

 Day 23: BST Level-Order Traversal For Explanation Check Video:   Sample Input 6 3 5 4 7 2 1 Sample Output 3 2 5 1 4 7 Code: import  java.util.*; import  java.io.*; class  Node{     Node left,right;      int  data;     Node( int  data){          this .data=data;         left=right=null;     } } class  Solution{ static   void  levelOrder(Node root){        Queue<Node> queue =  new  LinkedList<>();     queue.add(root);      while (queue.peek()!=null){         Node node = queue.remove();         System.out.print(node.data+ " " );          if (node.left!=null){             queue.add(node.left);         }          if (node.right!=null){             queue.add(node.right);         }     }             } public   static  Node insert(Node root, int  data){          if (root==null){              return   new  Node(data);         }          else {             Node cur;              if (data<=root.data){                 cur=insert(root.left,data);                 root.left=