Posts

Showing posts from February, 2022

Hit most balloons Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/4e75764f8f1638eb4f1c5478ca1986043e15e39a/1# (It is also the problem of the day for 28-02-2022) Solution:  int max=0;        for(int i=0;i<n;i++){            HashMap<Double,Integer>map=new HashMap<Double,Integer>();              int x1=arr[i][0];               int y1=arr[i][1];               int count=0;            for(int j=0;j<n;j++){                int x2=arr[j][0];                int y2=arr[j][1];                                if(x1==x2 && y1==y2){                    //same point                    count++;                    //continue;                }                else{                    double tan=(y2-y1)/(1.0*(x2-x1));                    if(map.containsKey(tan)){                        map.put(tan,map.get(tan)+1);                    }                    else{                        map.put(tan,1);                    }                }                            }            for(double a:map.

Escape The Forbidden Forest Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/a4f19ea532cee502aabec77c07e0d0a45b76ecf9/1# It is also the problem of the day for 26-02-2022 Solution: class Sol {     public static int build_bridges(String s1, String s2)     {         /* tle (Recurisve approach)         if(s1.length()==0|| s2.length()==0){            return 0;        }        else{            StringBuilder sb1=new StringBuilder();                for(int i=0;i<s1.length()-1;i++){                    sb1.append(s1.charAt(i));                                   }                StringBuilder sb2=new StringBuilder();                for(int i=0;i<s2.length()-1;i++){                    sb2.append(s2.charAt(i));                                 }            if(s1.charAt(s1.length()-1)==s2.charAt(s2.length()-1)){                                                return 1+build_bridges(sb1.toString(),sb2.toString());            }            else{                return Integer.max(build_bridges(sb1.toString(),s2),build

Leaders In An Array Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/leaders-in-an-array-1587115620/1# Solution: class Solution{     //Function to find the leaders in the array.     static ArrayList<Integer> leaders(int arr[], int n){         Stack<Integer>stack=new Stack<Integer>();         ArrayList<Integer>aa=new ArrayList<Integer>();         for(int i=n-1;i>-1;i--){             if(stack.isEmpty()){                 stack.push(arr[i]);                 aa.add(-1);             }             else{                 if(stack.peek()>arr[i]){                     aa.add(stack.peek());                 }                 else{                     while(!stack.isEmpty() && stack.peek()<=arr[i]){                         stack.pop();                     }                     if(stack.size()==0){                         aa.add(-1);                     }                     else{                         aa.add(stack.peek());                     }                 }

Return Two Prime Numbers Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/return-two-prime-numbers2509/1# It is also the problem of the day for 24-02-2022 Solution: class Solution{     public static boolean isPrime(int n){         if(n==1){             return false;         }         if(n==2 || n==3){             return true;         }         else if(n%2==0 || n%3==0){             return false;         }         else{             boolean flag=true;             for(int i=5;i<=Math.sqrt(n);i+=6){                 if((n%i)==0 || n%(i+2)==0){                     flag=false;                     break;                 }              }             if(flag==false){                 return false;             }             else{                 return true;             }         }     }     static List<Integer> primeDivision(int n){         ArrayList<Integer>aa=new ArrayList<Integer>();         for(int i=1;i<(n);i++){             if(isPrime(i)==true){                 aa.add(i);          

Java Loops || HackerRank

 Problem Link: https://www.hackerrank.com/challenges/java-loops/problem Solution: import  java.util.*; import  java.io.*; import  java.util.ArrayList; class  Solution{      public   static   void  main(String []argh){        Scanner sc= new  Scanner(System.in);         int  t=sc.nextInt();         while (t --> 0 ){             int  a=sc.nextInt();             int  b=sc.nextInt();             int  n=sc.nextInt();             int  first=a+b;              int  res=first;             System.out.print(first+ " " );             for ( int  i= 1 ;i<n;i++){                res=res+( int )Math.pow( 2 ,i)*b;                System.out.print(res+ " " );            }            System.out.println();        }     } } Time Complexity: O(T*N)[For each of T test cases, we iterate till size ie n] Space Complexity: O(T)[Depending on the number of test cases given] Auxiliary Space: O(1)[No additional data structure is used] Total Test Cases:6 Approach used: Here we observe a patter

