Posts

Showing posts from November, 2022

Find Players With Zero or One Losses LeetCode

Problem Link: https://leetcode.com/problems/find-players-with-zero-or-one-losses/description/ Solution Link: class Solution {     public List < List < Integer >> findWinners ( int [][] matches ) {         TreeMap <Integer,Integer>map1= new TreeMap < Integer , Integer >();         TreeMap <Integer,Integer>map2= new TreeMap < Integer , Integer >();         for ( int i = 0 ;i< matches . length ;i++){             int a =matches[i][ 0 ];                         if ( map1 . containsKey (a)){                 map1 . put (a, map1 . get (a)+ 1 );             }             else {                 map1 . put (a, 1 );             }         }         for ( int i = 0 ;i< matches . length ;i++){             int b =matches[i][ 1 ];                         if ( map2 . containsKey (b)){                 map2 . put (b, map2 . get (b)+ 1 );             }             else {                 map2 . put (b, 1 );             }         }         ArrayList <Inte

LCM Triplet Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/lcm-triplet1501/1 (It is also the problem of the day for 20-11-2022.) Solution: class Solution {     long lcmTriplets(long N) {         if(N<=2){             return N;         }         else if((N&1)==0){             if(N%3==0){                 N--;                 return N*(N-1)*(N-2);             }             return N*(N-1)*(N-3);         }         return N*(N-1)*(N-2);     } } Time Complexity: O(log n)[GFG Time:2.22] Space Complexity:O(1) Auxiliary Space:O(1) Total Test Cases: 1000105 Approach Used: Here we observe carefully. Then we notice one thing we need to return the multiplication of three numbers as an answer. Now the only thing is we need to identify what are those three numbers. So for that, we have different cases. If we see that the number is 2 or less than that then that number itself will be the answer. eg for 2 it will be 2 2 1 or 2 2 2 or 2 1 1 as all return the same value but it can't be 2 1 0 as i

Check if all levels of two trees are anagrams or not Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/check-if-all-levels-of-two-trees-are-anagrams-or-not/1 (It is also the problem of the day for 19-November-2022) Solution: class Solution {     public static void fun(Node root,ArrayList<Integer>arr){         if(root==null){             return ;         }         else{             Queue<Node>queue=new LinkedList<Node>();             queue.add(root);             arr.add(root.data);             while(!queue.isEmpty()){                 int a=queue.size();                 for(int i=0;i<a;i++){                     Node current=queue.remove();                     arr.add(current.data);                     if(current.right!=null){                         queue.add(current.right);                     }                     if(current.left!=null){                         queue.add(current.left);                     }                 }             }         }     }     public static boolean areAnagrams(Node node1, Node