Pages

Tuesday, May 27, 2025

Linear Search in C++ (University Topic)

 #include <iostream>

using namespace std;


int linearSearch(int arr[], int size, int element) {

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

        if(arr[i]==element) {

         return i;   

        }

    }

    

    

    return -1;

}




int main() {

    int arr[] = {10, 23,45,56,43,53};

    int size = sizeof(arr)/sizeof(int);

    int element = 56;

    int searchResult = linearSearch(arr, size, element);

    

    cout << "The element " << element << " is at index " << searchResult << endl;

    


    return 0;

}

No comments:

Post a Comment

3917. Count Indices With Opposite Parity (Brute Force) O(n2) + Optimized Solution O(n) + tips LEETCODE WEEKLY 500

  class Solution {     public int [] countOppositeParity ( int [] nums ) {          // approach 1 - checks every pair         int n = n...