Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created November 10, 2020 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amankharwal/c1396f988e070691fe724c60295e28cf to your computer and use it in GitHub Desktop.
Save amankharwal/c1396f988e070691fe724c60295e28cf to your computer and use it in GitHub Desktop.
#include<iostream>
using namespace std;
int linearSearch(int arr[], int n, int key){
for (int i = 0; i < n; i++){
if (arr[i] == key){
return i;
}
}
return -1;
}
int main(){
int n;
cin>>n;
int arr[n];
for (int i = 0; i < n; i++){
cin>>arr[i];
}
int key;
cin>>key;
cout<<linearSearch(arr, n, key)<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment