Problem solution · Java

Sorted GCD Pair Queries

Sorted GCD Pair Queries: a Java solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Direct simulation
Source
walkccc LeetCode Solutions
Length
53 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Sorted GCD Pair Queries, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 53 lines of Java from the credited upstream file 3312.java.
  • The implementation visibly relies on sequence storage.
  • 7 loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeSorted GCD Pair Queries · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  public int[] gcdValues(int[] nums, long[] queries) {    int maxNum = Arrays.stream(nums).max().getAsInt();    int[] ans = new int[queries.length];    // countDivisor[d] := the number of `nums` having `num % d == 0`    int[] countDivisor = new int[maxNum + 1];    // countGcdPair[g] := the number of pairs having gcd == g    long[] countGcdPair = new long[maxNum + 1];    // prefixCountGcdPair[g] := the number of pairs having gcd <= g    long[] prefixCountGcdPair = new long[maxNum + 1];     for (final int num : nums)      for (int i = 1; i * i <= num; ++i)        if (num % i == 0) {          ++countDivisor[i];          if (i != num / i)            ++countDivisor[num / i];        }     for (int gcd = maxNum; gcd >= 1; --gcd) {      // There are C(countDivisor[gcd], 2) pairs that have a common divisor      // that's a multiple of `gcd` (including the one that equals to `gcd`).      // So, substract the multiples of `gcd` to have the number of pairs with a      // gcd that's exactly `gcd`.      countGcdPair[gcd] = (long) countDivisor[gcd] * (countDivisor[gcd] - 1) / 2;      for (int largerGcd = 2 * gcd; largerGcd <= maxNum; largerGcd += gcd)        countGcdPair[gcd] -= countGcdPair[largerGcd];    }     for (int gcd = 1; gcd <= maxNum; ++gcd)      prefixCountGcdPair[gcd] = prefixCountGcdPair[gcd - 1] + countGcdPair[gcd];     for (int i = 0; i < queries.length; ++i)      ans[i] = getNthGcdPair(queries[i], prefixCountGcdPair);     return ans;  }   // Returns the `query`-th gcd pair.  private int getNthGcdPair(long query, long[] prefixCountGcdPair) {    int l = 1;    int r = prefixCountGcdPair.length - 1;    while (l < r) {      int m = (l + r) / 2;      if (prefixCountGcdPair[m] < query + 1)        l = m + 1;      else        r = m;    }    return l;  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