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 pa...

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 ...

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();      ...

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;             }         }        ...

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    ...

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(St...

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){  ...

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;   ...

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 ];     } }...