# Sudo Placement[1.5] | Second Smallest in Range

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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.
1. If the current element is smaller than first, then update first and second.
2. Else if the current element is smaller than 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 ` `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); ` `    ``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

 ` `

Output:

```-1
2
```

Time Complexity: O(M), where M = R-L is the number of elements in range [L, R]

Note: Since the constraints of the question was very less, a brute force solution will pass. The solution can be further optimised using Segment Tree.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.