Rearrange Geek and His Classmate Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/47e5aa3f32aee9e0405f04960f37c8a562d96a2f/1# It is also the problem of the day for 18-February-2022 Solution: long arr[]=new long[n];                  for(int i=0;i<n;i++){             long b=a[i];             //System.out.println(b);             int b1=(int)b;             arr[i]=a[b1];         }                 /* for(int i=0;i<n;i++){             System.out.println(arr[i]);         }*/                  for(int i=0;i<arr.length;i++){             a[i]=arr[i];         } TIME COMPLEXITY: O(N)[Array Length][GFG Time:3.5/5.0] SPACE COMPLEXITY:O(N)[Array Is Used] AUXILIARY SPACE:O[N][Additional Array Is Used] TOTAL TEST CASES:26 APPROACH USED: 1. First we will traverse the array store the array value of the element. For better explaination refer this example: array: 0 5 1 4 2 3 array index: 0 1 2 3 4 5  Now according to question we need to exchange places and we do that as  arr[1]=5 now we will find out the value at arr[5] i

Remove Leading Zeroes from an IP Address Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/remove-leading-zeros-from-an-ip-address3530/1# It is also the problem of the day for 11-02-2020 Solution: Today we will first discuss the approach and then we will move on to the solution. 1. We are given a string consisting of "." so to access each word we need to separate the words present in the string. For splitting the words from the string we will use the split function and its syntax will be split("\\.") .  2. Now for splitting we will use one string array and store all strings in it. 3. After splitting we need to look at every word and remove all the leading zeroes from them. 4. For removing leading zeroes we have 2 different ways:     a)traverse string till while(str.charAt(i)!=0) and simultaneously remove characters(You will require           stringbuilder for character deletions)     b)Use Integer.valueOf or Integer.parseInt() to directly get the values without leading zeroes as all leading zeroes

Capitalize The Title LeetCode

Problem Link: https://leetcode.com/problems/capitalize-the-title/ Solution:  Using ArrayList:  String arr[];             arr=s.split(" ");         ArrayList<String>aa=new ArrayList<String>();       for(int i=0;i<arr.length;i++){           aa.add(arr[i]);       }                  for(int i=0;i<aa.size();i++){             aa.set(i,aa.get(i).toLowerCase());             if(aa.get(i).length()>2){                 String a=Character.toUpperCase(aa.get(i).charAt(0)) + aa.get(i).substring(1);                 aa.set(i,a);             }         }         StringBuilder sb=new StringBuilder();         for(int i=0;i<aa.size();i++){             sb.append(aa.get(i));             sb.append(" ");         }         sb.deleteCharAt(sb.length()-1);         return sb.toString(); TIME COMPLEXITY: O(String Length)[LeetCode Time:16 ms faster than 26.16%] SPACE COMPLEXITY: O(Total Words In String)[It is a must for us to use a string array to extract all the words pres

XOR GAME GEEKS FOR GEEKS

 Problem Link: https://practice.geeksforgeeks.org/problems/xor-game2143/1# (It is also the problem of the day for 10 February) Solution: class Solution{     static int xorCal(int k){                if(k!=1){             String s=Integer.toBinaryString(k);             //System.out.println(s);             boolean flag=true;                         for(int i=0;i<s.length()-1;i++){                if(s.charAt(i)!=s.charAt(i+1)){                    flag=false;                    break;                }            }            if(flag){               StringBuilder sb=new StringBuilder();               sb.append(s);               sb.deleteCharAt(0);               int val=Integer.parseInt(sb.toString(),2);               return val;            }            else{                return -1;            }         }         else{             return 2;         }     } } TIME COMPLEXITY:[O(String Length) in the worst case all are 1 and in question given it will be for 63.][GFG Time:01./2.0] SPACE COM

