C Program
#include<stdio.h> int main() { int n, x, sum=0; scanf("%d", &n); for(int i=1; i<n; i++) scanf("%d", &x), sum += x; printf("%d", n*(n+1)/2 - sum); }
C Output
Input: 5 1 2 4 5 Output: 3
C++ Program
#include<iostream> using namespace std; int main() { int n, x, sum=0; cin >> n; for(int i=1; i<n; i++) cin >> x, sum += x; cout << n*(n+1)/2 - sum; }
C++ Output
Input: 6 1 2 3 4 6 Output: 5
JAVA Program
import java.util.*; class M { public static void main(String[] a) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), sum = 0; for(int i=1; i<n; i++) sum += sc.nextInt(); System.out.print(n*(n+1)/2 - sum); } }
JAVA Output
Input: 4 1 2 4 Output: 3
Python Program
n = int(input()) a = list(map(int, input().split())) print(n*(n+1)//2 - sum(a))
Python Output
Input: 7 1 2 3 4 6 7 Output: 5
In-Depth Learning – Entire Concept in Paragraphs
What Is "Find the Missing Number"?
Problem is to determine a single lost number from a sequential sequence of numbers between 1 and n. The array provided contains n−1 numbers, and we know that they are all different and belong to the sequence. Your responsibility is to determine which number is lost. It is a popular use-case of mathematics + programming to solve problems effectively.
How the Code Works
The concept is to apply the formula for the sum of the first n natural numbers:
ini
Copy
Edit
Sum = n*(n+1)/2
Next, find the sum of the elements in the array. The difference between the expected sum and the actual sum is the missing number.
Example:
Assume n = 5
Expected sum = 1+2+3+4+5 = 15
If array = [1, 2, 4, 5] → Actual sum = 12
Missing = 15 − 12 = 3
This reasoning is O(1) space and O(n) time — ideal for interviews and contests.
Example
Input:
n = 6
Array = [1, 2, 3, 4, 6]
Expected sum = 6*(6+1)/2 = 21
Actual sum = 1+2+3+4+6 = 16
Missing number = 21 - 16 = 5
Real-Life Analogy
Let's say you are arranging numbered chairs in an examination hall. You know you have 100 students and 100 numbered slips, but you get back only 99 slips. By adding the slips you got back and equating it with the total expected, you can determine who failed to return their slip — that's the missing number.
Likewise, banks or ticketing systems employ this type of logic to identify sequence number gaps for tracking or fraud identification purposes.
Where and When Is It Used?
This problem is applied in:
Competitive programming and interviews
Integrity checks in databases
Version control systems
Distributed systems for missing data
Lost packet detection in network communication
It is also a frequent screening problem in companies such as TCS, Wipro, Infosys, Amazon, etc.
Time and Space Complexity
Operation\tComplexity
Time\tO(n)
Space\tO(1)
No need to sort, store additional structures, or search. The mathematical approach is the best solution.
Python-Specific Benefit
Python can do this calculation in a very elegant way:
python
print(n*(n+1)//2 - sum(a))
This one-line code is readable and yet very powerful. For large-scale data, this is a memory-efficient and time-saving method.
SEO-Optimized Natural Paragraph for Ranking
Want to get missing number in an array of 1 to n in C, C++, Java, or Python? This tutorial gives the neatest and most concise code samples for solving one of the most frequent interview and competitive programming problems. The problem is to find the missing number from a sequential list by applying a math formula and straightforward iteration. It's a must-mastery technique that enhances your logical thought and problem-solving. It's frequently requested by companies during screening rounds and forms a great foundation for algorithmic thinking.
Social Plugin