Q1 Sakurako's Exam: The number of ones must be even or the number of twos must be even while the number of ones is not zero. This ensures that we can balance the array to achieve a sum of zero.
Q2 Square or Not: Check if it's length is a perfect square and if the number of ones in the string equals 4*r-4
, where r
is the square root of the length. If both conditions are met, the string can form into a beautiful matrix, and if not it cannot form.
Q3 Longest Good Array: first recognize that the maximum length of a good array is determined by the sum of the first k
natural numbers fitting within the boundary [l, r]
. Use a while loop to incrementally build the sum and track the largest k
where the sum remains within the allowed range.
Q4 Sakurako's Hobby: Traverse each index in the permutation and count the number of black integers reachable from that index by following the permutation links. I would use memoization to store results for each index to avoid redundant calculations and efficiently handle multiple test cases by processing each permutation and its associated color information separately.