Skip to main content

Posts

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

Reverse a string using Stack in java

 Reverse a string using Stack in java Explanation Of code: Code: import java.util.Stack; class Test{ public static void main(String[] args){ String s = "Baburao"; System.out.println(reverse(s));     } public static String reverse(String s){ Stack<Character> st = new Stack<>(); String reverse = ""; for(int i=0;i<s.length();i++){ st.push(s.charAt(i)); } while(st.size()!=0){ reverse = reverse + st.pop(); } return reverse; } } o/p:oarubaB

Java Program to Print Multiplication Table for any Number

 Java Program to Print Multiplication Table for any Number For Explnation Check the video: Code: import java.util.Scanner; class Test{ public static void main(String[] args){ Scanner scn = new Scanner(System.in); int n = scn.nextInt(); for(int i=1;i<=10;i++){ System.out.println(n+" x "+i+" = "+(n*i)); } } } o/p: 2 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

Jesse and Cookies Hackerrank Solution Java | Data Structure

 Jesse and Cookies Hackerrank Solution Java | Data Structure Note: Due to TLE i modified the some part of code. Sample Input STDIN Function ----- -------- 6 7 A[] size n = 6, k = 7 1 2 3 9 10 12 A = [1, 2, 3, 9, 10, 12] Sample Output 2 import  java.io.*; import  java.math.*; import  java.text.*; import  java.util.*; import  java.util.regex.*; public   class  Solution {      static  PriorityQueue<Integer> heap =  new  PriorityQueue<>();      /*      * Complete the cookies function below.      */      static   int  cookies( int  k) {                   int  count =  0 ;          while (heap.peek()<k && heap.size()>= 2 ){             heap.add(heap.remove()+heap.remove()* 2 );             count++;         }          if (heap.size()== 1  && heap.peek()<k){             count = - 1 ;         }          return  count;     }      public   static   void  main(String[] args) {         Scanner scn =  new  Scann

QHEAP1 Hackerrank Solution Java | Data Structure

 QHEAP1 Hackerrank Solution Java | Data Structure For Explanation: Sample Input STDIN Function ----- -------- 5 Q = 5 1 4 insert 4 1 9 insert 9 3 print minimum 2 4 delete 4 3 print minimum Sample Output 4 9 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);         PriorityQueue<Integer> heap =  new  PriorityQueue<Integer>();          int  q = scn.nextInt();          while (q--> 0 ){              int  q1 = scn.nextInt();              if (q1== 1 ){                  int  num = scn.nextInt();                 heap.add(num);             } else   if (q1== 2 ){                  int  num = scn.nextInt();                 heap.remove(num);             } else   if (q1== 3 ){