K 的最大值,使得 K 和 -K 都存在于给定索引范围内的数组中 [L, R]
给定一个由N个整数和2 个整数L和R组成的数组arr[] ,任务是返回大于0且L<=K<=R的最大整数K ,使得值K和-K都存在于数组arr中[] 。如果没有这样的整数,则返回0 。
例子:
Input: N = 5, arr[] = {3, 2, -2, 5, -3}, L = 2, R = 3
Output: 3
Explanation: The largest value of K in the range [2, 3] such that both K and -K exist in the array is 3 as 3 is present at arr[0] and -3 is present at arr[4].
Input: N = 4, arr[] = {1, 2, 3, -4}, L = 1, R = 4
Output: 0
方法:这个想法是遍历数组并将元素添加到集合中,同时检查它的负数,即arr[i]*-1到集合中。如果找到,则将其推入向量possible[] 。请按照以下步骤解决问题:
- 初始化一个 unordered_set
s[]来存储元素。 - 初始化一个向量possible[]来存储可能的答案。
- 使用变量i迭代范围[0, N)并执行以下步骤:
- 如果-arr[i]存在于集合s[]中,则将值abs(arr[i])推入向量possible[]。
- 否则将元素arr[i]添加到集合s[] 中。
- 将变量ans初始化为0以存储答案。
- 使用变量i迭代范围[0, size) ,其中size是向量possible[]的大小,并执行以下步骤:
- 如果possible[i]大于等于L且小于等于R,则将ans的值更新为ans或possible[i] 的最大值。
- 执行上述步骤后,打印ans的值作为答案。
下面是上述方法的实现。
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum value of K
int findMax(int N, int arr[], int L, int R)
{
// Using a set to store the elements
unordered_set s;
// Vector to store the possible answers
vector possible;
// Store the answer
int ans = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// If set has it's negation,
// check if it is max
if (s.find(arr[i] * -1) != s.end())
possible.push_back(abs(arr[i]));
else
s.insert(arr[i]);
}
// Find the maximum possible answer
for (int i = 0; i < possible.size(); i++) {
if (possible[i] >= L and possible[i] <= R)
ans = max(ans, possible[i]);
}
return ans;
}
// Driver Code
int main()
{
int arr[] = { 3, 2, -2, 5, -3 },
N = 5, L = 2, R = 3;
int max = findMax(N, arr, L, R);
// Display the output
cout << max << endl;
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to find the maximum value of K
static int findMax(int N, int []arr, int L, int R)
{
// Using a set to store the elements
HashSet s = new HashSet();
// ArrayList to store the possible answers
ArrayList possible
= new ArrayList();
// Store the answer
int ans = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// If set has it's negation,
// check if it is max
if (s.contains(arr[i] * -1))
possible.add(Math.abs(arr[i]));
else
s.add(arr[i]);
}
// Find the maximum possible answer
for (int i = 0; i < possible.size(); i++) {
if (possible.get(i) >= L && possible.get(i) <= R) {
ans = Math.max(ans, possible.get(i));
}
}
return ans;
}
// Driver Code
public static void main(String args[])
{
int []arr = { 3, 2, -2, 5, -3 };
int N = 5, L = 2, R = 3;
int max = findMax(N, arr, L, R);
// Display the output
System.out.println(max);
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python3 program for the above approach
# Function to find the maximum value of K
def findMax(N, arr, L, R) :
# Using a set to store the elements
s = set();
# Vector to store the possible answers
possible = [];
# Store the answer
ans = 0;
# Traverse the array
for i in range(N) :
# If set has it's negation,
# check if it is max
if arr[i] * -1 in s :
possible.append(abs(arr[i]));
else :
s.add(arr[i]);
# Find the maximum possible answer
for i in range(len(possible)) :
if (possible[i] >= L and possible[i] <= R) :
ans = max(ans, possible[i]);
return ans;
# Driver Code
if __name__ == "__main__" :
arr = [ 3, 2, -2, 5, -3 ];
N = 5; L = 2; R = 3;
Max = findMax(N, arr, L, R);
# Display the output
print(Max);
# This code is contributed by Ankthon
C#
// Java program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
public class GFG
{
// Function to find the maximum value of K
static int findMax(int N, int []arr, int L, int R)
{
// Using a set to store the elements
HashSet s = new HashSet();
// ArrayList to store the possible answers
ArrayList possible = new ArrayList();
// Store the answer
int ans = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// If set has it's negation,
// check if it is max
if (s.Contains(arr[i] * -1))
possible.Add(Math.Abs(arr[i]));
else
s.Add(arr[i]);
}
// Find the maximum possible answer
for (int i = 0; i < possible.Count; i++) {
if ((int)possible[i] >= L && (int)possible[i] <= R) {
ans = Math.Max(ans, (int)possible[i]);
}
}
return ans;
}
// Driver Code
public static void Main()
{
int []arr = { 3, 2, -2, 5, -3 };
int N = 5, L = 2, R = 3;
int max = findMax(N, arr, L, R);
// Display the output
Console.Write(max);;
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
3
时间复杂度: O(N)
辅助空间: O(N)