fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // typedef vector <int> vi;
  5. // typedef pair< int ,int > pii;
  6. // #define endl "\n"
  7. // #define sd(val) scanf("%d",&val)
  8. // #define ss(val) scanf("%s",&val)
  9. // #define sl(val) scanf("%lld",&val)
  10. // #define debug(val) printf("check%d\n",val)
  11. // #define all(v) v.begin(),v.end()
  12. // #define PB push_back
  13. // #define MP make_pair
  14. // #define FF first
  15. // #define SS second
  16. // #define ll long long
  17. // #define MOD 1000000007
  18. // #define clr(val) memset(val,0,sizeof(val))
  19. // #define what_is(x) cerr << #x << " is " << x << endl;
  20. // #define OJ \
  21. // freopen("input.txt", "r", stdin); \
  22. // freopen("output.txt", "w", stdout);
  23. // #define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  24.  
  25. void search(int a[][1000],int m,int n,int k){
  26. if(k<a[0][0] || k>a[m-1][n-1]){
  27. cout<<"The element is not present in the array";
  28. return;
  29. }
  30. int i=0;
  31. int j=n-1;
  32. while(i<=m-1 && j>=0){
  33. if(a[i][j]==k){
  34. cout<<"The key is at "<< i<<","<<j;
  35. return;
  36. }
  37. else if(a[i][j]<k){
  38. i++;
  39. }
  40. else{
  41. j--;
  42. }
  43. }
  44. cout<<" Not Found";
  45. return;
  46.  
  47. }
  48.  
  49.  
  50. int main(){
  51. int a[1000][1000]={{1,4,8,10},{2,5,9,15},{6,16,18,20}};
  52. search(a,3,4,15);
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0s 7468KB
stdin
Standard input is empty
stdout
The key is at 1,3