Rice Bags

Problem Overview

  • Given distinct rice bag grain counts, find the largest “perfect” set where, in sorted order, each bag’s grain count squared equals the next, with at least two bags.
  • Input: array riceBags of size n (1 ≤ n ≤ 2×10^5), with 2 ≤ riceBags[i] ≤ 10^6; Output: size of the largest such set, or -1 if none.
  • Real-world context: choosing bags on Amazon.com based on grain counts to form a valid progression.
  • Amazon interviews coding interview problem and a common interview question for candidates.

Example

Unlock to view complete problem details

and practice with sample input/output

Was this article helpful?

View Test Cases & Run Code requires membership

Input Variables
Execution Result: