Find The Repeating And Missing Number
You are given an array 'nums' consisting of first N positive integers. But from the N integers, one of the integers occurs twice in the array, and one of the integers is missing. You need to determine the repeating and the missing integer.
Example:
Let the array be [1, 2, 3, 4, 4, 5]. In the given array ‘4’ occurs twice and the number ‘6’ is missing.
Input format:
The very first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of every test case contains an integer ‘N’ denoting the number of elements present in the array.
The second line of every test case contains ‘N’ space-separated integers denoting the elements present in the array.
Output format:
For each test case, print the two space-separated integers denoting the repeating and the missing numbers, in the same order.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Follow Up:
Can you solve this in O(N) time and O(1) space complexities?
Constraints:
1 <= T <= 100
2 <= N <= 10^4
1 <= nums[i] <= N
Time Limit: 1 sec
CodingNinjas
author
2y
Sort the Array
- A simple and intuitive approach could be to sort the given array in ascending order.
- Now, in order to determine the repeating and the missing numbers, we check the adjacent pair of elemen...read more
CodingNinjas
author
2y
Using Frequency Array
- The idea here is that all the numbers except the missing and the repeating number will have a frequency count equal to 1.
- The missing number will have zero frequency whereas the re...read more
CodingNinjas
author
2y
Using Sum and Sum of Squares of Numbers
- The idea here is to generate two equations using the sum and sum of squares of the N numbers and use them to determine the repeating and the missing element.
- Let ...read more
CodingNinjas
author
2y
Using XOR Operation
- The idea here is to use the property of XOR operations which is, XOR of the two equal numbers results in zero, i.e. the numbers cancel out each other.
- Firstly, we calculate the XOR o...read more
CodingNinjas
author
2y
Using Given Array to Track Visited Numbers
- The idea here is to use the given array itself to keep track of visited numbers.
- This can be done by using the absolute value of a number as an index and marki...read more
Add answer anonymously...
Top Oracle Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app