Convert 1D Array Into 2D Array

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.
I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.
You are given a 0-indexed 1-dimensional (1D) integer array original, and two integers, m and n. You are tasked with creating a 2-dimensional (2D) array with m rows and n columns using all the elements from original.
The elements from indices 0 to n - 1 (inclusive) of original should form the first row of the constructed 2D array, the elements from indices n to 2 * n - 1 (inclusive) should form the second row of the constructed 2D array, and so on.
Return an m x n 2D array constructed according to the above procedure, or an empty 2D array if it is impossible.
LeetCode Problem - 2022
class Solution {
// Method to convert a 1D array (original) into a 2D array with m rows and n columns
public int[][] construct2DArray(int[] original, int m, int n) {
// Check if the total number of elements matches m * n
// If not, return an empty 2D array
if (m * n == original.length) {
// Create the 2D array to store the result
int[][] answer = new int[m][n];
// Index to keep track of the position in the original 1D array
int idx = 0;
// Fill the 2D array with elements from the original array
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// Assign elements from the original array to the 2D array
answer[i][j] = original[idx];
idx++; // Move to the next element in the 1D array
}
}
// Return the constructed 2D array
return answer;
} else {
// If the number of elements doesn't match, return an empty 2D array
return new int[0][0];
}
}
}




