U

21. Print Array Elements with Indices and Count

Easy

Problem Description

Given an array of NN integers, print each element along with its 0-based index formatted as Index {i}: {value} on separate lines. Finally, print Total elements: {N}.

Input Format

  • •Line 1: An integer NN (0≤N≤1050 \le N \le 10^5).
  • •Line 2: NN space-separated integers A0,A1,…,AN−1A_0, A_1, \dots, A_{N-1}. (Omitted if N=0N = 0).

Output Format

  • •NN lines: Index i: A[i]
  • •Final line: Total elements: N
Example 1:
Input:
3
10 20 30
Output:
Index 0: 10
Index 1: 20
Index 2: 30
Total elements: 3
Explanation: Elements at 0, 1, 2 printed with total count 3.
Example 2:
Input:
1
42
Output:
Index 0: 42
Total elements: 1
Explanation: Single element at index 0.
Example 3:
Input:
0
Output:
Total elements: 0
Explanation: Empty array outputs only count 0.
Constraints:
  • Standard integer bounds: 32-bit signed integer or input range [1, 10^5].
  • Time complexity target: O(N) or O(N log N) where applicable.
  • Auxiliary space target: O(1) or minimal auxiliary memory.
Follow up:Can this algorithm be adapted to an online streaming setting where the entire input cannot be held in memory simultaneously?
1,180 Online