Skip to main content

Posts

Showing posts with the label Hackerrank 30 Days of code

Day 13: Abstract Classes Hackerrank Solution - Java

 Day 13: Abstract Classes Hackerrank Solution - Java For Explanation Check Video: Sample Input The following input from stdin is handled by the locked stub code in your editor: The Alchemist Paulo Coelho 248 Sample Output The following output is printed by your  display()  method: Title: The Alchemist Author: Paulo Coelho Price: 248 Code: import  java.util.*; abstract   class  Book {     String title;     String author;          Book(String title, String author) {          this .title = title;          this .author = author;     }           abstract   void  display(); } class  MyBook  extends  Book{      int  price;     MyBook(String title,String author, int  price){          super (title,author);          this .price = price;     }      void  display(){         System.out.println( "Title: " +title+ "\nAuthor: " +author+ "\nPrice: " +price);     } } public   class  Solution {          public   static   void  main(String[] args) {         Scanner scanner = 

Day 12: Inheritance Hackerrank Solution Java

 Day 12: Inheritance Hackerrank Solution Java For Explanation: Sample Input Heraldo Memelli 8135627 2 100 80 Sample Output Name: Memelli, Heraldo ID: 8135627 Grade: O Code: import  java.util.*; class  Person {      protected  String firstName;      protected  String lastName;      protected   int  idNumber;           // Constructor     Person(String firstName, String lastName,  int  identification){          this .firstName = firstName;          this .lastName = lastName;          this .idNumber = identification;     }           // Print person data      public   void  printPerson(){          System.out.println(                  "Name: "  + lastName +  ", "  + firstName              +    "\nID: "  + idNumber);      }       } class  Student  extends  Person{      private   int [] testScores;     Student(String firstName,String lastName, int  idNumber, int [] scores){          super (firstName,lastName,idNumber);          this .testScores = scores;     }

Day 11: 2D Arrays Hackerrank Solution Java

 Day 11: 2D Arrays Hackerrank Solution Java For Explanation Watch Video: Sample Input 1 1 1 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 2 4 4 0 0 0 0 2 0 0 0 0 1 2 4 0 Sample Output 19 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 [][] arr =  new   int [ 6 ][ 6 ];          for  ( int  i =  0 ; i <  6 ; i++) {             String[] arrRowItems = scanner.nextLine().split( " " );             scanner.skip( "(\r\n|[\n\r\u2028\u2029\u0085])?" );              for  ( int  j =  0 ; j <  6 ; j++) {                  int  arrItem = Integer.parseInt(arrRowItems[j]);                 arr[i][j] = arrItem;             }         }          int  max = Integer.MIN_VALUE;          for ( int  i= 0 ;i<=

Day 10: Binary Numbers Hackerrank Solution Java

 Day 10: Binary Numbers Hackerrank Solution Java For Explanation Watch Video: Sample Input 1 5 Sample Output 1 1 Sample Input 2 13 Sample Output 2 2 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])?" );          int  count =  0 ;          int  max =  0 ;          while (n!= 0 ){              if (n% 2 == 0 ){                 count =  0 ;             } else {                 count++;                  if (count>max){                     max = count;                 }             }             n = n/ 2 ;         }         System.out.println(max);         scanner.close();     } }

Day 9: Recursion 3 Hackerrank Solution in java

 Day 9: Recursion 3 Hackerrank Solution in java For Explanation: Sample Input 3 Sample Output 6 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 {      // Complete the factorial function below.      static   int  factorial( int  n) {          if (n<= 1 ){              return   1 ;         } else {              return  n*factorial(n- 1 );         }     }      private   static   final  Scanner scanner =  new  Scanner(System.in);      public   static   void  main(String[] args)  throws  IOException {         BufferedWriter bufferedWriter =  new  BufferedWriter( new  FileWriter(System.getenv( "OUTPUT_PATH" )));          int  n = scanner.nextInt();         scanner.skip( "(\r\n|[\n\r\u2028\u2029\u0085])?" );          int  result = factorial(n);         bufferedWriter.write(String.valueOf(result));         bufferedWriter.new

Day 8: Dictionaries and Maps Hackerrank Solution in Java

 Day 8: Dictionaries and Maps Hackerrank Solution Java For Explanation: Sample Input 3 sam 99912222 tom 11122222 harry 12299933 sam edward harry Sample Output sam=99912222 Not found harry=12299933 Code: //Complete this code or write your own from scratch import  java.util.*; import  java.io.*; class  Solution{      public   static   void  main(String []argh){         Scanner in =  new  Scanner(System.in);          int  n = in.nextInt();         HashMap<String,Integer> hm =  new  HashMap<>();          for ( int  i =  0 ; i < n; i++){             String name = in.next();              int  phone = in.nextInt();              // Write code here             hm.put(name,phone);         }          while (in.hasNext()){             String s = in.next();              // Write code here              if (hm.containsKey(s)){                 System.out.println(s+ "=" +hm.get(s));             } else {                 System.out.println( "Not found" );             }

Day 7: Arrays Hackerrank Solution Java

 Day 7: Arrays Hackerrank Solution Java For Explanation Check The Video: Sample Input 4 1 4 3 2 Sample Output 2 3 4 1 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])?" );          int [] arr =  new   int [n];         String[] arrItems = scanner.nextLine().split( " " );         scanner.skip( "(\r\n|[\n\r\u2028\u2029\u0085])?" );          for  ( int  i =  0 ; i < n; i++) {              int  arrItem = Integer.parseInt(arrItems[i]);             arr[i] = arrItem;         }          for ( int  i=n- 1 ;i>= 0 ;i--){ //3,2,1,0             System.out.print(arr[i]+ " " ); //2 3 4 1        

Day 6: Let's Review Hackerrank Solution Java

Day 6: Let's Review Hackerrank Solution Java  For Explanation Check The Video: Sample Input 2 Hacker Rank Sample Output Hce akr Rn ak Code: import  java.io.*; import  java.util.*; public   class  Solution {      public   static   void  main(String[] args) {          /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */         Scanner scn =  new  Scanner(System.in);          int  t = scn.nextInt();          for ( int  i= 0 ;i<t;i++){             String s = scn.next(); //Rank             String s_even =  "" ;             String s_odd =  "" ;              for ( int  j= 0 ;j<s.length();j++){                  if (j% 2 == 0 ){                     s_even = s_even + s.charAt(j); //Rn                 } else {                     s_odd = s_odd + s.charAt(j); //ak                 }             }             System.out.println(s_even+ " " +s_odd);         }     } }

Day 5: Loops Hackerrank Solution Java

 Day 5: Loops Hackerrank Solution Java Do Check My Video For Explanation Of Following Solution: Sample Input 2 Sample Output 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20 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])?" );          for ( int  i= 1 ;i<= 10 ;i++){             System.out.println(n+ " x " +i+ " = " +n*i);         }         scanner.close();     } }