Project Euler #85: Counting rectangles

Sort by

recency

|

12 Discussions

|

  • + 0 comments

    JAVA

    import java.util.Scanner;
    
    public class Solution {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int tests = scanner.nextInt();
            while (tests-- > 0) {
                int target = scanner.nextInt();
                int root = (int) Math.sqrt(target);
                int bestRectangles = 0;
                int bestArea = 0;
    
                for (int x = 1; x <= root + 1; x++) {
                    int y = x;
                    int rectangles = 0;
    
                    do {
                        int area = x * y;
                        rectangles = x * (x + 1) * y * (y + 1) / 4;
    
                        if (Math.abs(bestRectangles - target) > Math.abs(rectangles - target)) {
                            bestRectangles = rectangles;
                            bestArea = area;
                        }
    
                        if (Math.abs(bestRectangles - target) == Math.abs(rectangles - target) && bestArea < area) {
                            bestArea = area;
                        }
    
                        y++;
                    } while (rectangles < target);
    
                    if (y == x + 1) {
                        break;
                    }
                }
                System.out.println(bestArea);
            }
            scanner.close();
        }
    }
    
  • + 0 comments

    100 Points C++

    #include <iostream>
    #include <cmath>
    
    int main()
    {
      unsigned int tests = 1;
      std::cin >> tests;
      while (tests--)
      {
        unsigned int target = 2000000;
        std::cin >> target;
    
        // assume x <= y, therefore x <= sqrt(limit)
        unsigned int root = sqrt(target);
        unsigned int bestRectangles = 0;
        unsigned int bestArea       = 0;
        for (unsigned int x = 1; x <= root + 1; x++) // allow slight overshooting
        {
          // start with a sqaure
          unsigned int y = x;
          // number of rectangles
          unsigned int rectangles = 0;
    
          // slowly increase y until too many rectangle in the grid
          do
          {
            unsigned int area = x * y;
    
            // the formula derived above
            rectangles = x * (x + 1) * y * (y + 1) / 4;
    
            // closer to desired number of rectangles than before ?
            if (abs(bestRectangles - target) > abs(rectangles - target))
            {
              bestRectangles = rectangles;
              bestArea       = area;
            }
    
            // prefer larger areas, too (additional requirement of Hackerrank)
            if (abs(bestRectangles - target) == abs(rectangles - target) && bestArea < area)
              bestArea = area;
    
            y++;
          } while (rectangles < target);
    
          // just a speed-up ... abortion when the inner loop exited with a square area x*y
          // => it means that no further solutions possible, area already too large
          if (y == x + 1) // plus one because y was incremented before leaving the inner loop
            break;
        }
        std::cout << bestArea << std::endl;
      }
      return 0;
    }
    
  • + 1 comment

    Is anyone having trouble in cases besides Test case 0 and Test case 5. My custom made test cases seems to give correct answers for a few one I tried. Even the custom test case with 2,000,000 (as in original euler problem) gives correct answer 2772. But all cases except 0 and 5 are showing wrong answer (except 13 and 14 which Timeout but will likley fail too I guess if did not timeout). I understand not giving test cases in the event/constest but 2 test cases could be hellpful instead of just one without compromising too much on fairness of the contect now that this has been 6 year old. If want, can remove these 2-3 testcases in counting the score.

  • + 1 comment

    I have been stuck with this for a while. What saved me was thinking about the rectangle 1999 x 1 and how many rectangles it contains ;-)

  • + 0 comments

    Once again, a problem setting where an elegant approach becomes dirty because of large number of tests, compromise in memoization and memory management.

    Hint: the count of a given rectangle is , where . Generate a list of (the upper bound can be found easily), and then a list of all results, sort them and find the nearest result(s). If it is not for the last 2 test cases, the overall performance would be much more impressive (a few ms vs a few sec).