chore(CPlusPlus): add largest and smallest number in an array (#1110)

pull/1070/head
Uma-95 2022-12-22 18:59:25 +05:30 committed by GitHub
parent bb641ee600
commit af47764be0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -0,0 +1,20 @@
#include <iostream>
#include <algorithm>
using namespace std;
//simple approach:
//sort the array in ascending order.
//the first element would be the smallest and the last element would be the largest
int main()
{
int arr[]={1,2,3,4,5};
int n=sizeof(arr)/sizeof(arr[0]);
cout<<n<<endl;
sort(arr,arr+n);//sorting the array
//sort(a,a+n)-->a is the name of the array and n is the size of array a
cout<<arr[0]<<" --> smallest number "<<endl;
cout<<arr[n-1]<<" --> largest number "<<endl;
return 0;
}

View File

@ -33,7 +33,7 @@
- [Sparse Matrix](Arrays/sparse_matrix.cpp)
- [Balanced Parenthesis](Arrays/balanced-parenthesis.cpp)
- [Find special index](Arrays/specialindex2.cpp)
- [Largest and smallest number in an array](Arrays/Largest-smallest.cpp)
## Dynamic-Programming