diff --git a/2d-array_questions/2darray.cpp b/2d-array_questions/2darray.cpp new file mode 100644 index 0000000..eac89e9 --- /dev/null +++ b/2d-array_questions/2darray.cpp @@ -0,0 +1,26 @@ +#include +#include +using namespace std; +int main() +{ + int m, n; + cin >> m >> n; + vector> arr(m, vector(n)); // for 2d array since we want any no od ro3ws and columns so using vectors + // for takihg input traverse + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + cin >> arr[i][j]; + } + } + // for output + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + cout << arr[i][j] << " "; + } + cout << "\n"; + } +} \ No newline at end of file diff --git a/2d-array_questions/binary_search_2d_array.cpp b/2d-array_questions/binary_search_2d_array.cpp new file mode 100644 index 0000000..b8798ed --- /dev/null +++ b/2d-array_questions/binary_search_2d_array.cpp @@ -0,0 +1,32 @@ +#include +#include +using namespace std; + +class Solution +{ +public: + bool searchMatrix(vector> &matrix, int target) + { + int row = matrix.size(); + int col = matrix[0].size(); + int low = 0, high = (row * col) - 1; + while (low <= high) + { + int mid = (low + high) / 2; + int r = mid / col, c = mid % col; + if (matrix[r][c] == target) + { + return true; + } + else if (matrix[r][c] < target) + { + low = mid + 1; + } + else + { + high = mid - 1; + } + } + return false; + } +}; \ No newline at end of file diff --git a/2d-array_questions/column_wise_sum.cpp b/2d-array_questions/column_wise_sum.cpp new file mode 100644 index 0000000..8c6d3ea --- /dev/null +++ b/2d-array_questions/column_wise_sum.cpp @@ -0,0 +1,40 @@ +#include +#include +using namespace std; + +int main() +{ + int m, n; + cout << "Enter the number of rows and columns: "; + cin >> m >> n; + + vector> arr(m, vector(n)); + + // Taking input + cout << "Enter elements: "; + for (int i = 0; i < m; i++) // Iterate over rows + { + for (int j = 0; j < n; j++) // Iterate over columns + { + cin >> arr[i][j]; + } + } + + // Calculate and output column-wise sum + for (int j = 0; j < n; j++) // Iterate over columns + { + int sum = 0; // Reset sum for each column + for (int i = 0; i < m; i++) // Iterate over rows + { + sum += arr[i][j]; // Add the element to the column sum + } + + // Output each element in the column along with the sum + for (int i = 0; i < m; i++) // Iterate over rows for output + { + cout << arr[i][j] << " "; // Print each element in the column + } + + cout << "-> sum for column " << j + 1 << " is: " << sum << endl; + } +} diff --git a/2d-array_questions/largest_row_sum.cpp b/2d-array_questions/largest_row_sum.cpp new file mode 100644 index 0000000..860f9fa --- /dev/null +++ b/2d-array_questions/largest_row_sum.cpp @@ -0,0 +1,60 @@ +#include +#include +#include // Include limits.h for INT_MIN +using namespace std; + +int largestrowsum(vector> &arr, int m, int n, int &rowIndex) +{ + int maxi = INT_MIN; + for (int i = 0; i < m; i++) + { + int sum = 0; + for (int j = 0; j < n; j++) + { + sum += arr[i][j]; + } + if (sum > maxi) + { + maxi = sum; + rowIndex = i; // Store the index of the row with the largest sum + } + } + return maxi; // Return the maximum sum +} + +int main() +{ + int m, n; + cout << "Enter the number of rows and columns: "; + cin >> m >> n; + cout << "Enter elements:" << endl; + vector> arr(m, vector(n)); + + // For taking input + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + cin >> arr[i][j]; + } + } + + // For output + cout << "The matrix is:" << endl; + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + cout << arr[i][j] << " "; + } + cout << "\n"; + } + + int rowIndex = -1; // To keep track of the row with the largest sum + int maxSum = largestrowsum(arr, m, n, rowIndex); // Call the function + + // Print the maximum sum and the row index + cout << "Maximum sum is " << maxSum << " at row index " << rowIndex + 1 << endl; + + return 0; +} diff --git a/2d-array_questions/linear_search.cpp b/2d-array_questions/linear_search.cpp new file mode 100644 index 0000000..e56e8c1 --- /dev/null +++ b/2d-array_questions/linear_search.cpp @@ -0,0 +1,66 @@ +#include +#include +using namespace std; + +// Function to check if the target element is present in the matrix +bool ispresent(vector> &arr, int target, int n, int m) +{ + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + if (arr[i][j] == target) + { + return true; + } + } + } + return false; +} + +int main() +{ + int m, n; + cout << "Enter the number of rows and columns: "; + cin >> m >> n; + + vector> arr(m, vector(n)); + + // Taking input + cout << "Enter the elements of the matrix:\n"; + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + cin >> arr[i][j]; + } + } + + // Displaying the matrix + cout << "The matrix is:\n"; + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + cout << arr[i][j] << " "; + } + cout << endl; + } + + // Searching for the target element + int target; + cout << "Element to be searched-> "; + cin >> target; + + // Check if the element is present in the matrix + if (ispresent(arr, target, n, m)) + { + cout << "Element present\n"; + } + else + { + cout << "Element not present\n"; + } + + return 0; +} diff --git a/2d-array_questions/print_like_a_wave.cpp b/2d-array_questions/print_like_a_wave.cpp new file mode 100644 index 0000000..1cc56a6 --- /dev/null +++ b/2d-array_questions/print_like_a_wave.cpp @@ -0,0 +1,53 @@ +#include +#include + +using namespace std; + +vector wavePrint(vector> arr, int nRows, int mCols) +{ + vector ans; + for (int j = 0; j < mCols; j++) + { + if (j % 2 == 1) + { + // Odd column index: bottom to top traversal + for (int i = nRows - 1; i >= 0; i--) + { + ans.push_back(arr[i][j]); + } + } + else + { + // Even column index: top to bottom traversal + for (int i = 0; i < nRows; i++) + { + ans.push_back(arr[i][j]); + } + } + } + return ans; +} + +int main() +{ + // Example input + vector> arr = { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9}}; + + int nRows = arr.size(); + int mCols = arr[0].size(); + + // Calling wavePrint function + vector result = wavePrint(arr, nRows, mCols); + + // Output the result + for (int val : result) + { + cout << val << " "; + } + cout << endl; + + return 0; +} diff --git a/2d-array_questions/questions.txt b/2d-array_questions/questions.txt new file mode 100644 index 0000000..dcdca30 --- /dev/null +++ b/2d-array_questions/questions.txt @@ -0,0 +1,92 @@ +2d array questions + +1. creating a 2d array + example m=2 and n=3 + 2 3 + 1 2 3 4 5 6 + 1 2 3 + 4 5 6 +2. linear search in 2d array + example Enter the number of rows and columns: 2 3 + Enter the elements of the matrix: + 1 2 3 4 5 6 + The matrix is: + 1 2 3 + 4 5 6 + Element to be searched-> 8 + Element not present +3.rowwise sum + example Enter the number of rows and columns: 2 3 + Enter the elements of the matrix: + 1 2 3 4 5 6 + The matrix is : + 1 2 3 + 4 5 6 + 6(sum of row 1 is 6) + 15(sum of row 2 is 15) +4.largest row sum + example Enter the number of rows and columns: 2 3 + Enter the elements of the matrix: + 1 2 3 4 5 6 + The matrix is : + 1 2 3 + 4 5 6 + 6(sum of row 1 is 6) + 15(sum of row 2 is 15) + + here 15 is largest row sum at row index=2 +5.print like a wave + example Elements of matrix is : + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9} + Output is : + 1 4 7 8 5 2 3 6 9 + + (HINT: for the odd rows we can go from bottom to top so column %2==1 for traverse from n-1 to 0 & + for odd rows we can go from top to bottom so column%2==0 so traverse from 0 to n-1) + +6.spiral matrix + example Elements of matrix is : + {1, 2, 3, 4}, + {5, 6, 7, 8}, + {9, 10, 11, 12}, + {13, 14, 15, 16} + Output is : + Elements in spiral order are: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 + + (HINT: declare 4 variables for easyiness of loops + top = 0, left = 0, right = col - 1, bottom = row - 1 and traverse acordingly ) + +7.rotate matrix by 90 degree + example Matrix before rotation: + 1 2 3 + 4 5 6 + 7 8 9 + + Matrix after rotation: + 7 4 1 + 8 5 2 + 9 6 3 + + (HINT: step 1. swap the rows values with column values and vice versa + step2.reverse every row to get the result + ) +8.rows with maximum one'search + example + {0, 1, 1}, + {1, 1, 1}, + {0, 0, 0} + Row with maximum 1s: 1 + Number of 1s in that row: 3 +9.Binary search in 2d array + ( HINT step 1. think it like a 1d array + step 2.row can be find by mid/col and column can be found by mid%col ) + step3. apply normal binary search + +10.search in 2d matrix + example {1, 4, 7, 11}, + {8, 9, 10, 20}, + {11, 12, 17, 30} + target = 10; + true \ No newline at end of file diff --git a/2d-array_questions/readme.txt b/2d-array_questions/readme.txt new file mode 100644 index 0000000..353d9cf --- /dev/null +++ b/2d-array_questions/readme.txt @@ -0,0 +1,19 @@ + 2D ARRAYS + + +2D arrays efficiently store and manage grid-like data structures, +making them ideal for representing matrices, tables, or images. + +Here are a few beginner-friendly points explaining the need for 2D arrays: + +1.Storing tabular data: 2D arrays allow you to store data in rows and columns, making it easier to represent grids, matrices, +or tables (e.g., a chessboard or a spreadsheet). + +2. Efficient access: 2D arrays allow direct access to elements using row and column indices, providing an organized way to +work with multi-dimensional data. + +3. Multi-dimensional problems: They are essential for solving problems like matrix operations, image processing, or graph +traversal, where two dimensions naturally represent the data. + +4. Space-efficient: Instead of creating multiple 1D arrays, a 2D array consolidates data into a single structure, reducing +complexity and improving memory management. \ No newline at end of file diff --git a/2d-array_questions/rotate_image.cpp b/2d-array_questions/rotate_image.cpp new file mode 100644 index 0000000..c95a66c --- /dev/null +++ b/2d-array_questions/rotate_image.cpp @@ -0,0 +1,68 @@ +#include +#include +#include // for reverse function + +using namespace std; + +class Solution +{ +public: + void rotate(vector> &matrix) + { + int n = matrix.size(); + + // Transpose the matrix (swap matrix[i][j] with matrix[j][i]) + for (int i = 0; i < n; i++) + { + for (int j = 0; j < i; j++) + { + swap(matrix[i][j], matrix[j][i]); + } + } + + // Reverse each row + for (int i = 0; i < n; i++) + { + reverse(matrix[i].begin(), matrix[i].end()); + } + } +}; + +// Main function to test the rotate function +int main() +{ + Solution sol; + + // Example matrix + vector> matrix = { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9}}; + + // Before rotation + cout << "Matrix before rotation:" << endl; + for (int i = 0; i < matrix.size(); i++) + { + for (int j = 0; j < matrix[i].size(); j++) + { + cout << matrix[i][j] << " "; + } + cout << endl; + } + + // Rotate the matrix + sol.rotate(matrix); + + // After rotation + cout << "\nMatrix after rotation:" << endl; + for (int i = 0; i < matrix.size(); i++) + { + for (int j = 0; j < matrix[i].size(); j++) + { + cout << matrix[i][j] << " "; + } + cout << endl; + } + + return 0; +} diff --git a/2d-array_questions/row_wise_sum.cpp b/2d-array_questions/row_wise_sum.cpp new file mode 100644 index 0000000..7940f12 --- /dev/null +++ b/2d-array_questions/row_wise_sum.cpp @@ -0,0 +1,32 @@ +#include +#include +using namespace std; +int main() +{ + int m, n; + cout << "Enter the number of rows and columns:"; + cin >> m >> n; + cout << "Enter elements:"; + vector> arr(m, vector(n)); + int sum = 0; + // for taking input + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + cin >> arr[i][j]; + } + } + + // for output + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + cout << arr[i][j] << " "; + sum += arr[i][j]; + } + cout << "sum for index " << i + 1 << " is -> " << sum; + cout << "\n"; + } +} \ No newline at end of file diff --git a/2d-array_questions/rows_with_maximum_ones(BINARY_SEARCH).cpp b/2d-array_questions/rows_with_maximum_ones(BINARY_SEARCH).cpp new file mode 100644 index 0000000..4720453 --- /dev/null +++ b/2d-array_questions/rows_with_maximum_ones(BINARY_SEARCH).cpp @@ -0,0 +1,77 @@ +#include +#include +using namespace std; + +class Solution +{ +private: + // Function to find the lower bound of 'x' in the sorted array 'arr' + int lowerbound(vector &arr, int n, int x) + { + int low = 0, high = n - 1; + int ans = n; // Initialize answer to n (out of bounds) + + // Binary search for the lower bound + while (low <= high) + { + int mid = (low + high) / 2; + if (arr[mid] >= x) + { + ans = mid; // Update the answer + high = mid - 1; + } + else + { + low = mid + 1; + } + } + return ans; + } + +public: + // Function to find the row with the maximum number of 1s + vector rowAndMaximumOnes(vector> &mat) + { + int n = mat.size(); // Number of rows + int m = mat[0].size(); // Number of columns + int count_max = -1, index = -1; + + // Iterate through each row + for (int i = 0; i < n; i++) + { + // Find the number of 1s in the current row using lowerbound + int count = m - lowerbound(mat[i], m, 1); + + // Update if the current row has more 1s + if (count > count_max) + { + count_max = count; + index = i; + } + } + + // Return the index of the row and the maximum number of 1s + return {index, count_max}; + } +}; + +// Main function to test the rowAndMaximumOnes function +int main() +{ + Solution sol; + + // Example matrix + vector> matrix = { + {0, 0, 0}, + {0, 1, 1}, + {1, 1, 1}}; + + // Call the function and get the result + vector result = sol.rowAndMaximumOnes(matrix); + + // Print the result + cout << "Row with maximum 1s: " << result[0] << endl; + cout << "Number of 1s in that row: " << result[1] << endl; + + return 0; +} diff --git a/2d-array_questions/rows_with_maximum_ones(LINEAR SEARCH).cpp b/2d-array_questions/rows_with_maximum_ones(LINEAR SEARCH).cpp new file mode 100644 index 0000000..a3581dd --- /dev/null +++ b/2d-array_questions/rows_with_maximum_ones(LINEAR SEARCH).cpp @@ -0,0 +1,57 @@ +#include +#include +using namespace std; + +class Solution +{ +public: + vector rowAndMaximumOnes(vector> &mat) + { + int n = mat.size(); + int m = mat[0].size(); + int maxcount = -1, index = -1; + + // Iterate over each row + for (int i = 0; i < n; i++) + { + int countrow = 0; // Initialize countrow for each row + + // Count the number of 1s in the current row + for (int j = 0; j < m; j++) + { + countrow += mat[i][j]; // Since mat[i][j] is either 0 or 1, just add it + } + + // Update maxcount and index if this row has more 1s + if (countrow > maxcount) + { + maxcount = countrow; + index = i; + } + } + + // Return the index of the row and the maximum number of 1s + return {index, maxcount}; + } +}; + +// Main function to test the rowAndMaximumOnes function +int main() +{ + Solution sol; + + // Example matrix + vector> matrix = { + {0, 1, 1}, + {1, 1, 1}, + {0, 0, 0}}; + + // Call the function and get the result + vector result = sol.rowAndMaximumOnes(matrix); + + // Print the result + cout << "Row with maximum 1s: " << result[0] << endl; + cout << "Number of 1s in that row: " << result[1] << endl; + + return 0; +} diff --git a/2d-array_questions/search_in_2d_matrix_II.cpp b/2d-array_questions/search_in_2d_matrix_II.cpp new file mode 100644 index 0000000..2f8aafa --- /dev/null +++ b/2d-array_questions/search_in_2d_matrix_II.cpp @@ -0,0 +1,30 @@ +#include +#include +using namespace std; + +class Solution +{ +public: + bool searchMatrix(vector> &matrix, int target) + { + int i = matrix.size(), j = matrix[0].size(); + int row = 0, col = j - 1; + while (row < i && col >= 0) + { + if (matrix[row][col] == target) + { + return true; + } + else if (matrix[row][col] < target) + { + row++; + } + else + { + col--; + } + } + + return false; + } +}; \ No newline at end of file diff --git a/2d-array_questions/spiral_matrix.cpp b/2d-array_questions/spiral_matrix.cpp new file mode 100644 index 0000000..df14416 --- /dev/null +++ b/2d-array_questions/spiral_matrix.cpp @@ -0,0 +1,73 @@ +#include +#include + +using namespace std; +class Solution +{ +public: + vector spiralOrder(vector> &matrix) + { + vector ans; + int row = matrix.size(); + int col = matrix[0].size(); + // declare 4 variables for easyiness of loops + int top = 0, left = 0, right = col - 1, bottom = row - 1; + // top row + while (top <= bottom && left <= right) + { + for (int i = left; i <= right; i++) + { + ans.push_back(matrix[top][i]); + } + top++; + // right column + for (int i = top; i <= bottom; i++) + { + ans.push_back(matrix[i][right]); + } + right--; + // bottom row + if (top <= bottom) + { + for (int i = right; i >= left; i--) + { + ans.push_back(matrix[bottom][i]); + } + bottom--; + } + // left column + if (left <= right) + { + for (int i = bottom; i >= top; i--) + { + ans.push_back(matrix[i][left]); + } + left++; + } + } + return ans; + } +}; +int main() +{ + vector> mat = { + {1, 2, 3, 4}, + {5, 6, 7, 8}, + {9, 10, 11, 12}, + {13, 14, 15, 16}}; + + // Create an instance of the Solution class + Solution finder; + + // Get spiral order using class method + vector ans = finder.spiralOrder(mat); + + cout << "Elements in spiral order are: "; + for (int i = 0; i < ans.size(); ++i) + { + cout << ans[i] << " "; + } + cout << endl; + + return 0; +}