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

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

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

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

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

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

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

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

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