My issue
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
mpa=a[:]
res=0
for j in range(n-1,-1,-1):
if a[j]-1!=0 and (3a[j])%(a[j]-1) == 0:
val=(3a[j])//(a[j]-1)
if val in mpa: res+=1
mpa.pop(j)
print(res)
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
mpa=a[:]
res=0
for j in range(n-1,-1,-1):
if a[j]-1!=0 and (3a[j])%(a[j]-1) == 0:
val=(3a[j])//(a[j]-1)
if val in mpa: res+=1
mpa.pop(j)
print(res)
My code
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
mpa=a[:]
res=0
for j in range(n-1,-1,-1):
if a[j]-1!=0 and (3*a[j])%(a[j]-1) == 0:
val=(3*a[j])//(a[j]-1)
if val in mpa: res+=1
mpa.pop(j)
print(res)
Why is this failing?
Problem Link: Freedom Practice Coding Problem - CodeChef
I don’t really understand your approach
What you were supposed to figure out was how to identify the indexes that make the arithmetic progression according to the constraints. Let’s do that first:
a = p - q
b = p + q
c = p * q
b + (b - a) = c
2b = c + a
2p + 2q = p * q + p - q
p + 3q = p * q
(p/q) + 3 = p
That 3 in your conditional make me think you had a similar idea or closer. Let’s complete the idea.
we know:
p and q are positives
p must be divisible by q
By many means, you could’ve noted that these numbers must be between 1 and 10, otherwise the multiplication would be very big. I flipped a coin making a graph
Only (4,4) and (6,2) make it.
Your task was basically to find all the possible indexes, that i < j, where we could have a pair of 4, or first 6 and then 2.
Therefore:
# cook your dish here
for T in range(int(input())):
N = int(input())
A = list(map(int,input().strip().split()))
four = A.count(4)
if four > 0:
four -= 1
four = four*(four+1)//2
six = 0
s_t = 0
for a in A:
if a == 6:
six += 1
elif a == 2:
s_t += six
print(s_t + four)