Source Code
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int a[m][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>a[i][j];
}
}
int search;
cin>>search;
int i=0,j=n-1;
while(i<m && j>=0)
{
if(search<a[0][0]||search>a[m-1][n-1]) cout<<“0”<<endl;
if(a[i][j]>search)
{
j–;
}
if(a[i][j]<search)
{
i++;
}
if(a[i][j]==search) cout<<“1”<<endl;
}
cout<<“0”<<endl;
}
Why i'm getting TLE in this code (efficient code)
You have to apply the staircase search approach as taught in the video.
Please dry run your code with this case:
3 3
3 30 38
44 52 54
57 60 69
52
Also break the loop as soon as you find the search element.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.