Search
 
SCRIPT & CODE EXAMPLE
 

CPP

second smallest element in array using one loop


#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;
}


Comment

PREVIOUS NEXT
Code Example
Cpp :: last element of a set in c++ 
Cpp :: Snake Procession codechef solution in c++ 
Cpp :: high school hacking competition 
Cpp :: 136. Single Number leetcode solution in c++ 
Cpp :: find the mminimum of the vector and its position in c++ 
Cpp :: initialize many variablles c++ 
Cpp :: days in a year c++ 
Cpp :: erase in c++ 
Cpp :: c++ 
Cpp :: c+ - Dormir en millisecondes 
Cpp :: c++ insert vector into vector 
Cpp :: composition namespaces c++ 
Cpp :: how to delete repeated element in stack c++ 
Cpp :: c++ linker input and output 
Cpp :: C++ (ISO) 
Cpp :: c++ vector merge algorithm 
Cpp :: how to fixed how many digit will be after point in c++ 
Cpp :: hola mundo c++ 
Cpp :: construct string with repeat limit 
Cpp :: frac{2}{5}MR^2 typed in c++ 
Cpp :: QMetaObject_invokeMethod 
Cpp :: c++ arreglo/array 
Cpp :: FINAL CODES_MY_OS_LAB 
Cpp :: c++ max and min of vector 
Cpp :: palindrome no example 
Cpp :: while loop c++ 
Cpp :: declaring multiple variables in cpp 
Cpp :: do while loop c++ 
C :: What are the 3 basic types of Plate Boundaries? Explain their differences (how they act). 
C :: adb switch to usb 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =