Problem 3289: The Two Sneaky Numbers of Digitville Problem Statement In the town of…
- User
Problem 3289: The Two Sneaky Numbers of Digitville
Problem Statement
In the town of Digitville, there was a list of numbers called
numscontaining integers from 0 ton - 1. Each number was supposed to appear exactly once in the list, however, two mischievous numbers sneaked in an additional time, making the list longer than usual.Task
As the town detective, your task is to find these two sneaky numbers. Return an array of size two containing the two numbers (in any order), so peace can return to Digitville.
Difficulty
- Easy
Examples
Example 1
- Input:
nums = [0,1,1,0] - Output:
[0,1] - Explanation: The numbers 0 and 1 each appear twice in the array.
Example 2
- Input:
nums = [0,3,2,1,3,2] - Output:
[2,3] - Explanation: The numbers 2 and 3 each appear twice in the array.
Example 3
- Input:
nums = [7,1,5,4,3,4,6,0,9,5,8,2] - Output:
[4,5] - Explanation: The numbers 4 and 5 each appear twice in the array.
Notes
- The input list contains integers from 0 to
n - 1, wherenis the length of the input list. Two numbers appear twice in the list, and the rest appear once.
Q-can you explain it using your own words ? Oky so we are given a list of integers , the common rules that each number in this list most appear at most 1 times But 2 malicious integers does not respect this rule … You task is to find them and return theme as list Q- what is the input/output format ? Input : List[int] Output : List[int]
Q- can you go through examples ?
Input : [0,3,2,1,3,2]
The rule tell use that the numbers within this list So looking at the list we see that only 3 and 2 are appears more then one time So we can return [2,3] as a result to this example
Q- do have any technique , pattern , particul ds to solve this ? I think this problem can be solving by two steps Step 1 : we need to sort the list to facilte the search for duplicate element Step 2 : search the elements that appear more than one time by computing the two adjacents ones
Q- can you more formal pesudoCode ?
Functions : def getSneakyNumbers(self, nums: List[int]) -> List[int]: // Q- why we need to compute the length of the list // A- it help us traverse the list more easily and make the code more readable n=nums.length() // Q- why we need to sort the input list // A- it facilitate the detection of duplicate element , by traverse the list paiwize with adjancent values nums.sort() // Q- why we need to initiate a an empty list // A- it help us store the anomalies values result=[] // Q- why we use for loop not while loop // A- because the problem is more iteration problem than traversal one , and also the length of the input collection is already known (computed ) For i from 1 to n // Q- why you start from the second element // A- it a choice not mandatory but the like the pattern current & prev to check the duplication problem in a collection If nums[i]==nums[i-1]: // Q-why we you add it to the list when the if condition is true // A- because if two adjance elements are equal we can infer that the element are anomaly result.append(nums[i]) Return result
- Assistant