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

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

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

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

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

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

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

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

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

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

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