chore(CPlusPlus): add index of smallest element in array (#584)

Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com>
pull/612/head^2
shefali kanojia 2021-10-22 18:30:43 +05:30 committed by GitHub
parent c152e76522
commit 2dddebc516
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,35 @@
//index of smallest element of array
/* Example :
size of array =5
Let array be ,arr[5]={9,4,1,5,8}
here smallest element of array is 1 which is at index 2 of array
output = 2 that is index of smallest element of array.
Time complexity = O(n)
*/
#include<iostream>
using namespace std;
int main()
{
int size,j;
cout<<"enter the size of array";
cin>>size;
int arr[size];
for(int i=0;i<size;i++)
{
cin>>arr[i];
}
//considering the element at zero index to be smallest
int min=arr[0];
int smallest_index=0;
//comparing min to all other elements
for(j=0;j<size;j++)
{
if(min>arr[j])
{
min=arr[j];
smallest_index=j;
}
}
cout<<"index of smallest element of array is"<<smallest_index;
}

View File

@ -22,6 +22,7 @@
- [Matrix Multiplication](Arrays/matrix-multiplication.cpp) - [Matrix Multiplication](Arrays/matrix-multiplication.cpp)
- [Smallest Sum Possible](Arrays/smallest-sum-possible.cpp) - [Smallest Sum Possible](Arrays/smallest-sum-possible.cpp)
- [Smallest Possible Sum](Arrays/smallest-possible-sum.cpp) - [Smallest Possible Sum](Arrays/smallest-possible-sum.cpp)
- [Index of Smallest element of Array](Arrays/index-of-smallest-element-of-array.cpp)
- [Move Zeros to End of The Array](Arrays/move-zeros-to-end-of-array.cpp) - [Move Zeros to End of The Array](Arrays/move-zeros-to-end-of-array.cpp)
- [Kadane's Algorithm](Arrays/Kadane's-Algorithm.cpp) - [Kadane's Algorithm](Arrays/Kadane's-Algorithm.cpp)