Search
 
SCRIPT & CODE EXAMPLE
 

CPP

second smallest element using single 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 :: string class cpp 
Cpp :: 1281. Subtract the Product and Sum of Digits of an Integer leetcode solution in c++ 
Cpp :: Redragon m609 weight 
Cpp :: vector.rbegin() 
Cpp :: attack on titan junior high list of episodes 
Cpp :: android call custom managed method from native code 
Cpp :: Basic Variables Entry C++ Programming 
Cpp :: Check if two stacks are the same using C++ 
Cpp :: what is a string called in c++ 
Cpp :: return value optimization example 
Cpp :: online convert c++ code to assembly language 
Cpp :: c++ sleep function 
Cpp :: arraylist equivalent cpp 
Cpp :: lower bound c++ 
Cpp :: pointers in c++ 
Cpp :: cpp map contains 
Cpp :: how to create a structure c++ 
Cpp :: are arrays faster than vectors c++ 
Cpp :: imgui menu bar 
Cpp :: flags of open operation c++ 
C :: hello word c 
C :: rename c 
C :: how to make a hello world program in c 
C :: how to print boolean in c 
C :: type change in c 
C :: Succ de ch 
C :: how to mutex lock in c 
C :: check if the c code is a palindrome 
C :: why do we need return 0 in c? 
C :: typedef in c 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =