<p>You have a set of integers <code>s</code>, which originally contains all the numbers from <code>1</code> to <code>n</code>. Unfortunately, due to some error, one of the numbers in <code>s</code> got duplicated to another number in the set, which results in <strong>repetition of one</strong> number and <strong>loss of another</strong> number.</p>

<p>You are given an integer array <code>nums</code> representing the data status of this set after the error.</p>

<p>Find the number that occurs twice and the number that is missing and return <em>them in the form of an array</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,2,4]
<strong>Output:</strong> [2,3]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [1,1]
<strong>Output:</strong> [1,2]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution:
    def findErrorNums(self, nums):
        n = len(nums)
        
        # Compute expected sums and squares
        expected_sum = n * (n + 1) // 2
        expected_sum_sq = n * (n + 1) * (2 * n + 1) // 6
        
        # Compute actual sums and squares
        actual_sum = sum(nums)
        actual_sum_sq = sum(x * x for x in nums)
        
        # Calculate the differences
        diff_sum = expected_sum - actual_sum
        diff_sum_sq = expected_sum_sq - actual_sum_sq
        
        # Solve for X and Y
        # diff_sum_sq = (Y^2 - X^2) = (Y - X) * (Y + X)
        # So: diff_sum_sq / diff_sum = Y + X
        sum_xy = diff_sum_sq // diff_sum
        
        # Now we can solve:
        # Y - X = diff_sum
        # Y + X = sum_xy
        # Adding these equations gives:
        # 2Y = diff_sum + sum_xy
        # So Y = (diff_sum + sum_xy) // 2
        Y = (diff_sum + sum_xy) // 2
        # Subtracting these equations gives:
        # 2X = sum_xy - diff_sum
        # So X = (sum_xy - diff_sum) // 2
        X = (sum_xy - diff_sum) // 2
        
        return [X, Y]

# Example usage:
sol = Solution()
print(sol.findErrorNums([1, 2, 2, 4]))  # Output: [2, 3]
print(sol.findErrorNums([1, 1]))  # Output: [1, 2]

17900