UNIT-I
Rubric: 10m Code + 5m Viva +
5m Record
EASY
10 Marks
Session: 59:59
Bubblewrap Sandbox Active
Problem Statement
Given an array containing N distinct numbers taken from the sequence 1 to N+1, exactly one integer is missing. Your task is to identify and print the single missing integer.
Example:
If N = 5 and the sequence is [1, 2, 4, 5, 6], the missing number is 3.
Input Format
Line 1 contains a single integer N (number of elements present).
Line 2 contains N space-separated integers.
Output Format
Print a single integer representing the missing number.
Constraints
1 <= N <= 10^5
All elements are unique and in the range [1, N+1].
Status
Lang
Score
Time
Submitted
No prior submissions for this problem.
Ready
Click Run Code to evaluate against sample test
cases, or Submit Solution for full evaluation.
No compiler or execution errors recorded.
#include <stdio.h>
int main() {
int n;
if (scanf("%d", &n) != 1) return 0;
long long sum = 0;
for (int i = 0; i < n; i++) {
int val;
scanf("%d", &val);
sum += val;
}
long long total = (long long)(n + 1) * (n + 2) / 2;
printf("%lld\n", total - sum);
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n;
if (!(cin >> n)) return 0;
long long sum = 0;
for (int i = 0; i < n; i++) {
int val;
cin >> val;
sum += val;
}
long long total = (long long)(n + 1) * (n + 2) / 2;
cout << (total - sum) << endl;
return 0;
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
if (!sc.hasNextInt()) return;
int n = sc.nextInt();
long sum = 0;
for (int i = 0; i < n; i++) {
sum += sc.nextLong();
}
long total = (long)(n + 1) * (n + 2) / 2;
System.out.println(total - sum);
}
}
import sys
def main():
lines = sys.stdin.read().split()
if not lines:
return
n = int(lines[0])
nums = [int(x) for x in lines[1:n+1]]
expected = (n + 1) * (n + 2) // 2
print(expected - sum(nums))
if __name__ == "__main__":
main()