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 for grouping will not be in starting of another group. So it means that whatever will be the last number used in grouping, you need to add +1 in that for making next group.


This was the main logic of code.

Now if you look at the code then you will easily understand the usage of variable "pointer2".


Uses of variable used:

1.Loop of i from 1 to n+1 is simply iterating for n numbers.

2.pointer variable is used to identify that what is the number of numbers we want. simply the value of i variable of loop.

3.val=1 is used for multiplication 

4.mul will be pointing to pointer2 so as to start from the last restored value.(Last restored value is the value which was used as the last member of group).

So every time mul starts with the fresh value and not with the earlier used one.

5.Running loop from j=0 to j<pointer signifies the number of elements required.

6.mod is used to make answer within range.

7.sum variable is used to do sum of all products.


eg if n=3

sum=0;

pointer=0;

pointer2=1;

for(int i=1;i<3+1;i++){

        //this will be the loop for i=1

        val=1;

    pointer=1(=i);

        mul=1(=pointer2);

        for(int j=0;j<1;j++){

                val=1*1;

                mul=2;(mul++)

        }    

      sum=1(=val);

        pointer2=2(=mul);


----------------------------------------------------------------------------------------------------------------------------

    //this will be for i=2

        val=1;

    pointer=2(=i);

        mul=2(=pointer2);

        for(int j=0;j<2;j++){

                val=1*2;

                mul=3;(mul++)

        }    

        here val updated to 2;

        for(int j=1;j<2;j++){

                val=2*3;

                mul=4;(mul++)

        }    

       sum=1+6=7;

        pointer2=4(=mul);

}

----------------------------------------------------------------------------------------------------------------------------

//this will be for i=3

        val=1;

    pointer=3(=i);

        mul=4(=pointer2);

        for(int j=0;j<3;j++){

                val=1*4;

                mul=5;(mul++)

        }    

        here val updated to 4;

        for(int j=1;j<3;j++){

                val=4*5;

                mul=6;(mul++)

        }    

        here val updated to 20;

        for(int j=1;j<3;j++){

                val=20*6;

                mul=7;(mul++)

        }    

       val=120

       sum=7+120=127;

        pointer2=7(=mul);

}


In this way code will work.



Time Complexity: O(N*N)

Space Complexity: O(1)

Auxiliary Space: O(1)


"Thanks For Reading.😇"

"Share Further To Increase Knowledge Treasure.😊"

Comments

Popular posts from this blog

Solutions Of Practice Questions Dated 01-06-2022

CODEFORCES SPY DETECTED ROUND 713

Maximum Winning Score Geeks For Geeks