Skip to content
This repository was archived by the owner on Oct 4, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions 2d-array_questions/2darray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int m, n;
cin >> m >> n;
vector<vector<int>> arr(m, vector<int>(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";
}
}
32 changes: 32 additions & 0 deletions 2d-array_questions/binary_search_2d_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <vector>
using namespace std;

class Solution
{
public:
bool searchMatrix(vector<vector<int>> &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;
}
};
40 changes: 40 additions & 0 deletions 2d-array_questions/column_wise_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
#include <vector>
using namespace std;

int main()
{
int m, n;
cout << "Enter the number of rows and columns: ";
cin >> m >> n;

vector<vector<int>> arr(m, vector<int>(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;
}
}
60 changes: 60 additions & 0 deletions 2d-array_questions/largest_row_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <vector>
#include <limits.h> // Include limits.h for INT_MIN
using namespace std;

int largestrowsum(vector<vector<int>> &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<vector<int>> arr(m, vector<int>(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;
}
66 changes: 66 additions & 0 deletions 2d-array_questions/linear_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <iostream>
#include <vector>
using namespace std;

// Function to check if the target element is present in the matrix
bool ispresent(vector<vector<int>> &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<vector<int>> arr(m, vector<int>(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;
}
53 changes: 53 additions & 0 deletions 2d-array_questions/print_like_a_wave.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <iostream>
#include <vector>

using namespace std;

vector<int> wavePrint(vector<vector<int>> arr, int nRows, int mCols)
{
vector<int> 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<vector<int>> arr = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};

int nRows = arr.size();
int mCols = arr[0].size();

// Calling wavePrint function
vector<int> result = wavePrint(arr, nRows, mCols);

// Output the result
for (int val : result)
{
cout << val << " ";
}
cout << endl;

return 0;
}
92 changes: 92 additions & 0 deletions 2d-array_questions/questions.txt
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions 2d-array_questions/readme.txt
Original file line number Diff line number Diff line change
@@ -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.
Loading