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

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

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

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

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

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)[Arra...

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

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

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

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

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

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

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

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