Posts

Recursive Sequence Geeks For Geeks Problem Of The Day 12-02-2024

Problem Link: https://www.geeksforgeeks.org/problems/recursive-sequence1611/1 Code: class Solution{     static long sequence(int n){                 long sum=0;         int pointer=0;                  int mod=1000000007;         int pointer2=1;                  for(int i=1;i<n+1;i++){            pointer=i;            long val=1;            int mul=pointer2;            for(int j=0;j<pointer;j++){                val=(val*mul)%mod;                mul++;            }            pointer2=mul;            sum=(sum+val)%mod;         }         return sum;     } } Solution Approach: Here we need to find out the sum of product of consecutive numbers up to a particular range. 1.In problem it is given that if i==2 then it means 2 consecutive numbers product and later on their product should be added in overall sum. Now the major thing is to identify that these 2 consecutive numbers will be what ie (1,2) or (2,3) or (3,4) so for that they have given condition that number which was used last fo

Java Date And Time HackerRank

CODE:   public   static  String findDay( int  month,  int  day,  int  year) {         String finalday= " " ;          try {             String d1= day+ "/" +month+ "/" +year;             SimpleDateFormat sdf= new  SimpleDateFormat( "dd/MM/yyyy" );             Date date1=sdf.parse(d1);             DateFormat format1= new  SimpleDateFormat( "EEEE" );            finalday=format1.format(date1);              // return finalday;         }          catch (Exception e){             System.out.println(e);         }                 return  finalday.toUpperCase();                                }

Good Subtrees Problem Of The Day GFG

Problem Link: https://practice.geeksforgeeks.org/problems/df12afc57250e7f6fc101aa9c272343184fe9859/1 Solution : class Solution {     // static LinkedHashSet<Integer> set1=new LinkedHashSet<Integer>();     // static LinkedHashSet<Integer> set2=new LinkedHashSet<Integer>();         public static LinkedHashSet<Integer> fun(Node root, int k,int[] count){         if(root==null){             return new LinkedHashSet<Integer>();         }         else{             LinkedHashSet<Integer> set1=fun(root.left,k,count);             LinkedHashSet<Integer> set2=fun(root.right,k,count);             set1.addAll(set2);             set1.add(root.data);                         if(set1.size()<=k){                 // System.out.println(set1);                 count[0]++;             }             return set1;         }     }          public static int goodSubtrees(Node root,int k)     {         if(root==null){             return 0;         }         else{  

Make array elements unique. Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/6e63df6d2ebdf6408a9b364128bb1123b5b13450/1 It is also the problem of the day for 11-01-2023 Solution: class Solution {     public long minIncrements(int[] arr, int n) {        long count=0;        Arrays.sort(arr);     //   for(int a:arr){     //       System.out.print(a+" ");     //   }        HashMap<Integer,Integer>map=new HashMap<Integer,Integer>();       int index=1;       for(int a:arr){           if(index<a){               index=a;               index++;               map.put(a,1);               //condition1           }           else if(index==a){               index++;               map.put(index,1);               //condition2           }           else{               map.put(index,1);               count+=index-a;               index++;               //condition3           }         //   System.out.print(count+" ");       }     //   System.out.println(map);       return count;     }

3 Divisors Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/3-divisors3942/1 It is also the problem of the day for 8-12-2022 Solution: //{ Driver Code Starts //Initial Template for Java import java.io.*; import java. util.*; class GFG {     public static void main(String args[])throws IOException     {         Scanner sc = new Scanner(System.in);         int t = sc.nextInt();         while(t-->0){             int q = sc.nextInt();             ArrayList<Long> query = new ArrayList<>();             for(int i=0;i<q;i++){                 query.add(sc.nextLong());             }             Solution ob = new Solution();                              ArrayList<Integer> ans = ob.threeDivisors(query,q);             for(int cnt : ans) {                 System.out.println(cnt);             }         }     } } // } Driver Code Ends //User function Template for Java class Solution{     static ArrayList<Long>aa=new ArrayList<Long>();          //for checking prime    

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