Posts

Showing posts from January, 2022

Reverse Words In A Given String Geeks For Geeks

 Problem Link: Its also Problem Of Day 29 January https://practice.geeksforgeeks.org/problems/reverse-words-in-a-given-string5459/1 Solution: class Solution  {     //Function to reverse words in a given string.     String reverseWords(String s)     {                     StringBuilder sb=new StringBuilder();        Stack<String>stack=new Stack<String>();         for(int i=0;i<s.length();i++){             if(s.charAt(i)=='.'){                 stack.add(sb.toString());                 sb.setLength(0);             }             else{                 sb.append(s.charAt(i));             }       ...

Set Mismatch LeetCode

Problem Link: https://leetcode.com/problems/set-mismatch/   Solution 1: int sum=0;         int sum1=0;         HashSet<Integer>set=new HashSet<Integer>();         for(int i=0;i<nums.length;i++){            set.add(nums[i]);             sum1=sum1+nums[i];         }                               for(int a:set){             sum=sum+a;         }                  int arr[]=new int[2];         arr[0]=sum1-sum;         int sum2=(nums.length*(nums.length+1))/2;         arr[01]=sum2-sum;                  return arr; ------------------------------------------------...

Counting Bits LeetCode

 Problem Link: https://leetcode.com/problems/counting-bits/ Solution 1 : public int cal(int n){          int count=0;          while(n>0){              n=n&(n-1);              ++count;          }          return count;      }               public int[] countBits(int n) {         int arr[]=new int[n+1];         for(int i=0;i<=n;i++){             arr[i]=cal(i);         }         return arr;     } Time Complexity:O(N*number of set bits in number)[LeetCode Time: 2ms faster than 68.42%] Space Complexity:O(1)[No array or any other data structure is used.] Auxillary Space:O(N) [An additional array is used which will be returned later.] LeetCode ...

