Posts

Showing posts from April, 2022

Shop In Candy Store Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/shop-in-candy-store1145/1# (It is also the problem of the day for 28-04-2022) Solution: Using Arrays.sort and Without additional arraylist: class Solution{     static ArrayList<Integer> candyStore(int arr[],int n,int k){        ArrayList<Integer>ab=new ArrayList<Integer>();                 Arrays.sort(arr);        /*for(int i=0;i<arr.length;i++){             System.out.print(arr[i]+" ");         }        */                int start=0;        int end=arr.length-1;        int cost=0;                while(start<=end){            cost=cost+arr[start];            start++;            end=end-k;        }        //System.out.println(cost);                start=arr.length-1;        end=0;        int cost2=0;                while(end<=start){            cost2=cost2+arr[start];            //System.out.println(cost2);            end+=k;            start--;        }               // System.out.println(cos

Product of Primes Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/product-of-primes5328/1# (It is also the problem of the day for 24-04-2022) Solution: class Solution{     public static boolean checkPrime(int n){         if(n==1){             return false;         }         else if(n==2 || n==3){             return true;                      }         else if(n%2==0 || n%3==0){             return false;         }         else{             for(int i=5;i<Math.sqrt(n)+1;i+=6){                 if(n%i==0 || n%(i+2)==0){                     return false;                 }             }             return true;         }     }     static long primeProduct(long l, long r){         long p=01;         for(int i=(int)l;i<((int)r+1);i++){             if(checkPrime(i)==true){                 p=(p*i)%1000000007;             }         }         return p;     } } Time Complexity: O((r-l)*(Math.sqrt(number)))[GFG Time:0.21/2.68] [Here number varies from l to r and taking it in worst case it will be r so

Search insert position of K in a sorted array Geeks For Geeks

 Problem Link: (It is also the problem of the day for 23-04-2022) Solution: class Solution {     static int searchInsertK(int arr[], int n, int k)     {         boolean flag=true;         int pos=0;         for(int i=0;i<n;i++){             if(arr[i]==k){                 flag=false;                 pos=i;             }         }         if(flag==true){             for(int i=0;i<arr.length-1;i++){                 if(arr[i]<k && k<arr[i+1]){                     pos=i+1;                     break;                 }                 else if(k>arr[arr.length-1]){                     pos=arr.length;                 }                 else if(k<arr[0]){                     pos=0;                 }             }             return pos;         }         else{             return pos;         }              } } Total Test Cases:304 Time Complexity:    O(N) [GFG Time:0.44/3.09][Linear Search] Space Complexity: O(N)[Array is given.] Auxiliary Space: O(1) Approach Used: Here

Sum of two numbers without using arithmetic operators Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/sum-of-two-numbers-without-using-arithmetic-operators/1# (It is also the problem of the day for 22-04-2022)  Solution: class Solution {     int sum(int a, int b)     {         return ((a|b)+(a&b));     } } Total Test Cases:310 Time Complexity: O(max number of bits(number 1, number 2))[As we will perform operations and for which we require all the bits.] [GFG Time:0.11/1.13] Space Complexity: O(1) Auxiliary Space: O(1) Approach Used: Here we will do two things. 1. Take or of two numbers 2. Take and of two numbers eg if the number is 20 and the other is 40 then 20 = 10100 (5 bits) -> 010100 to convert it into 6 bits as another number is also 6 bits, so and operation will be performed easily and correctly. 40= 101000 (6 bits) Now taking and of two numbers  and=010100 & 101000 -> 000000 ie 0 Now doing or operation we get or=010100 | 101000 -> 111100 ie 60 Now and+or = 0+60 =60 =20+40 Hence we will get correct answer

