Sudo Placement[1.5] | Second Smallest in Range
Last Updated :
11 Jul, 2025
Given an array of N integers and Q queries. Every query consists of L and R. The task is to print the second smallest element in range L-R. Print -1 if no second smallest element exists.
Examples:
Input:
a[] = {1, 2, 2, 4}
Queries= 2
L = 1, R = 2
L = 0, R = 1
Output:
-1
2
Approach: Process every query and print the second smallest using the following algorithm.
- Initialize both first and second smallest as INT_MAX
- Loop through all the elements.
- If the current element is smaller than the first, then update first and second.
- Else if the current element is smaller than the second then update second
If the second element is still INT_MAX after looping through all the elements, print -1 else print the second smallest element.
Below is the implementation of the above approach:
C++
// C++ program for
// SP - Second Smallest in Range
#include <bits/stdc++.h>
using namespace std;
// Function to find the second smallest element
// in range L-R of an array
int secondSmallest(int a[], int n, int l, int r)
{
int first = INT_MAX;
int second = INT_MAX;
for (int i = l; i <= r; i++) {
if (a[i] < first) {
second = first;
first = a[i];
}
else if (a[i] < second and a[i] != first) {
second = a[i];
}
}
if (second == INT_MAX)
return -1;
else
return second;
}
// function to perform queries
void performQueries(int a[], int n)
{
// 1-st query
int l = 1;
int r = 2;
cout << secondSmallest(a, n, l, r) << endl;
// 2nd query
l = 0;
r = 1;
cout << secondSmallest(a, n, l, r);
}
// Driver Code
int main()
{
int a[] = { 1, 2, 2, 4 };
int n = sizeof(a) / sizeof(a[0]);
performQueries(a, n);
return 0;
}
Java
// Java program for
// SP - Second Smallest in Range
class GFG
{
// Function to find the
// second smallest element
// in range L-R of an array
static int secondSmallest(int a[], int n,
int l, int r)
{
int first = Integer.MAX_VALUE;
int second = Integer.MAX_VALUE;
for (int i = l; i <= r; i++)
{
if (a[i] < first)
{
second = first;
first = a[i];
}
else if (a[i] < second &&
a[i] != first)
{
second = a[i];
}
}
if (second == Integer.MAX_VALUE)
return -1;
else
return second;
}
// function to perform queries
static void performQueries(int a[], int n)
{
// 1-st query
int l = 1;
int r = 2;
System.out.println(secondSmallest(a, n, l, r));
// 2nd query
l = 0;
r = 1;
System.out.println(secondSmallest(a, n, l, r));
}
// Driver Code
public static void main(String[] args)
{
int a[] = { 1, 2, 2, 4 };
int n = a.length;
performQueries(a, n);
}
}
// This code is contributed
// by ChitraNayal
Python
# Python program for
# SP - Second Smallest in Range
# Function to find the
# second smallest element
# in range L-R of an array
import sys
def secondSmallest(a, n, l, r):
first = sys.maxsize
second = sys.maxsize
for i in range(l, r + 1):
if (a[i] < first):
second = first
first = a[i]
elif (a[i] < second and
a[i] != first):
second = a[i]
if (second == sys.maxsize):
return -1
else:
return second
# function to perform queries
def performQueries(a, n):
# 1-st query
l = 1
r = 2
print(secondSmallest(a, n, l, r))
# 2nd query
l = 0
r = 1
print(secondSmallest(a, n, l, r))
# Driver Code
a = [1, 2, 2, 4 ]
n = len(a)
performQueries(a, n);
# This code is contributed
# by Shivi_Aggarwal
C#
// C# program for
// SP - Second Smallest in Range
using System;
class GFG
{
// Function to find the
// second smallest element
// in range L-R of an array
static int secondSmallest(int[] a, int n,
int l, int r)
{
int first = int.MaxValue;
int second = int.MaxValue;
for (int i = l; i <= r; i++)
{
if (a[i] < first)
{
second = first;
first = a[i];
}
else if (a[i] < second &&
a[i] != first)
{
second = a[i];
}
}
if (second == int.MaxValue)
return -1;
else
return second;
}
// function to perform queries
static void performQueries(int[] a, int n)
{
// 1-st query
int l = 1;
int r = 2;
Console.WriteLine(secondSmallest(a, n, l, r));
// 2nd query
l = 0;
r = 1;
Console.WriteLine(secondSmallest(a, n, l, r));
}
// Driver Code
public static void Main()
{
int[] a = { 1, 2, 2, 4 };
int n = a.Length;
performQueries(a, n);
}
}
// This code is contributed
// by ChitraNayal
PHP
<?php
// PHP program for
// SP - Second Smallest in Range
// Function to find the
// second smallest element
// in range L-R of an array
function secondSmallest(&$a, $n, $l, $r)
{
$first = PHP_INT_MAX;
$second = PHP_INT_MAX;
for ($i = $l; $i <= $r; $i++)
{
if ($a[$i] < $first)
{
$second = $first;
$first = $a[$i];
}
else if ($a[$i] < $second and
$a[$i] != $first)
{
$second = $a[$i];
}
}
if ($second == PHP_INT_MAX)
return -1;
else
return $second;
}
// function to perform queries
function performQueries(&$a, $n)
{
// 1-st query
$l = 1;
$r = 2;
echo secondSmallest($a, $n,
$l, $r)."\n";
// 2nd query
$l = 0;
$r = 1;
echo secondSmallest($a, $n,
$l, $r)."\n";
}
// Driver Code
$a = array(1, 2, 2, 4);
$n = sizeof($a);
performQueries($a, $n);
// This code is contributed
// by ChitraNayal
?>
JavaScript
<script>
// Javascript program for
// SP - Second Smallest in Range
// Function to find the
// second smallest element
// in range L-R of an array
function secondSmallest(a,n,l,r)
{
let first = Number.MAX_VALUE;
let second = Number.MAX_VALUE;
for (let i = l; i <= r; i++)
{
if (a[i] < first)
{
second = first;
first = a[i];
}
else if (a[i] < second &&
a[i] != first)
{
second = a[i];
}
}
if (second == Number.MAX_VALUE)
return -1;
else
return second;
}
// function to perform queries
function performQueries(a,n)
{
// 1-st query
let l = 1;
let r = 2;
document.write(secondSmallest(a, n, l, r)+"<br>");
// 2nd query
l = 0;
r = 1;
document.write(secondSmallest(a, n, l, r)+"<br>");
}
// Driver Code
let a=[1, 2, 2, 4];
let n = a.length;
performQueries(a, n);
// This code is contributed by rag2127
</script>
Time Complexity: O(M), where M = R-L is the number of elements in the range [L, R]
Auxiliary Space: O(1)
Note: Since the constraints of the question were very less, a brute force solution will pass. The solution can be further optimized using a Segment Tree.
Similar Reads
Sudo Placement[1.3] | Stack Design Given q number of queries, you need to perform operations on the stack. Queries are of three types 1, 2, and 3. If the operation is to push (1) then push the elements, if the operations are to pop (2) then pop the element and if it is Top (3), then print the element at the top of the stack (If the s
4 min read
Sudo Placement[1.3] | Playing with Stacks You are given 3 stacks, A(Input Stack), B(Auxiliary Stack) and C(Output Stack). Initially stack A contains numbers from 1 to N, you need to transfer all the numbers from stack A to stack C in sorted order i.e in the end, the stack C should have smallest element at the bottom and largest at top. You
7 min read
Sudo Placement[1.4] | Jumping the Subtree Given a binary search tree of n nodes with distinct values. Also given are Q queries. Each query consists of a node value that has to be searched in the BST and skip the subtree that has given node as its root. If the provided node is the root itself then print "Empty" without quotes. After that pri
9 min read
Smallest greater elements in whole array An array is given of n length, and we need to calculate the next greater element for each element in the given array. If the next greater element is not available in the given array then we need to fill '_' at that index place. Examples : Input : 6 3 9 8 10 2 1 15 7 Output : 7 6 10 9 15 3 2 _ 8 Here
11 min read
k smallest elements in same order using O(1) extra space We are given an array of n-elements you have to find k smallest elements from the array but they must be in the same order as they are in the given array and we are allowed to use only O(1) extra space. Examples: Input : arr[] = {4, 2, 6, 1, 5}, k = 3 Output : 4 2 1 Explanation : 1, 2 and 4 are thre
8 min read
Find the smallest and second smallest elements in an array Given an array arr[] of integers, find the smallest and second smallest distinct elements in the array. The result should be returned in ascending order, meaning the smallest element should come first, followed by the second smallest. If there is no valid second smallest (i.e., all elements are the
13 min read