Created
March 19, 2016 11:20
-
-
Save ravichandrae/3c7e9df7842dd8dbe0fc to your computer and use it in GitHub Desktop.
searching for a given number in adjacent array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
bool search_adj(const vector<int> &arr, int s) { | |
bool ret = false; | |
size_t i; | |
for( i = 0; i < arr.size(); ) { | |
if( arr[i] == s ) { | |
ret = true; | |
break; | |
} | |
else { | |
i += abs(s-arr[i]); | |
} | |
} | |
return ret; | |
} | |
int main() { | |
int n; | |
cin >> n; | |
int i; | |
vector<int> arr(n); | |
for( i = 0; i < n; i++ ) | |
cin >> arr[i]; | |
int s;//number to search | |
cin >> s; | |
if( search_adj(arr, s) ) | |
cout << "Found" << endl; | |
else | |
cout << "Not found" << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment