Find Missing Number in Sequence
Find missing number in array containing 1 to n with one missing.
AdvancedPhase 4 - Level 5: Logical & AppliedExample 13 of 14
find-missing-number-in-sequence.py
1# Take array2n = int(input("Enter n (expected range 1 to n): "))3arr = []4for i in range(n - 1):5 arr.append(int(input(f"Element {i+1}: ")))67# Calculate expected sum8expected_sum = n * (n + 1) // 29actual_sum = sum(arr)10missing = expected_sum - actual_sum1112print(f"Missing number: {missing}")
Output
Enter n (expected range 1 to n): 5 Element 1: 1 Element 2: 2 Element 3: 4 Element 4: 5 Missing number: 3
What's going on
Use sum formula to find missing number.
Key Concepts:
Expected sum = n*(n+1)/2
Actual sum from array
Missing = expected - actual
🔗Related Content
- 📖
Phase 4 - Learn Concepts
Review Phase 4 concepts and explanations
- 📝
Phase 4 - Quiz
Test your Phase 4 understanding with quiz questions
- 💻
Phase 4 - All Practice Problems
Explore all practice problems for Phase 4
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum