100DaysofDSA

What I learned on the Day 5:

Use of find() function:

int main(){
    int arr[] = {1,2,3,4,5};
    int n = 5;
    int key = 4;

    bool present = binary_search(arr,arr+n,key);
    if(present){
        cout<<"present";
    }
    else{
        cout<<"absent";
    }
}
  1. instade of using find, u can also use binary_Search() for finding if any element present or not.

use lower_bound() and upper_bound():

int main(){
    int arr[] = {1,2,3,4,4,4,5};
    int n = 7;
    int key = 4;

    // lower bound
    auto lb = lower_bound(arr,arr+n,key);     // gives the address of the 1st
                                              // occurence element that is 
                                              // >=key
    cout<<(lb-arr)                            // prints index of lower bound 
                                              // of 4, which is 3(1st occ or 4).

    //upper bound
    auto ub = upper_bound(arr,arr+n,key);     // gives the address of the 1st
                                              // occurence element that is 
                                              // >key 
    cout<<(ub-arr);                           // prints index of upper bound 
                                              // of 4, which is 6(idx of 5).

    //calculate the frequency of the key
    cout<<ub-lb;                              // prints the frequency of key 
}

Use sort() function:

}

- to sort in decreasing order
```cpp
// this function tells the sort() func that 
// we want decreasing order
bool compare(int a,int b){
    return a>b;
}

int main(){
    int arr[] = {1,2,10,4,14,41,-5};
    int n = 7;
    
    // use sort() to sort in decreasing order
    sort(arr,arr+n,compare);

    for(int i=0;i<n;i++){
        cout<<arr[i];
    }

}