Recursively remove all adjacent duplicates Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/recursively-remove-all-adjacent-duplicates0744/1 (It is also the problem of the day for 21-04-2022) Solution: class Solution{     String remove(String s) {         boolean flag=false;         for(int i=0;i<s.length()-1;i++){             if(s.charAt(i)==s.charAt(i+1)){                 flag=true;                 break;             }         }         if(flag!=true){             return s;                      }         else{             StringBuilder sb1=new StringBuilder(s);             StringBuilder sb=new StringBuilder();             sb1.append('0');             String s1=sb1.toString();             //System.out.println(s1);             char c=s1.charAt(0);             int count=1;                          for(int i=1;i<s1.length();i++){                 if(s1.charAt(i)!=c){                     if(count==1){                         sb.append(c);                     }                     else{                        

Partition a list around a given point Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/partition-a-linked-list-around-a-given-value/1 (It is also the problem of the day for 19-04-2022) Solution 1: Without modification class Solution {     public static Node partition(Node head, int x) {         Node temp=head;         Node temp1=new Node(-1);         Node temp2=new Node(-1);         Node temp3=new Node(-1);         Node temp4=temp1;        Node temp5=temp2;        Node temp6=temp3;                  while(temp!=null){             if(temp.data<x){                 temp1.next=new Node(temp.data);                              temp=temp.next;                temp1=temp1.next;             }             else if(temp.data==x){                 temp2.next=new Node(temp.data);               //  temp2=temp2.next;                 temp=temp.next;                 temp2=temp2.next;             }             else{                 temp3.next=new Node(temp.data);               //  temp3=temp3.next;                 temp=temp.next;  

k-Ary Tree Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/k-ary-tree1235/1 (It is also the problem of the day for 18-04-2022) Solution: class Solution {     static Long karyTree(int k, int m) {         long a=01;//return (long)(Math.pow(k,m)%1000000007);                  for(int i=0;i<m;i++){             a=(a*k)%1000000007;         }         return a;     } }; TIME COMPLEXITY: O(M)[GFG Time:10.34/18.44] SPACE COMPLEXITY: O(1) AUXILIARY SPACE: O(1) TOTAL TEST CASES:111 APPROACH USED: Here if we observe clearly then a number of leaf nodes are equal to k^m. eg if k=3 m=2 at the 0th level only one node ie the head node at the 1st level three nodes  at the 2nd level, each of the three nodes will be further divided into 3 nodes. Hence 1*3=3 so for 3 nodes 3*3=9 and all are leaf nodes and equal to 3^2 ie 9 So we will find out the value of k raise to the power m. Question arises: Can't we directly use Math. pow(k,m) and change the return value from double to long? Answer: Yes, it is an

LCP Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/cf0cd86c66d07222499f84ec22dbcf6cae30e848/1# (It is also the problem of the day for 07-04-2022) Solution: Approach 1:  int count=0;         Arrays.sort(arr);         String a=arr[0];          StringBuilder sb=new StringBuilder();                   while(count<a.length()){             boolean flag=true;             for(int i=0;i<n;i++){                 if(arr[i].charAt(count)!=a.charAt(count)){                     flag=false;                 }                              }                          if(flag==true){                 sb.append(a.charAt(count));             }             else{                 break;             }             count++;                      }                 if(sb.length()==0){             return "-1";         }                  else{             return sb.toString();         } Time Complexity: O(nlogn)[GFG Time: 0.35/6.28] Space Complexity:O(n)[Array is given] Auxiliary Space: O(n)[String

Maximum Selections Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/87ecf96cfd61e511c697c8c94d21c5de7f471fcb/1# It is also the problem of the day for 06 April 2022 Solution: class solver {     static int max_non_overlapping(int ranges[][], int n){      Arrays.sort(ranges,(range1,range2)->(range1[1]-range2[1]));         int count=0;         int last=Integer.MIN_VALUE;         for(int i=0;i<ranges.length;i++){             if(ranges[i][0]>=last){                 count++;                 last=ranges[i][1];             }         }         return count;     } } Time Complexity:O(NlogN)[GFG Time:2.83/4.04] Space Complexity: O(N) Auxiliary Space: O(1) Approach Used: Here we follow a standard approach: 1. We will sort the second elements first and then arrange the whole array. eg1: (2,4) and (3,1) are given then we will sort according to the end distance and hence after sorting we will get (3,1) and (2,4) eg2: (3,8) and (2,8) are given then their second parameter is the same and in the array, w

Count Zeros XOR pairs Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/counts-zeros-xor-pairs0349/1 (It is also the problem of the day for 30 March 2022) Solution: class Complete{              // Function for finding maximum and value pair     public static long calculate (int arr[], int n) {         HashMap<Integer,Integer>map=new HashMap<Integer,Integer>();         for(int i=0;i<n;i++){             if(map.containsKey(arr[i])){                 map.put(arr[i],map.get(arr[i])+1);             }             else{                 map.put(arr[i],1);             }         }         long count=0;         for(int a:map.keySet()){             if(map.get(a)>1){                 count=count+(map.get(a)*(map.get(a)-1))/2;             }         }         return count;     }           } Time Complexity: O(N)[GFG Time:0.2/7.3] Space Complexity: O(N)[Array is given] Auxiliary Space: O(N)[Map is used] Approach Used: Here we will store the frequency of every element on the map. It is so because we

Move All Zeroes To the Start Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/move-all-zeros-to-the-front-of-the-linked-list/1# (It is also the problem of the day for 2 April 2022.) Solution 1: class GfG{     public Node moveZeroes(Node head){         Node temp1=new Node(-1);//zero linked list         Node temp2=new Node(-1);//non-zero linked list         Node temp3=temp1;         Node temp4=temp2;         Node temp5=head;         while(temp5!=null){             if(temp5.data==0){                 temp1.next=new Node(0);                 temp1=temp1.next;             }             else{                 temp2.next=new Node(temp5.data);                 temp2=temp2.next;             }             temp5=temp5.next;         }         temp1.next=temp4.next;         return temp3.next;     } } Total Test Cases:100 Time Complexity:O(N)[GFG Time:0.3/1.5] Space Complexity: O(N)Given is a Linked list  Auxiliary Space: O(N)[2 Linked list is created of different sizes] Approach Used: 1. Here we will traverse the given li