One number in the array appears twice, while another number from the range [1, n] is missing.
Our task is to find both:
Repeating Number (A)
Missing Number (B)
Example
Input: nums = [ 1 , 2 , 2 , 4 ]
Output: [ 2 , 3 ]
Enter fullscreen mode
Exit fullscreen mode
Here, 2 appears twice and 3 is missing.
Brute Force Approach
Intuition
For every number from 1 to n , count how many times it appears in the array.
If frequency is 2 , it is the repeating number.
If frequency is 0 , it is the missing number.
Time & Space Complexity
Time Complexity: O(N²)
Space Complexity: O(1)
Java Code
class Solution { public int [] findErrorNums ( int [] nums ) {
int repeating = - 1 ; int missing = - 1 ; int n = nums .…