- Related Questions & Answers
- Program to find all missing numbers from 1 to N in Python
- Find count of Almost Prime numbers from 1 to N in C++
- C Program to print numbers from 1 to N without using semicolon
- Print prime numbers from 1 to N in reverse order
- Program to find kth lexicographic sequence from 1 to n of size k Python
- Find four missing numbers in an array containing elements from 1 to N in Python
- An Interesting Method to Generate Binary Numbers from 1 to n?
- Compute sum of digits in all numbers from 1 to n
- Maximum XOR using K numbers from 1 to n in C++
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n in C++
- C++ program to find the sum of the series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Find four missing numbers in an array containing elements from 1 to N in C++
- Program to find sum of prime numbers between 1 to n in C++
- Python program to count total set bits in all number from 1 to n.
- Smallest possible number divisible by all numbers from 1 to n in JavaScript

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a list of numbers called nums of length n + 1. These numbers are picked from range 1, 2, ..., n. As we know, using the pigeonhole principle, there must be a duplicate. We have to find that and return it.

So, if the input is like [2, 1, 4, 3, 3], then the output will be 3

To solve this, we will follow these steps −

- l := size of nums
- temp := l*(l-1) /2
- temp_sum := sum of all elements in nums
- return (temp_sum - temp)

Let us see the following implementation to get better understanding −

class Solution: def solve(self, nums): l = len(nums) temp = l*(l-1)/2 temp_sum = sum(nums) return temp_sum-temp ob = Solution() print(ob.solve([2, 1, 4, 3, 3]))

[2, 1, 4, 3, 3]

3

Advertisements