|
U
Pro21. Print Array Elements with Indices and Count
Easy
Problem Description
Given an array of N 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 N (0≤N≤105).
- •Line 2: N space-separated integers A0,A1,…,AN−1. (Omitted if N=0).
Output Format
- •N 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
Code
|Loading Code Editor...
SavedLn 1, Col 1
param_1 =
param_2 =
Sandboxed Engine
Ctrl+Enter = Run | Ctrl+Shift+Enter = Submit