给定的阵列ARR []选自N正整数,一些元件为-1,任务是找到最小的数,说K,使得用K阵列中更换所有-1 S最小化的任何一对之间的最大绝对差相邻元素。
例子:
Input: arr[] = {-1, 10, -1, 12, -1}
Output: 11
Explanation:
Consider the value of K as 11. Now, replacing all array elements having value -1 to the value K(= 11) modifies the array to {11, 10, 11, 12, 11}. The maximum absolute difference among all the adjacent element is 1, which is minimum among all possible value of K.
Input: arr[] = {1, -1, 3, -1}
Output: 2
朴素的方法:解决给定问题的最简单方法是从1 开始迭代K 的所有可能值,逐一检查K 的哪个值给出了任何两个相邻元素之间最小的最大绝对差,并打印该值K 。
时间复杂度: O(N*K)
辅助空间: O(1)
高效的方法:上述方法也可以基于以下观察进行优化:
- 如果要从一组数字中最小化任何数字的绝对值,例如要最小化X,则该集合的最小和最大元素的平均值是使绝对值最小的X 的最佳值。
- 因此,想法是找到与元素“-1”相邻的所有数组元素的最小值和最大值,并将这两个数字的平均值打印为K的结果值。
请按照以下步骤解决问题:
- 初始化两个变量,比如maxE为 INT_MIN 和minE为 INT_MAX,以存储数组中与“-1”相邻的所有可能值中的最大和最小元素。
- 遍历给定数组并执行以下步骤:
- 如果当前元素arr[i]为“-1”且下一个元素不为“-1” ,则将maxE的值更新为maxE的最大值,将arr[i + 1]和minE的值更新为minE和arr[i + 1] 。
- 如果当前元素ARR [i]是不是“-1”和下一个元素是“-1”,则maxE的值更新为最大maxE的和常用3 [i]和矿到最小矿井和常用3 [我] 。
- 完成上述步骤后,如果maxE和minE 的值没有变化,则所有数组元素都是“-1” ,因此,打印0作为K的结果值。否则,打印minE和maxE的平均值作为K的结果值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the value of K to
// minimize the value of maximum absolute
// difference between adjacent elements
void findMissingValue(int arr[], int N)
{
// Stores the maximum and minimum
// among array elements that are
// adjacent to "-1"
int minE = INT_MAX, maxE = INT_MIN;
// Traverse the given array arr[]
for (int i = 0; i < N - 1; i++) {
// If arr[i] is -1 & arr[i + 1]
// is not -1
if (arr[i] == -1
&& arr[i + 1] != -1) {
minE = min(minE, arr[i + 1]);
maxE = max(maxE, arr[i + 1]);
}
// If arr[i + 1] is -1 & arr[i]
// is not -1
if (arr[i] != -1
&& arr[i + 1] == -1) {
minE = min(minE, arr[i]);
maxE = max(maxE, arr[i]);
}
}
// If all array element is -1
if (minE == INT_MAX
and maxE == INT_MIN) {
cout << "0";
}
// Otherwise
else {
cout << (minE + maxE) / 2;
}
}
// Driver Code
int main()
{
int arr[] = { 1, -1, -1, -1, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
findMissingValue(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to find the value of K to
// minimize the value of maximum absolute
// difference between adjacent elements
public static void findMissingValue(int arr[], int N)
{
// Stores the maximum and minimum
// among array elements that are
// adjacent to "-1"
int minE = Integer.MAX_VALUE,
maxE = Integer.MIN_VALUE;
// Traverse the given array arr[]
for(int i = 0; i < N - 1; i++)
{
// If arr[i] is -1 & arr[i + 1]
// is not -1
if (arr[i] == -1 && arr[i + 1] != -1)
{
minE = Math.min(minE, arr[i + 1]);
maxE = Math.max(maxE, arr[i + 1]);
}
// If arr[i + 1] is -1 & arr[i]
// is not -1
if (arr[i] != -1 && arr[i + 1] == -1)
{
minE = Math.min(minE, arr[i]);
maxE = Math.max(maxE, arr[i]);
}
}
// If all array element is -1
if (minE == Integer.MAX_VALUE &&
maxE == Integer.MIN_VALUE)
{
System.out.println("0");
}
// Otherwise
else
{
System.out.println((minE + maxE) / 2);
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, -1, -1, -1, 5 };
int N = arr.length;
findMissingValue(arr, N);
}
}
// This code is contributed by Potta Lokesh
Python3
# Python 3 program for the above approach
import sys
# Function to find the value of K to
# minimize the value of maximum absolute
# difference between adjacent elements
def findMissingValue(arr, N):
# Stores the maximum and minimum
# among array elements that are
# adjacent to "-1"
minE = sys.maxsize
maxE = -sys.maxsize - 1
# Traverse the given array arr[]
for i in range(N - 1):
# If arr[i] is -1 & arr[i + 1]
# is not -1
if (arr[i] == -1 and arr[i + 1] != -1):
minE = min(minE, arr[i + 1])
maxE = max(maxE, arr[i + 1])
# If arr[i + 1] is -1 & arr[i]
# is not -1
if (arr[i] != -1 and arr[i + 1] == -1):
minE = min(minE, arr[i])
maxE = max(maxE, arr[i])
# If all array element is -1
if (minE == sys.maxsize and maxE == -sys.maxsize-1):
print("0")
# Otherwise
else:
print((minE + maxE) // 2)
# Driver Code
if __name__ == '__main__':
arr = [1, -1, -1, -1, 5]
N = len(arr)
findMissingValue(arr, N)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the value of K to
// minimize the value of maximum absolute
// difference between adjacent elements
public static void findMissingValue(int[] arr, int N)
{
// Stores the maximum and minimum
// among array elements that are
// adjacent to "-1"
int minE = Int32.MaxValue,
maxE = Int32.MinValue;
// Traverse the given array arr[]
for(int i = 0; i < N - 1; i++)
{
// If arr[i] is -1 & arr[i + 1]
// is not -1
if (arr[i] == -1 && arr[i + 1] != -1)
{
minE = Math.Min(minE, arr[i + 1]);
maxE = Math.Max(maxE, arr[i + 1]);
}
// If arr[i + 1] is -1 & arr[i]
// is not -1
if (arr[i] != -1 && arr[i + 1] == -1)
{
minE = Math.Min(minE, arr[i]);
maxE = Math.Max(maxE, arr[i]);
}
}
// If all array element is -1
if (minE == Int32.MaxValue &&
maxE == Int32.MinValue)
{
Console.WriteLine("0");
}
// Otherwise
else
{
Console.WriteLine((minE + maxE) / 2);
}
}
// Driver Code
public static void Main()
{
int[] arr = { 1, -1, -1, -1, 5 };
int N = arr.Length;
findMissingValue(arr, N);
}
}
// This code is contributed by rishavmahato348
Javascript
3
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。