DIVKIDS - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: raysh07
Tester: sushil2006
Editorialist: iceknight1093

DIFFICULTY:

Cakewalk

PREREQUISITES:

None

PROBLEM:

There are N candy jars; the i-th has A_i candies.
Find the largest possible number of candies that can be obtained by choosing exactly one jar; such that the candy count is divisible by X.
If there’s no valid jar, print 0 instead.

EXPLANATION:

It’s enough to simply implement what’s asked for.

For each i from 1 to N,

  • If A_i is not divisible by X, this is not a valid jar to choose. Ignore it.
    • In most languages, this can be checked using the condition a[i] % x == 0
  • If A_i is divisible by X, this is a candidate jar to be picked.

Track the largest number of candies across all candidate jars, and print that as the answer.

TIME COMPLEXITY:

\mathcal{O}(N) per testcase.

CODE:

Editorialist's code (PyPy3)
for _ in range(int(input())):
    n, x = map(int, input().split())
    a = list(map(int, input().split()))
    ans = 0
    for i in range(n):
        if a[i] % x == 0:
            ans = max(ans, a[i])
    print(ans)