Reverse First K Elmeents Of Queue Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/reverse-first-k-elements-of-queue/1# (It is also the problem of the day for 13 January.) Solution 1: Using LinkedList class GfG {     // Function to reverse first k elements of a queue.     public Queue<Integer> modifyQueue(Queue<Integer> q, int k) {        int count=0;        //Queue<Integer>aa=new ArrayDeque<Integer>();        Queue<Integer>aa=new LinkedList<Integer>();        Stack<Integer>stack=new Stack<Integer>();        while(count<k){            ++count;            stack.push(q.poll());        }        while(!stack.isEmpty()){            aa.add(stack.pop());        }        while(!q.isEmpty()){            aa.add(q.poll());        }        //Queue<Integer>        return aa;     } } TIME COMPLEXITY: O(Total Elements in q)[GFG Time:1.1/2.8] SPACE COMPLEXITY: O(Total Elements in q)[Given is queue] AUXILARY SPACE: O(Total Elements in q)[Here in the worst case the stack

Find Prime Numbers In Range Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/find-prime-numbers-in-a-range4718/1 It is also the problem of the day of 8 February. Solution: class Solution {     public boolean isPrime(int a){         if(a==1){             return false;         }         if(a==2 || a==3){             return true;         }         else if(a%2==0 || a%3==0){             return false;         }         else{             boolean flag=true;             for(int i=5;i<=Math.sqrt(a);i+=6){                 if(a%i==0 || a%(i+2)==0){                     flag=false;                     break;                 }             }             return flag;         }     }     ArrayList<Integer> primeRange(int m, int n) {         ArrayList<Integer>aa=new ArrayList<Integer>();         for(int i=m;i<n+1;i++){             if(isPrime(i)){                 aa.add(i);             }         }         return aa;     } } TIME COMPLEXITY: O(N*SQRT(N)) SPACE COMPLEXITY: O(1) AUXILLARY SPACE: O(N)[

Super Reduced String Hacker Rank

 Problem Link: https://www.hackerrank.com/challenges/reduced-string/problem Solution: Stack<Character>stack= new  Stack<Character>();                   for ( int  i= 0 ;i<s.length();i++){              if (stack.isEmpty()){                 stack.push(s.charAt(i));             }              else {                  if (stack.peek()==s.charAt(i)){                     stack.pop();                 }                  else {                     stack.push(s.charAt(i));                 }             }         }          if (stack.size()!= 0 ){         StringBuilder sb= new  StringBuilder();          while (!stack.isEmpty()){             sb.append(stack.pop());         }         sb.reverse();                   return  sb.toString();          }          else {              return   "Empty String" ;         }          TIME COMPLEXITY: O(String Length) SPACE COMPLEXITY: O(1) AUXILLARY SPACE:O(String Length) TOTAL TEST CASES: 16 APPROACH: Using Stacks. Here we will use the

Binary Number To Decimal Number Geeks For Geeks

 Problem Link https://practice.geeksforgeeks.org/problems/binary-number-to-decimal-number3525/1# Solution: 1. Direct Inbuilt Function ie Integer.parseInt(String s,2(base you want to convert to)) class Solution {     public int binary_to_decimal(String s)     {                  return Integer.parseInt(s,2);                   } } Time Complexity: Though Integer.parseInt is an inbuilt function but it has the complexity of O(String length). It is so because it reads the string character by character and converts the string into integer according to radix ie 2 here.[GFG Time: 0.3/2.0] Space Complexity: O(1) Auxillary Space: O(1) Total Test Cases: 10030 Approach: Using of inbuilt function ----------------------------------------------------------------------------------------------------------------------------- Approach 2:  StringBuilder sb=new StringBuilder();         sb.append(s);         sb.reverse();         s=sb.toString();                  int index=0;         int val=0;             

Find Position Of Set Bit Geeks For Geeks

 Problem Link: (It is also the problem of the day for 2 February) https://practice.geeksforgeeks.org/problems/find-position-of-set-bit3706/1# Solution:  if(n==0){             return -1;         }         else{         int a=0;         int index=0;         int c=0;         int count=0;                           while(n!=0){             a=n%2;             if(a==1){                 c=index;                 ++count;                 if(count>1){                     c=-1;                     break;                 }             }             index++;             n=n/2;         }        if(c==-1){            return -1;        }        else{           return ++c;        } } Time:O(log n)[GFG Time: 0.1/2.0] Space:O(1) Auxillary Space:O(1)[No recursion stack is used.] Total Test Cases:102 Approach: Here we will follow up the digit-wise approach and check for every digit starting from the last of the number. If the digit is 1 then we will store that index in one variable c and simultaneously w