Adding Ones- GFG

 It's around 11:45 and I just finished the submission of this Problem, actually I had made the code for this problem yesterday only, it was just that I couldn't submit it at that time.

I have my DSLA exam tomorrow the preparation for which I finished just 20 mins. ago, overall the sub was quite easy yeah if I compare it to IDS which is really a hard stuff for me cause it's too much theory which I was preparing yesterday, just 2-3 topics, cause I am not well prepared for IDS, and ofcourse when I switch from IDS to DSLA it's like this logical and senseful stuff seems too easy to me.

Mainly if I would list the topics I am well prepared in DSLA then they would be following:-

UNIT-2: 

The Group theory stuff, Numeric Funcs, Recurrence Relations.

Left Ring Theory Part.

UNIT-4:

Trace  and all, 3 types of Decompositions-Eigen, singular value, and Cholesky, then Determinant Stuff.

Just realised I missed the Gradient Part...

UNIT-5:

The Hypothesis Part- t test, z test, f test, then THE ANOVA Test. Type 1 and Type 2 Errors( just the theoretical understanding ).

Overall I think that these units had quite simple topics, if we leave the group and ring theory part, cause I just hate the proving kind of stuff and specially when it's like this, where there is no sense at all, just proving closure,associative,identity and blah blah.

Considering if we include all the units, thinkin about the end sem, the units 1 and 3 will be quite beneficial for me cause I had prepared them before this semester only, so they would be helpful,

and considering this I think that out of the 5 subs, OD,ADA and now DSLA are all fine for me, then IDS and SE... I have been working on the theory of SE from past 1 or 2 week alternate days, but still I think about 4-5 topics are left, then IDS is like read and forget kind of stuff, I'll have to just work on IDS after mid-sem, maybe 3-4 will be fine, and then I will be quite prepared for the End sem. I am thinking of finishing everything from before so that I don't have to give the whole days during the exams, ofcourse revision will be going on but then I would also be able to do the streak stuff of GFG I have started a week ago.

So, that's the plan for now, will see if it works. 

Coming to this Problem I think it's quite self explanatory. Well, the problem statement and the code is given below.

I would just say that don't go for the first approach that comes to your mind, I did that mistake too, yes I know what it will be.

Problem Statement:-

You start with an array A of size N. Initially all elements of the array A are zero. You will be given K positive integers. Let j be one of these integers, you have to add 1 to all A[i], where i ≥ j. Your task is to print the array A after all these K updates are done.

Note:
 1-based indexing is followed for updates.

Example 1:

Input:
N = 3, K = 4
1 1 2 3
Output:
2 3 4
Explanation:
Initially the array is {0, 0, 0}. After the
first 1, it becomes {1, 1, 1}. After the
second 1 it becomes {2, 2, 2}. After 2, 
it becomes {2, 3, 3} and
After 3 it becomes, {2, 3, 4}.

Example 2:

Input:
N = 2, K = 3
1 1 1
Output:
3 3 
Explanation:
Initially the array is {0, 0}. After the
first 1, it becomes {1, 1}. After the
second 1 it becomes {2, 2}. After the
third 1, it becomes {3, 3}.

Your Task:  
You don't need to read input or print anything. Your task is to complete the function update() which takes the array A[] and its size N as inputs and make the updates and fill the array A[].

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N, K ≤ 105

1 ≤ updates[i] ≤ N, for all i from 1 to N.


Code Implementation:-

class Solution:

    def update(self, a, n, updates, k):

        # Your code goes here

        dp=[0]*n

        for update in updates:

            # Whichever index we have, just traverse from that index-1 to the end of the a array, that is till n

            # for i in range(update-1,n):

            #     a[i]+=1

            dp[update-1]+=1

        # for ind,upd in enumerate(dp):

        #     for i in range(ind,n):

        #         a[i]+=upd

        

        a[0]=dp[0]

        for i in range(1,n):

            a[i]+=a[i-1]+dp[i]

        # print(dp)

        # print(a)

Comments

Popular Posts