给定数组中两个偶数之间的最大距离
给定一个包含 N 个整数的数组arr [] ,任务是找到任意两次偶数出现之间的最大距离。
例子:
Input: arr[] = {1, 2, 3, 4, 5, 6, 7}
Output: 4
Explanation: The distance between arr[1] = 2 and arr[5] = 6 is 4 which is the maximum distance between two even integers present in the given array.
Input: arr[] = {3, 5, 6, 9, 11}
Output: 0
Explanation: The given array contains less than 2 even integers. Hence, the maximum distance is 0.
朴素方法:给定问题可以通过检查数组中出现的所有偶数对之间的距离并保持其中的最大值来解决,这将是所需的答案。
时间复杂度: O(N 2 )
辅助空间: O(1)
有效方法:这个问题可以使用两指针方法来解决。
- 小于2 个偶数的情况分别处理。
- 最大距离偶数的索引将是偶数第一次和最后一次出现的索引。
- 这可以简单地通过使用两个指针遍历数组来完成——一个从开始,一个从结束。
- 一旦两个指针都达到偶数,返回它们之间的距离。
下面是上述方法的实现:
C++
// C++ program to of the above approach
#include
using namespace std;
// Function to calculate the maximum
// distance between any two occurrence
// of even integers in the given array
int maximizeDistance(int arr[], int n)
{
// Stores the index of
// 1st even integer
int i = 0;
// Traverse array arr[]
while (i < n) {
if (arr[i] % 2 == 0) {
break;
}
i++;
}
// Stores the index of
// last even integer
int j = n - 1;
// Traverse array arr[]
// in reverse direction
while (j >= 0) {
if (arr[j] % 2 == 0) {
break;
}
j--;
}
// Case where arr[] has less
// that two even integers
if (i >= j) {
return 0;
}
// Return Answer
return j - i;
}
// Driver Code
int main()
{
int arr[] = { 3, 4, 5, 6, 7, 8 };
int N = sizeof(arr) / sizeof(int);
cout << maximizeDistance(arr, N);
return 0;
}
Java
// Java program to of the above approach
import java.util.*;
public class GFG {
// Function to calculate the maximum
// distance between any two occurrence
// of even integers in the given array
static int maximizeDistance(int arr[], int n)
{
// Stores the index of
// 1st even integer
int i = 0;
// Traverse array arr[]
while (i < n) {
if (arr[i] % 2 == 0) {
break;
}
i++;
}
// Stores the index of
// last even integer
int j = n - 1;
// Traverse array arr[]
// in reverse direction
while (j >= 0) {
if (arr[j] % 2 == 0) {
break;
}
j--;
}
// Case where arr[] has less
// that two even integers
if (i >= j) {
return 0;
}
// Return Answer
return j - i;
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 3, 4, 5, 6, 7, 8 };
int N = arr.length;
System.out.print(maximizeDistance(arr, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# python3 program to of the above approach
# Function to calculate the maximum
# distance between any two occurrence
# of even integers in the given array
def maximizeDistance(arr, n):
# Stores the index of
# 1st even integer
i = 0
# Traverse array arr[]
while (i < n):
if (arr[i] % 2 == 0):
break
i += 1
# Stores the index of
# last even integer
j = n - 1
# Traverse array arr[]
# in reverse direction
while (j >= 0):
if (arr[j] % 2 == 0):
break
j -= 1
# Case where arr[] has less
# that two even integers
if (i >= j):
return 0
# Return Answer
return j - i
# Driver Code
if __name__ == "__main__":
arr = [3, 4, 5, 6, 7, 8]
N = len(arr)
print(maximizeDistance(arr, N))
# This code is contributed by rakeshsahni
C#
// C# program to of the above approach
using System;
class GFG {
// Function to calculate the maximum
// distance between any two occurrence
// of even integers in the given array
static int maximizeDistance(int[] arr, int n)
{
// Stores the index of
// 1st even integer
int i = 0;
// Traverse array arr[]
while (i < n) {
if (arr[i] % 2 == 0) {
break;
}
i++;
}
// Stores the index of
// last even integer
int j = n - 1;
// Traverse array arr[]
// in reverse direction
while (j >= 0) {
if (arr[j] % 2 == 0) {
break;
}
j--;
}
// Case where arr[] has less
// that two even integers
if (i >= j) {
return 0;
}
// Return Answer
return j - i;
}
// Driver Code
public static void Main()
{
int[] arr = { 3, 4, 5, 6, 7, 8 };
int N = arr.Length;
Console.Write(maximizeDistance(arr, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
4
时间复杂度: O(N)
辅助空间: O(1)