Problem · Math

Find Pair with Maximum GCD

Learn this problem
MediumTiktokOA
See Tiktok hiring insights

Problem statement

Given an integer array arr, choose two elements at distinct indices and return the maximum possible greatest common divisor (GCD) of the pair.

Function

findPairWithMaximumGCD(arr: int[]) → int

Examples

Example 1

arr = [1, 2, 3, 4, 8, 9]return = 4

The pair (4, 8) has GCD 4, which is the maximum possible.

Example 2

arr = [1, 2, 3, 5]return = 1

The pair (3, 5) has GCD 1. Every pair in the array has GCD 1.

Constraints

  • arr contains at least two elements.
  • Every element of arr is a positive 32-bit integer.

More Tiktok problems

drafts saved locally
public int findPairWithMaximumGCD(int[] arr) {
  // write your code here
}
arr[1, 2, 3, 4, 8, 9]
expected4
checking account