Flatten 2D Array Problem
Implement an iterator called ‘FLATTEN_2D’ to transform a two-dimensional array ‘ARR_2D’ into a one-dimensional array ‘ARR_1D’. The iterator should support the following operations:
- NEXT: Returns the next element in the row-wise traversal of ‘ARR_2D’. The first call should return the first element from ‘ARR_2D’.
- HAS_NEXT: Returns ‘true’ if there are more elements to traverse; otherwise, return ‘false’ if the iteration is complete.
Example:
Input:
ARR_2D = [ [0, 1], [2, 3, 4], [], [5] ]
Output:
ARR_1D = [0, 1, 2, 3, 4, 5]
Explanation:
The elements of the 2D array are traversed row-wise and combined into a single 1D array.
Input Format:
The first line of input contains an integer ‘T’ indicating the number of test cases. Each test case consists of:
- An integer ‘N’ indicating the number of rows in ‘ARR_2D’.
- For each row, an integer ‘M’ indicating the number of columns followed by ‘M’ space-separated integers representing the row elements.
Output Format:
For each test case, output the elements of ‘ARR_1D’ on a separate line.
Constraints:
- 1 <= T <= 10
- 1 <= N <= 100
- 0 <= M <= 100
- -10^6 <= Value in each element of ‘ARR_2D’ <= 10^6
Note:
You are not required to print the output; your task is to implement the function for these operations.

AnswerBot
4mo
Implement an iterator to flatten a 2D array into a 1D array in row-wise traversal.
Create a class 'FLATTEN_2D' with 'NEXT' and 'HAS_NEXT' methods.
Maintain current row and column indices to keep track o...read more
Help your peers!
Add answer anonymously...
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

