Interview Questions/Coding/Strongest Common Factor

Strongest Common Factor

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Strongest Common Factor

You are given an array of positive integers. Choose two different positions in the array and calculate the greatest common divisor of the two chosen numbers. Return the largest GCD value that can be obtained from any such pair.

Examples

Example 1
Input:
5
3 14 15 7 9
Output:
7

Approach hint

Instead of checking every pair, think about checking possible GCD values from largest to smallest.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp