给定的阵列ARR []由N个整数的,任务是通过重复地更换任何一对连续的不相等的元素,以减少给定阵列的单个元件,说ARR [i]和常用3第[i + 1],最大(ARR [ i],arr [i + 1])+1 。如果可能,从可开始操作的位置打印元素的索引。否则,打印-1 。
例子:
Input: arr[] = {5, 3, 4, 4, 5}
Output: 1
Explanation:
Step 1: Replace arr[1] and arr[2] with max(arr[1], arr[2])+1 = max(5, 3) + 1 = 6. Therefore, arr[] = {6, 4, 4, 5}.
Step 2: Replace arr[1] and arr[2] with max(arr[1], arr[2]) + 1 = max(6, 4) + 1 = 7. Therefore, arr[] = {7, 4, 5}.
Step 3: Replace arr[1] and arr[2] with max(arr[1], arr[2])+1 = max(7, 4) + 1 = 8. Therefore, arr[] = {8, 5}.
Step 4: Replace arr[1] and arr[2] with max(arr[1], arr[2]) + 1 = max(8, 5)+1 = 9. Therefore, arr[] = {9}.
Input: arr[] ={1, 1}
Output: -1
天真的方法:最简单的方法是遍历给定的数组,并对每个元素arr [i] ,根据给定的约束条件开始连续执行给定的操作。如果对于任何元素,该数组变为单个元素,则打印索引i,否则打印-1 。
时间复杂度: O(N 2 )
辅助空间: O(N)
高效方法:想法是使用排序算法。请注意,如果所有元素都相同,答案将始终为-1 。否则,可以选择具有最大元素的索引以开始执行操作。请按照以下步骤解决问题:
- 创建另一个与给定数组相同的数组B []并创建一个变量save,以-1初始化以存储答案。
- 对数组B []排序。
- 使用变量i在[N – 1到0]范围内遍历数组,如果找到两个连续的不相等元素,即B [i]不等于B [i – 1] ,则将save更新为save = i 。
- 遍历数组后:
- 如果save为-1 ,则打印-1并返回。
- 否则,如果save等于arr [0],而save不等于arr [1] ,则将save更新为1 。
- 否则,如果save等于arr [N – 1]且save不等于arr [N – 2] ,则将save更新为N。
- 否则,在[1,N – 1]范围内循环,并检查save是否等于arr [i] ,以使arr [i]不等于arr [i – 1]和arr [i + 1] ,然后更新另存为save = i + 1 。
- 完成上述步骤后,打印存储在变量save中的索引。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print the index from
// where the operation can be started
void printIndex(int arr[], int N)
{
// Initialize B[]
int B[N];
// Initialize save
int save = -1;
// Make B[] equals to arr[]
for (int i = 0; i < N; i++) {
B[i] = arr[i];
}
// Sort the array B[]
sort(B, B + N);
// Traverse from N-1 to 1
for (int i = N - 1; i >= 1; i--) {
// If B[i] & B[i-1] are unequal
if (B[i] != B[i - 1]) {
save = B[i];
break;
}
}
// If all elements are same
if (save == -1) {
cout << -1 << endl;
return;
}
// If arr[1] is maximum element
if (save == arr[0]
&& save != arr[1]) {
cout << 1;
}
// If arr[N-1] is maximum element
else if (save == arr[N - 1]
&& save != arr[N - 2]) {
cout << N;
}
// Find the maximum element
for (int i = 1; i < N - 1; i++) {
if (save == arr[i]
&& (save != arr[i - 1]
|| save != arr[i + 1])) {
cout << i + 1;
break;
}
}
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 5, 3, 4, 4, 5 };
// Length of array
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
printIndex(arr, N);
return 0;
}
Java
// Java program for the
// above approach
import java.util.*;
class GFG{
// Function to print the index
// from where the operation can
// be started
static void printIndex(int arr[],
int N)
{
// Initialize B[]
int []B = new int[N];
// Initialize save
int save = -1;
// Make B[] equals to arr[]
for (int i = 0; i < N; i++)
{
B[i] = arr[i];
}
// Sort the array B[]
Arrays.sort(B);
// Traverse from N-1 to 1
for (int i = N - 1; i >= 1; i--)
{
// If B[i] & B[i-1] are
// unequal
if (B[i] != B[i - 1])
{
save = B[i];
break;
}
}
// If all elements are same
if (save == -1)
{
System.out.print(-1 + "\n");
return;
}
// If arr[1] is maximum
// element
if (save == arr[0] &&
save != arr[1])
{
System.out.print(1);
}
// If arr[N-1] is maximum
// element
else if (save == arr[N - 1] &&
save != arr[N - 2])
{
System.out.print(N);
}
// Find the maximum element
for (int i = 1; i < N - 1; i++)
{
if (save == arr[i] &&
(save != arr[i - 1] ||
save != arr[i + 1]))
{
System.out.print(i + 1);
break;
}
}
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = {5, 3, 4, 4, 5};
// Length of array
int N = arr.length;
// Function Call
printIndex(arr, N);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the
# above approach
# Function to prthe index
# from where the operation can
# be started
def printIndex(arr, N):
# Initialize B
B = [0] * (N)
# Initialize save
save = -1
# Make B equals to arr
for i in range(N):
B[i] = arr[i]
# Sort the array B
B = sorted(B)
# Traverse from N-1 to 1
for i in range(N - 1, 1, -1):
# If B[i] & B[i-1] are
# unequal
if (B[i] != B[i - 1]):
save = B[i]
break
# If all elements are same
if (save == -1):
print(-1 + "")
return
# If arr[1] is maximum
# element
if (save == arr[0] and
save != arr[1]):
print(1)
# If arr[N-1] is maximum
# element
elif (save == arr[N - 1] and
save != arr[N - 2]):
print(N)
# Find the maximum element
for i in range(1, N - 1):
if (save == arr[i] and
(save != arr[i - 1] or
save != arr[i + 1])):
print(i + 1)
break
# Driver Code
if __name__ == '__main__':
# Given array arr
arr = [ 5, 3, 4, 4, 5 ]
# Length of array
N = len(arr)
# Function Call
printIndex(arr, N)
# This code is contributed by Rajput-Ji
C#
// C# program for the
// above approach
using System;
class GFG{
// Function to print the index
// from where the operation can
// be started
static void printIndex(int []arr,
int N)
{
// Initialize []B
int []B = new int[N];
// Initialize save
int save = -1;
// Make []B equals to []arr
for(int i = 0; i < N; i++)
{
B[i] = arr[i];
}
// Sort the array []B
Array.Sort(B);
// Traverse from N-1 to 1
for(int i = N - 1; i >= 1; i--)
{
// If B[i] & B[i-1] are
// unequal
if (B[i] != B[i - 1])
{
save = B[i];
break;
}
}
// If all elements are same
if (save == -1)
{
Console.Write(-1 + "\n");
return;
}
// If arr[1] is maximum
// element
if (save == arr[0] &&
save != arr[1])
{
Console.Write(1);
}
// If arr[N-1] is maximum
// element
else if (save == arr[N - 1] &&
save != arr[N - 2])
{
Console.Write(N);
}
// Find the maximum element
for(int i = 1; i < N - 1; i++)
{
if (save == arr[i] &&
(save != arr[i - 1] ||
save != arr[i + 1]))
{
Console.Write(i + 1);
break;
}
}
}
// Driver Code
public static void Main(String[] args)
{
// Given array []arr
int []arr = { 5, 3, 4, 4, 5 };
// Length of array
int N = arr.Length;
// Function Call
printIndex(arr, N);
}
}
// This code is contributed by Amit Katiyar
Javascript
1
时间复杂度: O(N)
辅助空间: O(N)