Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- pair<int, int> find(vector<vector<int>>& X, int k) {
- int n = X.size();
- int m = X[0].size();
- int a = 0;
- int b = m - 1;
- while (a < n && b >= 0) {
- if (X[a][b] == k) {
- break;
- } else if (X[a][b] < k) {
- a += 1;
- } else {
- b -= 1;
- }
- }
- if (a >= n || b < 0) {
- return make_pair(-1, -1); // not found
- }
- return make_pair(a, b);
- }
- int main() {
- vector<vector<int>> X = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
- int k = 5;
- pair<int, int> result = find(X, k);
- if (result.first == -1 && result.second == -1) {
- cout << "Element not found." << endl;
- } else {
- cout << "Element found at position (" << result.first << ", " << result.second << ")." << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement