2d Array searcher for sorted array: Samsung

 // Online C++ compiler to run C++ program online

#include <iostream>

//#include <bits/stdc++.h>

using namespace std;


int main() {

    // Write C++ code here

    int p,q,r;

    p=3;

    q=4;

    r=3;

    

    int arr[p][q]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};

    int arr2[q][r]={{1,2,3},{4,5,6},{7,8,9},{4,5,6}};

    for(int i=0;i<p;i++){

        for(int j=0;j<q;j++){

            cout<<arr[i][j]<<" ";

        }

        cout<<endl;

    }

    int target=13;

    int i=0;

    int j=q-1;

    int x=arr[i][j];

    cout<<x<<endl;

    bool flag=false;

    while(i<p || j>=0){

        if(target>arr[i][j]){

            i++;

        }

        else if(target<arr[i][j]){

            j--;

        }

        else{

            flag=true;

            break;

        }

    if(flag==true){

        break;

    }

    }

    if(flag==false){

        cout<<"Not found";

    }

    else{

        cout<<"found";

    }

}

Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings