Team. Coco - 7 months ago 44

Python Question

"You are given an array of n integers and an integer k. Find and print the number of

`(i,j)`

`i<j and a[i] + a[j]`

Sample input would be:

`6 3`

1 3 2 6 1 2

where 6 is n, 3 is k and the second line is the array of integers. The output for this input would be 5.

Here is my code, but i am not passing the test cases and am almost positive it has to do with how i am indexing it.

`import sys`

n,k = input().strip().split(' ')

n,k = [int(n),int(k)]

a = [int(a_temp) for a_temp in input().strip().split(' ')]

count=0;

for i in range(n):

curr = n-i

for j in range(curr):

if i < i + j:

if k % (a[i] + a[i+j]) ==0:

count = count + 1

print(count)

Also, followup question: Is this method i am approaching an efficient way of going about it?

Answer

you can try this ...

```
import sys
n,k = input().strip().split(' ')
n,k = [int(n),int(k)]
a = [int(a_temp) for a_temp in input().strip().split(' ')]
print(sum([1 for i in range(n) for j in range(i) if (a[i]+a[j])%k==0]))
```

Source (Stackoverflow)