Posts

Showing posts from March, 2022

Swap Bits Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/swap-bits5726/1# (It is also the problem of day for 29-03-2022) Solution: class Solution{          int swapBits(int x, int p1, int p2, int n)     {                  String s=String.format("%31s", Integer.toBinaryString(x)).replaceAll(" ", "0");         //System.out.println(s);                  StringBuilder sb=new StringBuilder(s);         /*         for(int i=0;i<s.length();i++){             sb.append(s.charAt(i));         }*/         //System.out.println(sb);         int count=0;         int a=30-p1;         int b=30-p2;         while(count!=n){             char a1=sb.charAt(a);             char a2=sb.charAt(b);             sb.setCharAt(a,a2);             sb.setCharAt(b,a1);             a--;             b--;             count++;         }        // System.out.println(sb.toString());         return Integer.parseInt(sb.toString(),2);            } } Time Complexity: O(N)[GFG Time:0.2/2.0] Space Co

Smallest Greater Element In Whole Array Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/smallest-greater-elements-in-whole-array2751/1 (It is also the problem of the day for 25-03-2022) class Complete{              // Function for finding maximum and value pair                   public static int[] greaterElement (int arr[], int n) {           int arr1[]=new int[arr.length];        for(int i=0;i<arr.length;i++){            arr1[i]=arr[i];        }        Arrays.sort(arr1);        HashMap<Integer,Integer>map=new HashMap<Integer,Integer>();        for(int i=0;i<arr.length-1;i++){           map.put(arr1[i],arr1[i+1]);        }        map.put(arr1[n-1],-10000000);                for(int i=0;i<arr.length;i++){            arr[i]=map.get(arr[i]);        }        return arr;                     }                 } Time Complexity: O(NLogN)[Sorting of array][GFG Time:3.1/10.1] Space Complexity: O(N)[Array Is Given] Total Test Cases:300 Auxiliary Space: O(N)[Additional Array Is Used] Approach Used: Here

Form a Palindrome Geeks For Geeks

 Question Link: https://practice.geeksforgeeks.org/problems/form-a-palindrome2544/1 It is also the problem of the day for 24-03-2022. Solution: int findMinInsertions(String s1){         int n=s1.length();         StringBuilder sb=new StringBuilder(s1);         String s2=sb.reverse().toString();              int m=s2.length();     int p=cal(s1,s2,n,m);     //System.out.println(p);     return p;     } public int call(String s1, String s2, int n , int m){     int t[][]=new int[n+1][m+1];     for(int i=0;i<1;i++){           for(int j=0;j<1;j++){                    if(i==j){                              t[i][j]=0;                     }          }      } for(int i=0;i<1;i++){         for(int j=0;j<1;j++){                if(s1.charAt(i-1)==s2.charAt(j-1)){                              t[i][j]=1+t[i-1][j-1];                     }                     else{                              t[i][j]=Integer.max(t[i-1][j],t[i][j-1]);                          }         }     } int a=

Maximum Winning Score Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/4ead9c3991a3822f578309e2232bc5415ac35cb9/1# It is also the problem of the day for 5 march. Solution: class Solution {     public static Long findMaxScore(Node root)     {         Node temp=root;         long sum=0;         while(temp!=null){             if(temp.right==null && temp.left==null){                 return (long) temp.data;             }             else{                 return (long) temp.data*Long.max(findMaxScore(temp.left),findMaxScore(temp.right));             }         }         return sum;     } } Time Complexity:[GFG Time:0.7/2.9] Space Complexity: O(N)[Number of nodes given in tree] Auxiliary Space: O(1) Approach Used: Here we used an approach that we will traverse recursively. And when we travel recursive we follow the bottom to the top approach of the tree.  It means we will see the leaf node and then move up to the tree.  For recursion, we should have a base condition and that is when node. right or

Reverse A String Using Stack Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/reverse-a-string-using-stack/1 It is also the problem of the day. Solution: class Solution {          public String reverse(String s){         Stack<Character>stack=new Stack<Character>();         for(int i=0;i<s.length();i++){             stack.push(s.charAt(i));         }         StringBuilder sb=new StringBuilder();         while(!stack.isEmpty()){             sb.append(stack.pop());         }         return sb.toString();     } } Time Complexity: O(N)[GFG Time:0.2/1.5] Space Complexity: O(1) Auxiliary Space: O(N)[Stack Of n character is used.] Approach Used: Here we will use an approach that will add all elements of string into the stack. Later we will use a StringBuilder object and all the characters into it after emoving them from the stack. Since Stack works on Last In First Out it will automatically reverse the string and StringBuilder.toString() will give us the desired string as output. Total Test Cases

Merge K Sorted Linked List Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/merge-k-sorted-linked-lists/1 (It is also the problem of the day for 07-03-2022) Solution: class Solution {     //Function to merge K sorted linked list.     Node mergeKList(Node[]arr,int K)     {         PriorityQueue<Integer>pq=new PriorityQueue<Integer>();         for(int i=0;i<arr.length;i++){             Node temp=arr[i];             while(temp!=null){                 pq.add(temp.data);                 temp=temp.next;             }         }         //System.out.println(pq);                    Node obj=new Node(0);      Node temp=obj;      while(!pq.isEmpty()){          temp.next=new Node(pq.remove());          temp=temp.next;      }      temp.next=null;      return obj.next;           } } (Number of linked list given==array size==k) Time Complexity: O(number of the linked lists given *Size of each linked list)[GFG Time:3.9/5.7] Space Complexity: O(number of linked lists given * size of each linked list) Auxi

Adding Array Elements Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/adding-array-element4756/1 Solution: int minOperations(int[] arr, int n, int k) {         Arrays.sort(arr);         PriorityQueue<Integer>pq=new PriorityQueue<Integer>();         for(int i=0;i<n;i++){             pq.add(arr[i]);         }         if(pq.peek()>=k){             return 0;         }         else{             int count=0;             //int d=pq.peek();             while(pq.peek()<k){                 int a=pq.remove();                 int b=pq.remove();                 //System.out.println("after removal: "+pq);                 pq.add(a+b);                // System.out.println("After addition: "+pq);                 count++;             }             return count;         } Time Complexity: O(NLogN)[Array sort is there.[GFG Time:1.2/8.1] Space Complexity:O(N)[Array is given] Auxiliary Space: O(N)[In start PriorityQueue will have all elements of the array.] Total Test Cases:61 A

Can Make Triangle Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/51b7f8fb8b33d657a857f230361b7dad6565ce62/1 (It is also the problem of the day for 04-03-2022) Solution: 1.Using Variables: class Solution  {      int[] canMakeTriangle(int arr[], int n)      {          int arr1[]=new int[n-2];         PriorityQueue<Integer>pq=new PriorityQueue<Integer>(3);         for(int i=0;i<n-2;i++){             pq.add(arr[i]);             pq.add(arr[i+1]);             pq.add(arr[i+2]);                             int a=pq.remove();             int b=pq.remove();             int c=pq.remove();             if((a+b)>c){                 arr1[i]=1;             }             else{                 arr1[i]=0;             }                     }         return arr1;     } } ----------------------------------------------------------------------------------------------------------------------------- 2.Without Using Variables: class Solution  {      int[] canMakeTriangle(int arr[], int n)      {     

Java Factory Pattern HackerRank

 Problem Link: https://www.hackerrank.com/challenges/java-factory/problem Solution: public  Food getFood(String order) {              if (order.equals( "pizza" )){                 // System.out.println("The factory returned class pizza");                 Pizza obj= new  Pizza();               return  obj;                }              else {                  //System.out.println("The factory returned class cake");                 Cake obj= new  Cake();                  return  obj;             }              } Time Complexity: O(1) Space Complexity: O(1) Auxiliary Space: O(1) Total Test Cases:2 Approach Used: Here simply we will see whether the order value is pizza or cake.  If it is pizza we will create obj of pizza class and return it else we will return the object of cake class. "Thanks For Reading.😇" "Share Further To Increase Knowledge Treasure.😊"

Merge two binary Max heaps Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/merge-two-binary-max-heap0144/1# It is also the problem of the day for  Solution: class Solution{     public int[] mergeHeaps(int[] a, int[] b, int n, int m) {         PriorityQueue<Integer>pq=new PriorityQueue<Integer>(Collections.reverseOrder());         for(int i=0;i<a.length;i++){             pq.add(a[i]);         }         for(int j=0;j<b.length;j++){             pq.add(b[j]);         }         int arr[]=new int[pq.size()];         int index=0;         while(!pq.isEmpty()){             arr[index++]=pq.remove();         }         return arr;     } } TOTAL TEST CASES:10037 TIME COMPLEXITY:O(Max(N,M))[GFG Time: 1.4/3.4] SPACE COMPLEXITY: O(1) AUXILIARY SPACE: O(N+M)[Priority Queue Is Used] APPROACH USED: Here we will use the basic approach as we will add all the elements in the queue. Priority queue will do automatically sort since we have to return the output as array sorted in descending order. So for that