Print 1 To N Without Using Loops Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/print-1-to-n-without-using-loops3621/1 Solution: class Solution{     static void printTillN(int n){         if(n==0){             return ;         }         else{             printTillN(n-1);             System.out.print(n+" ");         }         // code here     } } Time Complexity: O(N)[GFG Time: 0.5/2.0] Space Complexity:O(1) Auxillary Space:O(N)[Recusive Stack of max N+1 functional calls][Non-Tail Recursive] Total Test Cases: 200 Approach: We, Will, apply for Recursion. Here base condition will be when n==0 else we will make a recursive call and print number later. "Thanks For Reading.😇" "Share Further To Increase Knowledge Treasure.😊" 

Rearrange A String Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/rearrange-a-string4100/1 Solution: class Solution {     public String arrangeString(String s)         {            PriorityQueue<Character>set=new PriorityQueue<Character>();             int res=0;            for(int i=0;i<s.length();i++){                if(Character.isDigit(s.charAt(i))){                     res=res+Character.getNumericValue(s.charAt(i));                  }                else{                    set.add(s.charAt(i));                }            }         ...

Sort Integers By The Number Of 1 Bits LeetCode

 Problem Link: https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/ Solution: public int cal(int n){         int count=0;         while(n>0){             n=n&(n-1);             ++count;         }         return count;     }               public int[] sortByBits(int[] arr) {         TreeMap<Integer,PriorityQueue<Integer>>map=new TreeMap<Integer,PriorityQueue<Integer>>();                  for(int i=0;i<arr.length;i++){            if(map.containsKey(cal(arr[i]))){               PriorityQueue<Integer>pq=map.get(cal(arr[i]));                pq.add(arr[i]);       ...

Power Set Geeks For Geeks

 Problem Link https://practice.geeksforgeeks.org/problems/power-set4302/1/ Solution:  ArrayList<String>aa=new ArrayList<String>();         StringBuilder sb=new StringBuilder();         for(int i=01;i<(int)Math.pow(2,s.length());i++){             for(int j=0;j<s.length();j++){                 if((i&(1<<j))!=0){                     sb.append(s.charAt(j));                 }                              }             aa.add(sb.toString());             sb.setLength(0);         }                 Collections.sort(aa);              ...

Flipping Bits HackerRank

Problem Link: https://www.hackerrank.com/challenges/flipping-bits/problem Solution:   class  Result {      public   static   long  flippingBits( long  n) {                  long  n1=~n;         String s=Long.toBinaryString(n1);          //System.out.println(s);          double  res= 0.0 ;          for ( int  i= 0 ;i< 32 ;i++){              if (s.charAt( 63 -i)== '1' ){                 res=res+Math.pow( 2 ,i);             }         }  ...

Maximizing XOR Hacker Rank

 Problem Link: https://www.hackerrank.com/challenges/maximizing-xor/problem Solution: class  Result {      /*      * Complete the 'maximizingXor' function below.      *      * The function is expected to return an INTEGER.      * The function accepts the following parameters:      *  1. INTEGER l      *  2. INTEGER r      */      public   static   int  maximizingXor( int  l,  int  r) {         PriorityQueue<Integer>pq= new  PriorityQueue<Integer>(Collections.reverseOrder());             ...

Two Numbers With Odd Occurrences Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/two-numbers-with-odd-occurrences5846/1/ Solution 1: Using XOR Operator class Solution {     public int[] twoOddNum(int arr[], int n)     {         int xor=0;       for(int i=0;i<arr.length;i++){           xor=xor^arr[i];       }       int sn=(xor&(~(xor-1)));       int sum1=0;       int sum2=0;       for(int i=0;i<arr.length;i++){           if((arr[i]&sn)!=0){               sum1=sum1^arr[i];           }           else{               sum2=sum2^arr[i];           }       }       int arr1[]=new int[2];       arr1[0]=Math.max(sum1,sum2);     ...

Number of 1 bits GFG Practice

Problem Link: https://practice.geeksforgeeks.org/problems/set-bits0143/1 Solution: int count=0;         int a=0;         while(n!=0){             a=n%2;             if(a==1){                 ++count;             }             n=n/2;               //n=n>>1 (you can use this statement also in place of n=n/2 as every time n is divided by 2)         }         return count; TIME:O(Total bits in given number)[GFG Time:0.2/1.3] SPACE:O(1) AUXILLARY SPACE:O(1) Total Test Cases:150 Approach : Here we will convert the number into binary and check for each digit from last. If the digit is 1 then increment the counter variable count else value of the count variable remains the same. Return count; ---------------------...

Count Digits in A Factorial Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/count-digits-in-a-factorial3957/1# Solution: class Solution{     static int facDigits(int n){         double a=0.0;         for(int i=2;i<=n;i++){             a=a+Math.log10(i);         }        a=a+1;         return (int)a;     } } TIME:O(N)[Here we are traversing the whole array once starting from 2 and taking the sum of all log values of numbers.][GFG Time:0.1/1.2] SPACE:O(1)[No array or any memory consuming data structure is used] AUXILLARY SPACE:O(1)[No extra space required] Total Test Cases:290 APPROACH: 1.We know that log(a*b)=log(a)+log(b) and for factorial, we can write, factorial of number n = 1*2*3*4*.......................*n and log(1*2*3*4*.......................*n)=log(1)+log(2)+log(3)+log(4)+......................................+log(n) Using this approach in...

Absolute Value Geeks For Geeks

 Problem Link: Absolute Value | Practice | GeeksforGeeks Solution: class Solution {     public int absolute(int I) {         return Math.abs(I);     } } Time:O(1) Space:O(1) Auxillary Space:O(1)

LCM and GCD Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/lcm-and-gcd4516/1/ Solution:  class Solution {     static Long fun(Long a, Long b){         if(b==0){             return a;         }         else{             return fun(b,a%b);         }     }      static Long[] lcmAndGcd(Long a , Long b) {             Long c=fun(a,b);         Long arr[]=new Long[2];         Long d=(a*b)/c;             arr[0]=d;             arr[1]=c;         // System.out.println(arr[0]+" "+arr[1]);         return arr;      } }; TIME:O(log(min(a,b)))[GFG Time:0.1/1.5] SPACE:O(1)[Array arr of size 2 is created.] AUXILLARY SPACE:O(Log...

Factorial Number Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/factorial-number2446/1 Solution : class Solution{     static int isFactorial(int n) {         //code here         int val=1;         int index=1;                 while(val<n){                        val=val*index;            index++;        }        if(val==n){            return 1;        }        else{            return 0;        }              } } TIME:O(Log n)[Every Time multiplication with 2][GFG Time:0.2/1.4] SPACE:O(1) AUXILLARY SPACE:O(1)[Only Constants] Total Test Cases:11555 Approach:  We will apply the while loop and se...

Count Digits Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/count-digits5716/1 Solution: class Solution{     static int evenlyDivides(int n){                int num=n;         int count=0;         int a=0;         while(n!=0){             a=n%10;             if(a!=0){                 if(num%a==0){                     ++count;                 }             }             n=n/10;         }        // System.out.println(count);         return count;     } } TIME:O(logn)[GFG Time :0.1/1.2] SPACE:O(1)[No Storage only constants] AUXILLARY SPACE:O(1)[No Additional Space...

Even Odd GeeksForGeeks

 Problem Link: https://practice.geeksforgeeks.org/problems/even-odd/1# Solution: class Solution{     public void evenOdd(int a, int b){         int a1=a&1;         if(a1==1){             System.out.println(b);             System.out.println(a);         }         else{              System.out.println(a);             System.out.println(b);         }     } } TIME:O(1)[Geeks For Geeks Time: 0.1/1.3] SPACE:O(1) Approach: Since in question only they have asked to print even number before than odd number, so it is a guarantee that among two numbers they provide one will be even one will be odd.  So if we check for one number only the other number will be reverse of that. That's why here we checked for number a and if it is even then b will...

Find Unique Element Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/find-unique-element2632/1  Solution :  HashMap<Integer,Integer>map=new HashMap<Integer,Integer>();         for(int i=0;i<n;i++){             if(map.containsKey(a[i])){                 map.put(a[i],map.get(a[i])+1);             }             else{                 map.put(a[i],1);             }         }         int c=0;         for(int a1:map.keySet()){             int b=map.get(a1);             if(b%k!=0){                 c=a1;                 break;             }...

Substring Of Size Three with Distinct Characters LeetCode

 Problem Link: https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/ Solution: class Solution {     public int countGoodSubstrings(String s) {             ArrayDeque<Character>aa=new ArrayDeque<Character>();             if(s.length()<3){                 return 0;             }         else{             int count=0;             aa.addLast(s.charAt(0));              aa.addLast(s.charAt(01));              aa.addLast(s.charAt(02));             if(s.charAt(0)!=s.charAt(1) && s.charAt(1)!=s.charAt(2) && s.charAt(0)!=s.charAt(2)){                 count++;       ...