Zweit kleinste Element in Array mit einer Schleife


#include <iostream>
#include<vector>
using namespace std;

int main() {
	vector<int> v;
    int n;

    cout<<"Enter total element: ";
    cin>>n;

    // Initializing array
    cout<<"Enter values: "<<endl;
    for(int i=0;i<n;i++){
        int temp;
        cin>>temp;
        v.push_back(temp);
    }

    // Finding second smallest using single loop
    int smallest=v[0];
    int secondSmallest=v[1];
    for(int i=0;i<n;i++){
        if(v[i]<smallest){
            secondSmallest=smallest;
            smallest=v[i];
        }
        if(v[i]<secondSmallest && v[i]!=smallest){
            secondSmallest=v[i];
        }
    }

    cout<<"Smallest Number: "<<smallest<<endl;
    cout<<"Second Smallest Number: "<<secondSmallest<<endl;

	return 0;
}


Abhay Singh