diff --git a/Matrix/a.exe b/Matrix/a.exe index 1e482c1..8659487 100644 Binary files a/Matrix/a.exe and b/Matrix/a.exe differ diff --git a/Matrix/common.cpp b/Matrix/common.cpp new file mode 100644 index 0000000..0c654f6 --- /dev/null +++ b/Matrix/common.cpp @@ -0,0 +1,56 @@ +/* +Common elements in all rows of a given matrix +*/ + +#include +using namespace std; + +void commonElements(int a[10][10], int n, int m) +{ + unordered_map mp; + for (int i = 0; i < m; i++) //initializing first row elements with value 1 + { + mp[a[0][i]] = 1; + } + for (int i = 1; i < n; i++) //checking from second row + { + for (int j = 0; j < m; j++) + { + if (mp[a[i][j]] == i) //if element is present in map + { + mp[a[i][j]] = i + 1; //count increasing by 1 + if (i == n - 1 and mp[a[i][j]] == n) //if last row + { + cout << a[i][j] << " "; + } + } + } + } +} + +int main() +{ + + int a[10][10], i, j, n, m; + + cout << "Enter number of rows (between 1 and 10): "; + cin >> n; + cout << "Enter number of columns (between 1 and 10): "; + cin >> m; + + cout << endl + << "Enter elements of the matrix: " << endl; + + // Storing elements of first matrix entered by user. + for (i = 0; i < n; ++i) + { + for (j = 0; j < m; ++j) + { + cout << "Enter element a" << i + 1 << j + 1 << " : "; + cin >> a[i][j]; + } + } + commonElements(a, n, m); + + return 0; +} \ No newline at end of file diff --git a/Matrix/kMat.cpp b/Matrix/kMat.cpp new file mode 100644 index 0000000..aa9d208 --- /dev/null +++ b/Matrix/kMat.cpp @@ -0,0 +1,102 @@ +/* +Kth element in Matrix +Given a N x N matrix, where every row and column is sorted in non-decreasing order. + Find the kth smallest element in the matrix. + +Example 1: +Input: +N = 4 +mat[][] = {{16, 28, 60, 64}, + {22, 41, 63, 91}, + {27, 50, 87, 93}, + {36, 78, 87, 94 }} +K = 3 +Output: 27 +Explanation: 27 is the 3rd smallest element. + + +Example 2: +Input: +N = 4 +mat[][] = {{10, 20, 30, 40} + {15, 25, 35, 45} + {24, 29, 37, 48} + {32, 33, 39, 50}} +K = 7 +Output: 30 +Explanation: 30 is the 7th smallest element. +*/ + +#include +using namespace std; + +int count(int a[10][10], int n, int m, int mid) +{ + int count = 0; //to store count of values smaller or equal to mid + int j = m - 1; //from reverse + for (int i = 0; i < n; i++) + { + for (j; j >= 0; j--) + { + if (a[i][j] <= mid) + { + count += j + 1; + //no of columns +1 (col no. starts from 0) + break; + } + } + } + return count; +} + +int kSmall(int a[10][10], int n, int m, int k) +{ + + int low = a[0][0]; + int high = a[n - 1][m - 1]; + while (low < high) + { + int mid = floor((high - low) / 2 + 1); + int c = count(a, n, m, mid); + if (c >= k) + { + high = mid; + } + else + { + low = mid + 1; + } + } + + return low; +} + +int main() +{ + int a[10][10], i, j, n, m, k; + + cout << "Enter number of rows (between 1 and 10): "; + cin >> n; + cout << "Enter number of columns (between 1 and 10): "; + cin >> m; + + cout << endl + << "Enter elements of the matrix: " << endl; + + // Storing elements of first matrix entered by user. + for (i = 0; i < n; ++i) + { + for (j = 0; j < m; ++j) + { + cout << "Enter element a" << i + 1 << j + 1 << " : "; + cin >> a[i][j]; + } + } + + cout << "Enter value of k (kth smallest element): "; + cin >> k; + + int ans = kSmall(a, n, m, k); + cout << ans << endl; + return 0; +} \ No newline at end of file