FastPrepFind Pair with Maximum GCD

Find Pair with Maximum GCD

TikTok logoTikTok● MediumOA

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

See TikTok hiring insights
Saved
public int findPairWithMaximumGCD(int[] arr) {
  // write your code here
}
arr[1, 2, 3, 4, 8, 9]
expected4
Checking account…