What should be the answer for test case
1
5
1 0 7 0 10
No shouldnt it be 18?
When you consider the coder with rating 7 he is friends with all others as there are 2 on his left side and 2 on his right.
So the team should consist of 1,7 and 10?
It’s 18. The largest 3 consecutive element sum only applies to n>6
But the output of accepted code comes 17
Let me check.
Bad test cases
for _ in range(int(input())):
n=int(input())
ans=0
seq=map(int,input().split())
seq=list(seq)
if(0):
for j in range(3):
ans+=max(seq)
seq.remove(max(seq))
else:
for i in range(n):
thisans=seq[i]+seq[(i+1)%n]+seq[i-1]
ans=max(ans,thisans)
print(ans)
This passed
There are 2 edge cases
N<6 any triplet can be chosen
N=6 remember to check alternate 1 3 5 or 2 4 6.
I still didnt get the question completely. Can someone explain why is it working?
#Program 1
def fun(a):
a.sort()
return sum(a[-3:])
t=int(input())
for _ in range(t):
n=int(input())
a=list(map(int,input().split()))
ans=0
a=[a[-1]]+a+[a[0]]
for i in range(1,n+1):
ans=max(ans,sum(a[i-1:i+2]))
print(ans)
#program 2
def fun(a):
a.sort()
return sum(a[-3:])
t=int(input())
for _ in range(t):
n=int(input())
a=list(map(int,input().split()))
ans=0
if(n<=5):
a.sort()
ans=sum(a[-3:])
else:
a=[a[-1]]+a+[a[0]]
for i in range(1,n+1):
ans=max(ans,sum(a[i-1:i+2]))
print(ans)
Both solution gives AC.
Then what will be the ans for
7
5 0 10 0 6 0 12 ?
18
Why can’t it be 28 (also AC code gives ans 18)?
Because the edge people aren’t friends(12 and 10). 17 was a typo.
By edge you mean 1st and last indexed person ?
See edit above.
Was this mentioned in the problem statement?
10 and 12 are not at a distance of 1 or 2, so they aren’t friends.
the answer is 17
it’s just the maximum sum of three consecutive elements in a circular array
submission
it should be 28
Cant you take 10,6,12 as the team?
If n is equals to 3,4 or 5, answer would be sum of largest three values out of them as all elements are friend of each other.
If n >= 6, each time we can only take consecutive three elements.
But if n is 6, we need to consider one special case of (1,3,5) or (2,4,6) as these three are not consecutive elements but are friend of each other.