Max GCD Pair Problem Statement

Given an array of positive integers, determine the Greatest Common Divisor (GCD) of a pair of elements such that it is the maximum among all possible pairs in the array. The GCD of two numbers a and b is the greatest number x that divides both a and b without leaving a remainder.

Example:

Input:
arr = [1, 5, 2, 3, 4]
Output:
2
Explanation:

The maximum GCD found is 2, which is obtained from the pair (2, 4). All other pairs yield a GCD of 1.

Input:

 The first line of input contains a single integer 'T', denoting the number of test cases. Each test case includes: - An integer 'N' indicating the size of the array. - 'N' space-separated positive integers that represent the elements of the array.

Output:

 For each test case, output the maximum GCD value on a new line.

Constraints:

  • 1 <= T <= 10
  • 2 <= N <= 10^4
  • 1 <= ARR[i] <= 10^5
Note:

No input or output functions need to be handled; implement the function to solve the problem as described.

AnswerBot
2d

Find the maximum GCD of a pair of elements in an array of positive integers.

  • Iterate through all pairs of elements in the array

  • Calculate the GCD of each pair using Euclidean algorithm

  • Keep track of the ...read more

Help your peers!
Add answer anonymously...
PhonePe Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter