Hlp needed in SUMAGCD

Here i took element by element from the array and added it to the gcd of remaining elements by creating prefix and suffix array.But i am getting wa can someone fing the bug in my code. My solution link is
https://www.codechef.com/viewsolution/24755575

For n>2 after sort-unique-resize, ur ans should be max(a[0]+gcd(a[1],hcf) , a[1]+gcd(a[0],hcf))
hcf=gcd(a[2],a[3],…a[n-1])
(Note:- considering sort is in descending order so that a[0] or a[1] are greatest elements of the array)
My soln:
https://www.codechef.com/viewsolution/24727209

Thank you i got ur solution.But i am unable to find the bug in my code. I am taking every element seperately so it includes the two highest elements also.

check your code for the case
1
3
7 7 7
ans : 8
i don’t think you handled this case.

@rama_545 for your test case the answer should be 14 as we have two groups (7,7) and (7) . my code is giving the same answer.

Unique deletes one instance per two consecutive instances, so to get the number of distinct integers u should sort the vector before applying unique,so as to get every instance of every integer adjacent.
For example: test case- 18 18 9 17 18 9 9 17 17
N should be 3, but ur code will give n=6.
Correct ans= 17+9
Ur ans= 18+1
Hope this helps.

@derco thank you so much for ur help.

1 Like