infj1's blog

By infj1, history, 6 months ago, In English

So recently i started doing competitive programming after a break. What I notice from practice or participation in a contest is that, I'm not able to solve B problem in div2 and C in div3 and finally C(sometimes) and D in div4. Are there any resources that contain the topics to solve these problems?

I felt bad for not solving a problem after taking a long time on it(especially div2 B). Could you guys suggest any tips and resources? Also as a newbie , should I spend a lot of time on a problem before looking at the editorial ?

Any suggestions would be helpful! Thanks

Full text and comments »

  • Vote: I like it
  • -9
  • Vote: I do not like it

By infj1, history, 11 months ago, In English

Given an array of length n containing only zeros and ones, you can perform operations by removing either the first or the last element of the array. Calculate the minimum number of operations required to achieve a total sum of s in the array. If it is not possible to obtain the sum s after any number of operations, the output should be -1.

The code below is my approach. But when I submit its giving wrong answer. Could you guys what's the problem with the code? Or the approach?

for _ in range(int(input())):

n,s=[int(i) for i in input().split()]

a=[int(i) for i in input().split()]

c=0

for i in range(n):


    if a[i]==1:
        c+=1


if c<s:
    print(-1)
else:
    l=0
    r=n-1
    req=c-s
    got=0
    ans=0
    if got==req:
        print(0)
        continue
    while l<r and got<req:
        old_l=l
        while l<r and a[l]==0:
            l+=1
        old_r=r
        while r>l and a[r]==0:
            r-=1
        l_dis=l-old_l
        r_dis=old_r-r
        if l_dis<r_dis:
            r=old_r
            ans+=l_dis+1
            l+=1
        else:
            l=old_l
            ans+=r_dis+1
            r-=1
        got+=1
    print(ans)

Full text and comments »

  • Vote: I like it
  • -4
  • Vote: I do not like it

By infj1, history, 15 months ago, In English

Hi guys. I have been doing cp for a while and i am not able to implement even though if i have the logic... And also , solving A tasks are sometimes pretty easier . But i want to improve ... Can you guys give some suggestions? I was doing leetcode for some time but i wanted to solve questions on codeforces... Because one of my friends told that the questions are ad-hoc and sometimes convoluted. And pls suggest some math resources for cp. It seems like most of the questions on codeforces are based on mathematical thinking. (I am very bad at it but i want to improve).

Thanks in advance :)

Full text and comments »

  • Vote: I like it
  • +1
  • Vote: I do not like it