tutorialcup@gmail.com

Find the first and second smallest elements

Arrays
Reverse Array
Pair Sum Equals Number
Floor and Ceil
Largest Sum Subarray
Missing Number
Odd Occuring Number
Sort by Frequency
First and Second Smallest
Majority Element
Difference between elements
Arrange Even and Odd
Closest Sum Elements
Distinct Elements
First Repeating Number
Common Elements in Arrays
Smallest Missing Number
Max Adjacent Sum
Find Occurrences of Number
Rotate Image
Distance Between Numbers
Pair with given difference
Product Array Puzzle
Replace Elements
First Repeating Element
Move Zeroes to End
Pythagorean Triplets
Maximum Average Subarray
Subarray Sum Equal to X
Leaders in Array
Largest Pair Sum
Segregate 0s 1s and 2s
Find Duplicates
Consecutive Elements Check
Triplet with given Sum
Binary Search in Sorted Array
Fixed point in sorted array
Reorder Elements by Indexes
Merging two sorted arrays
Find next greater number
Reorder array using given indexes
Count of triplets with sum less than given value
Merge two sorted arrays
Subarray and Subsequence
Rearrange array maximum minimum form
Find the lost element from a duplicated array
Count minimum steps to get the given array
Maximum element in an array which is increasing and then decreasing
Minimum number of jumps to reach the end of an array
Subarray with given sum
Length of Longest Increasing Subsequence
Smallest positive number missing unsorted array
The Celebrity Problem
Sorted subsequence of size 3
Partition Problem
Find pair with given difference
Maximum length of chain pairs
Four elements that sum to given
Maximum circular subarray sum
Count possible triangles
Longest Increasing Subsequence
Petrol Bunks Tour
Tug of War
Counting Sort
Maximum Repeating Number
Positive Negative Arrangement
Find a Peak element
Elements More Than n/k
Max Product Subsequence
Longest Bitonic subarray in an array
Number of smaller elements on right side
Implement two stacks in an array
Maximum sum increasing subsequence
Find the two numbers with odd occurrences in an unsorted array
Largest subarray with equal number of 0's and 1's
Maximum product subarray
Replace every element with the greatest on the right side in an array
Sorting a k sorted array
Find the row with maximum number of 1's
Shuffle a given array
Iterative Implementation of quick sort
Arrange given numbers to form the biggest number
Pancake sorting
Pancake sorting Problem
Maximum Subarray Sum using Divide and Conquer
Merge Overlapping Intervals
Stock Buy Sell to Maximize Profit
Sort Elements by frequency
Print all possible combinations of r elements in a given array of size n
Monotonically increasing function
Minimum element in a sorted and rotated array
Merge k Sorted Arrays
Flip Zeroes for Consecutive 1's
Least Average Subarray
Longest span with same sum in two binary arrays
Form minimum number from given sequence of D's and I's
Number of strictly increasing subarrays
Minimum difference between any two elements in an array
Number of pairs with given sum
Make Array Palindrome
Dynamic Programming, Longest Bitonic SubSequence

Given an array of integers. Find the first and second smallest integers from an array.

OR

Find two smallest numbers from an array

INPUT: 7, 6, 8, 10, 11, 5, 13, 99

OUTPUT: First Smallest is 5 and Second Smallest is 6

Method 1

TIME COMPLEXITY: O(N)

SPACE COMPLEXITY: O(1)

Algorithm

1. Take two variables smallest and nextSmallest.
2. Let say smallest is initialized with first element of array.
3. Keep on comparing the array values as:
a. If the array element is smaller than smallest then

  • Compare the value of smallest with value of  nextSmallest and update accordingly.
  • Update the smallest value.

b. Else compare if the array element is larger than smallest but smaller than nextSmallest then change the value of nextSmallest variable.

Program for Method 1

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int arr[] = {4,1,8,9,11,3,77,2};
	int N = sizeof(arr)/sizeof(arr[0]);
    
	int small,next_small=INT_MAX;
	small = arr[0];
		
	for(int i=1;i<N;i++)
	{
		if(arr[i] < small)
		{
			next_small = small;
			small = arr[i];	
		}			
		else if(arr[i] < next_small and arr[i] > small)
			next_small = arr[i];
	}
		
	cout << "Smallest and the second smallest numbers are respectively "<< small << " and " << next_small <<endl;
    
	return 0;
}
Try It

Method 2

TIME COMPLEXITY: O(NlogN)
SPACE COMPLEXITY: O(N)

Algorithm

1. Simply Sort the array using Merge or Heap sort. We can also use sort() STL.
2. The first and the second element will be the smallest and next smallest respectively as it is in ascending order.

Program for Method 2

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int arr[] = {4,1,8,9,11,3,77,2};
	int N = sizeof(arr)/sizeof(arr[0]);
	
	//sort the array and the first two elements are the desired values
	sort(arr,arr+N);
		
	cout << "Smallest and the second smallest numbers are respectively "<< arr[0] << " and  " << arr[1] <<endl;
    
    return 0;
}
Try It 


C++ Online Test - Check Your Rank across the World


Next > < Prev
Scroll to Top