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.
Be the first one to answer
Add answer anonymously...
Spinny Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter