Skip to main content

Posts

Showing posts with the label Data Structure

Tree: Level Order Traversal Hackerrank Solution - java | Hackerrank Data Structures

 Tree: Level Order Traversal Hackerrank Solution - java For Explanation Watch Video :  Code import  java.util.*; import  java.io.*; class  Node {     Node left;     Node right;      int  data;          Node( int  data) {          this .data = data;         left = null;         right = null;     } } class  Solution {      /*           class Node          int data;         Node left;         Node right;     */      public   static   void  levelOrder(Node root) {         Queue<Node> q =  new  LinkedList<>();          if (root==null){              return ;         }         q.add(root);          while (!q.isEmpty()){             Node temp = q.poll();             System.out.print(temp.data+  " " );              if (temp.left!=null){                 q.add(temp.left);             }              if (temp.right!=null){                 q.add(temp.right);             }         }     }      public   static  Node insert(Node root,  int  data) {          if (root == null) {    

Delete Nth node from the end of the given linked list Solution - java

 Delete Nth node from the end of the given linked list Solution . For Explanation Watch Video : Code:: class Solution {     public ListNode removeNthFromEnd(ListNode head, int n) {         int count = 0;//5         ListNode temp1 = head;         while(temp1!=null){             count++;             temp1 = temp1.next;         }         if(count==n){             return head.next;         }         ListNode temp2 = head;         int remove = count - n;//3         int count1 = 0;         while(count1!=remove-1){             temp2 = temp2.next;             count1++;         }         temp2.next = temp2.next.next;         return head;     } }

Find the middle of a given linked list Solution - java

 Find the middle of a given linked list Solution - java For Explanation watch video:  Code:: Function to find Middle element if Head Of Linked List Given class Solution {     int getMiddle(Node head)     {          Node slow = head;          Node fast = head;          while(fast!=null && fast.next!=null){              slow = slow.next;              fast = fast.next.next;          }          return slow.data;     } }

java comparator hackerrank solution - java

 java comparator hackerrank solution - java For Explanation Watch Video : Code:: import  java.util.*; // Write your Checker class here class  Checker  implements  Comparator<Player>{      @Override      public   int  compare(Player p1,Player p2){         String name1 = p1.name;         String name2 = p2.name;          int  score1 = p1.score;          int  score2 = p2.score;          if (score1<score2){              return   1 ;         } else   if (score1>score2){              return  - 1 ;         } else  {              return  name1.compareTo(name2);         }     } } class  Player{

Connect n ropes with minimum cost Java Code Solution

  Connect n ropes with minimum cost Java Code Solution Watch Video:: Code: import java.util.*; import java.lang.*; import java.io.*; class Test{     public static void main(String[] args) {                  //Taking input using class Scanner         Scanner in = new Scanner(System.in);                  //Taking count of testcases         int t = in.nextInt();         while (t-- > 0) {                          //takling count of elements             int n = in.nextInt();                          //Creating an array of size n             long arr[] = new long[n];             //inserting elements to the array             for (int i = 0; i < n; ++i) arr[i] = in.nextLong();             //calling minCost method of class solve             System.out.println(new Solution().minCost(arr, n));         }     } } // } Driver Code Ends class Solution{     long minCost(long arr[], int n) {        PriorityQueue<Long> heap = new PriorityQueue<>();        for(int i=0;i<n;i++){      

Birthday Cake Candles Hackerrank Solution Java

Birthday Cake Candles Hackerrank Solution Java For Explanation Watch Video: Sample Input 0 4 3 2 1 3 Sample Output 0 2 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 'birthdayCakeCandles' function below.      *      * The function is expected to return an INTEGER.      * The function accepts INTEGER_ARRAY candles as parameter.      */      public   static   int  birthdayCakeCandles(List<Integer> candles) {      // Write your code here     Collections.sort(candles);      int  count =  1 ;      int  largest = candles.get(candles.size()- 1 );      for ( int  i=candles.size()- 2 ;i>= 0 ;i--){          if (candles.get(i)!=largest){             

Java Regex Hackerrank Solution

 Java Regex Hackerrank Solution For Explanation Watch Video: Sample Input 000.12.12.034 121.234.12.12 23.45.12.56 00.12.123.123123.123 122.23 Hello.IP Sample Output true true true false false false Code: import  java.util.regex.Matcher; import  java.util.regex.Pattern; import  java.util.Scanner; class  Solution{      public   static   void  main(String[] args){         Scanner in =  new  Scanner(System.in);          while (in.hasNext()){             String IP = in.next();             System.out.println(IP.matches( new  MyRegex().pattern));         }     } } //Write your code here class MyRegex{     //1--> it can contain single digit i.e ([0-9]);     //2--> It can contain two digits i.e ([0-9][0-9]);     //3--> Range is (099 to 199)i.e((0|1)[0-9][0-9]);            //4--> range is (200 - 249) i.e (2[0-4][0-9]) ;     //5--> range is (250-255) i.e (25[0-5]);         String reg = "([0-9]|[0-9][0-9]|(0|1)[0-9][0-9]|2[0-4][0-9]|25[0-5])";     public String pattern = r

Maximum Element Hackerrank Solution Java | Hackerrank Data Structure

 Maximum Element Hackerrank Solution Java For Explanation Video: Sample Input STDIN Function ----- -------- 10 operations[] size n = 10 1 97 operations = ['1 97', '2', '1 20', ....] 2 1 20 2 1 26 1 20 2 3 1 91 3 Sample Output 26 91 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; public   class  Solution {      public   static   void  main(String[] args){          Scanner scn = new Scanner(System.in);         int n = scn.nextInt();         Stack<Integer> st = new Stack<Integer>();         PriorityQueue<Integer> heap = new PriorityQueue<>(Collections.reverseOrder());         while(n-->0){             int t = scn.nextInt();             switch(

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

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