Given an unsorted array Arr of size N of positive integers. One number ‘A’ from set {1, 2, …N} is missing and one number ‘B’ occurs twice in array. Find these two numbers.
Example 1:
Input: N = 2 Arr[] = {2, 2} Output: 2 1 Explanation: Repeating number is 2 and smallest positive missing number is 1.
Example 2:
Input: N = 3 Arr[] = {1, 3, 3} Output: 3 2 Explanation: Repeating number is 3 and smallest positive missing number is 2.
Your Task:
You don’t need to read input or print anything. Your task is to complete the function findTwoElement() which takes the array of integers arr and n as parameters and returns an array of integers of size 2 denoting the answer ( The first index contains B and second index contains A.)
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 105
1 ≤ Arr[i] ≤ N
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # https://practice.geeksforgeeks.org/problems/find-missing-and-repeating2512/1# class Solution: def findTwoElement( self,arr, n): zeroArray = [0]*(n+1) sol = [0,0] count = 0 for i in range(0, n): zeroArray[arr[i]] +=1 for i in range(1,len(zeroArray)): if count ==2: break if zeroArray[i] ==2: sol[0] = i count+=1 elif zeroArray[i] ==0: sol[1] = i count+=1 return sol # code here |