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

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

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

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

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

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

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

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

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

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