Skip to main content

Posts

Showing posts with the label Hackerrank 30 Days of code

Day 29: Bitwise AND Hackerrank Solution Java

 Day 29: Bitwise AND  For Explanation Check Video: Sample Input STDIN Function ----- -------- 3 T = 3 5 2 N = 5, K = 2 8 5 N = 8, K = 5 2 2 N = 8, K = 5 Sample Output 1 4 0 Code: import  java.io.*; import  java.math.*; import  java.security.*; import  java.text.*; import  java.util.*; import  java.util.concurrent.*; import  java.util.function.*; import  java.util.regex.*; import  java.util.stream.*; import   static  java.util.stream.Collectors.joining; import   static  java.util.stream.Collectors.toList; class  Result {      /*      * Complete the 'bitwiseAnd' function below.      *      * The function is expected to return an INTEGER.      * The function accepts following parameters:      *  1. INTEGER N      *  2. INTEGER K      */      public   static   int  bitwiseAnd( int  N,  int  K) {      // Write your code here      int  res =  0 ;      for ( int  i= 1 ;i<=N;i++){ //1          for ( int  j=i+ 1 ;j<=N;j++){              int  ans = i&j; //

Day 21: Generics Hackerrenk Solution Java

 Day 21: Generics Hackerrenk Solution Java For Explanation Watch Video: Code: import  java.util.*; class  Printer <T> {      /**     *    Method Name: printArray     *    Print each element of the generic array on a new line. Do not return anything.     *    @param A generic array     **/      void  printArray(T arr[]){          for ( int  i= 0 ;i<arr.length;i++){             System.out.println(arr[i]);         }     }      // Write your code here } public   class  Generics {           public   static   void  main(String args[]){         Scanner scanner =  new  Scanner(System.in);          int  n = scanner.nextInt();         Integer[] intArray =  new  Integer[n];          for  ( int  i =  0 ; i < n; i++) {             intArray[i] = scanner.nextInt();         }         n = scanner.nextInt();         String[] stringArray =  new  String[n];          for  ( int  i =  0 ; i < n; i++) {             stringArray[i] = scanner.next();         }                  Printer<Integer&

Day 20: Sorting Hackerrank Solution Java

 Day 20: Sorting Hackerrank Solution Java For Explanation Watch Video: Sample Input 0 3 1 2 3 Sample Output 0 Array is sorted in 0 swaps. First Element: 1 Last Element: 3 Code: import  java.io.*; import  java.util.*; import  java.text.*; import  java.math.*; import  java.util.regex.*; public   class  Solution {      public   static   void  main(String[] args) {         Scanner in =  new  Scanner(System.in);          int  n = in.nextInt();          int [] a =  new   int [n];          for ( int  a_i= 0 ; a_i < n; a_i++){             a[a_i] = in.nextInt();         }          int  numberOfSwaps =  0 ;          for  ( int  i =  0 ; i < n; i++) {                   for  ( int  j =  0 ; j < n -  1 ; j++) {                  // Swap adjacent elements if they are in decreasing order                  if  (a[j] > a[j +  1 ]) {                      int  temp = a[j];                     a[j] = a[j+ 1 ];                     a[j+ 1 ] = temp;                     numberOfSwaps++;         

Day 19: Interfaces Hackerrank Solution Java

 Day 19: Interfaces Hackerrank Solution Java For Explanation Check Video: Sample Input 6 Sample Output I implemented: AdvancedArithmetic 12 Code: import  java.io.*; import  java.util.*; interface  AdvancedArithmetic{     int  divisorSum( int  n); } class  Calculator  implements  AdvancedArithmetic {      public   int  divisorSum( int  n) { //6          int  sum =  0 ;          for ( int  i= 1 ;i<=n;i++){              if (n%i== 0 ){                 sum = sum + i;             }         }          return  sum;     } } class  Solution {      public   static   void  main(String[] args) {         Scanner scan =  new  Scanner(System.in);          int  n = scan.nextInt();         scan.close();                  AdvancedArithmetic myCalculator =  new  Calculator();           int  sum = myCalculator.divisorSum(n);         System.out.println( "I implemented: "  + myCalculator.getClass().getInterfaces()[ 0 ].getName() );         System.out.println(sum);     } }

Day 18: Queues and Stacks Hackerrank Solution Java

 Day 18: Queues and Stacks Hackerrank Solution Java For Explanation Check Video: Sample Input racecar Sample Output The word, racecar, is a palindrome. Code: import  java.io.*; import  java.util.*; public   class  Solution {      // Write your code here.//racecar     Stack<Character> st =  new  Stack<>();     Queue<Character> q =  new  LinkedList<>();      void  pushCharacter( char  ch){         st.push(ch); //race     }      void  enqueueCharacter( char  ch){         q.add(ch); //ecar     }      char  popCharacter(){          return  st.pop(); //c     }      char  dequeueCharacter(){          return  q.remove(); //c     }      public   static   void  main(String[] args) {         Scanner scan =  new  Scanner(System.in);         String input = scan.nextLine();         scan.close();          // Convert input String to an array of characters:          char [] s = input.toCharArray();          // Create a Solution object:         Solution p =  new  Solution();     

Day 17: More Exceptions Hackerrank Solution Java

 Day 17: More Exceptions Hackerrank Solution Java For Explanation Check Video: Sample Input 4 3 5 2 4 -1 -2 -1 3 Sample Output 243 16 n and p should be non-negative n and p should be non-negative Code: import  java.util.*; import  java.io.*; //Write your code here class  Calculator{      int  power( int  n, int  p) throws  Exception{          if (n< 0  || p< 0 ){              throw   new  Exception( "n and p should be non-negative" );         } else {              return  ( int )Math.pow(n,p);         }     } } class  Solution{      public   static   void  main(String[] args) {              Scanner in =  new  Scanner(System.in);          int  t = in.nextInt();          while  (t-- >  0 ) {                       int  n = in.nextInt();              int  p = in.nextInt();             Calculator myCalculator =  new  Calculator();              try  {                  int  ans = myCalculator.power(n, p);                 System.out.println(ans);             }              c

Day 16: Exceptions - String to Integer Hackerrank Solution Java

 Day 16: Exceptions - String to Integer Hackerrank Solution Java For Explanation Check Video: Sample Input 0 3 Sample Output 0 3 Sample Input 1 za Sample Output 1 Bad String Code: import  java.io.*; import  java.util.*; import  java.text.*; import  java.math.*; import  java.util.regex.*; public   class  Solution {      public   static   void  main(String[] args) {         Scanner in =  new  Scanner(System.in);         String S = in.next();          try {              int  i = Integer.parseInt(S);             System.out.println(i);         } catch (Exception e){             System.out.println( "Bad String" );         }     } }

Day 15: Linked List Hackerrank Solution Java

 Day 15: Linked List Hackerrank Solution Java For Explanation Check Video: Sample Input STDIN Function ----- -------- 4 T = 4 2 first data = 2 3 4 1 fourth data = 1 Sample Output 2 3 4 1 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 insert(Node head, int  data) {         Node n =  new  Node(data);          if (head==null){              return  n;         } else {             Node temp = head;              while (temp.next!=null){                 temp = temp.next;             }             temp.next = n;              return  head;         }     }      public   static   void  display(Node head) {         Node start = head;          while (start != null) {             System.out.print(start.data +  " " );             start = start.next;         }     }      public   static   void  main(String args[]) {

Day 14: Scope Hackerrank Solution Java

 Day 14: Scope Hackerrank Solution Java For Explanation Check Video: Sample Input STDIN Function ----- -------- 3 __elements[] size N = 3 1 2 5 __elements = [1, 2, 5] Sample Output 4 Code: import  java.io.*; import  java.util.*; import  java.text.*; import  java.math.*; import  java.util.regex.*; class  Difference {      private   int [] elements;      public   int  maximumDifference;      // Add your code here     Difference( int [] arr){         elements = arr;     }      void  computeDifference(){         Arrays.sort(elements);         maximumDifference = elements[elements.length- 1 ]-elements[ 0 ];     } }  // End of Difference class public   class  Solution {      public   static   void  main(String[] args) {         Scanner sc =  new  Scanner(System.in);          int  n = sc.nextInt();          int [] a =  new   int [n];          for  ( int  i =  0 ; i < n; i++) {             a[i] = sc.nextInt();         }         sc.close();         Difference difference =  new  D