须藤放置[1.5] |范围内第二小
给定一个包含 N 个整数和 Q 个查询的数组。每个查询由 L 和 R 组成。任务是打印范围 LR 中第二小的元素。如果不存在第二小的元素,则打印 -1。
例子:
Input:
a[] = {1, 2, 2, 4}
Queries= 2
L = 1, R = 2
L = 0, R = 1
Output:
-1
2
方法:处理每个查询并使用以下算法打印第二小的查询。
- 将第一个和第二个最小的都初始化为 INT_MAX
- 循环遍历所有元素。
- 如果当前元素小于第一个元素,则更新第一个和第二个。
- 否则,如果当前元素小于第二个,则更新第二个
如果循环完所有元素后第二个元素仍然是 INT_MAX,则打印 -1 否则打印第二小的元素。
下面是上述方法的实现:
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[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
Javascript
输出:
-1
2
时间复杂度: O(M),其中 M = RL 是 [L, R] 范围内的元素数
注意:由于问题的约束非常少,因此蛮力解决方案将通过。可以使用段树进一步优化该解决方案。