Maximum distance between two even integers in a given array
Last Updated :
10 Mar, 2022
Given an array arr[] having N integers, the task is to find the maximum distance between any two occurrences of even integers.
Examples:
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.
Naive Approach: The given problem can be solved by checking the distance between all pairs of even integers occurring in the array and maintaining the maximum in them which will be the required answer.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: This problem can be solved using Two Pointer approach.
- Handle the case with less than 2 even integers separately.
- The index of maximum distance even integers will be the index of the first and the last occurrence of even integers.
- This can be done simply by traversing the array using two pointers – one from start and one from end.
- As soon an even integer is reached from both pointers, return the distance between them.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximizeDistance( int arr[], int n)
{
int i = 0;
while (i < n) {
if (arr[i] % 2 == 0) {
break ;
}
i++;
}
int j = n - 1;
while (j >= 0) {
if (arr[j] % 2 == 0) {
break ;
}
j--;
}
if (i >= j) {
return 0;
}
return j - i;
}
int main()
{
int arr[] = { 3, 4, 5, 6, 7, 8 };
int N = sizeof (arr) / sizeof ( int );
cout << maximizeDistance(arr, N);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int maximizeDistance( int arr[], int n)
{
int i = 0 ;
while (i < n) {
if (arr[i] % 2 == 0 ) {
break ;
}
i++;
}
int j = n - 1 ;
while (j >= 0 ) {
if (arr[j] % 2 == 0 ) {
break ;
}
j--;
}
if (i >= j) {
return 0 ;
}
return j - i;
}
public static void main(String args[])
{
int arr[] = { 3 , 4 , 5 , 6 , 7 , 8 };
int N = arr.length;
System.out.print(maximizeDistance(arr, N));
}
}
|
Python3
def maximizeDistance(arr, n):
i = 0
while (i < n):
if (arr[i] % 2 = = 0 ):
break
i + = 1
j = n - 1
while (j > = 0 ):
if (arr[j] % 2 = = 0 ):
break
j - = 1
if (i > = j):
return 0
return j - i
if __name__ = = "__main__" :
arr = [ 3 , 4 , 5 , 6 , 7 , 8 ]
N = len (arr)
print (maximizeDistance(arr, N))
|
C#
using System;
class GFG {
static int maximizeDistance( int [] arr, int n)
{
int i = 0;
while (i < n) {
if (arr[i] % 2 == 0) {
break ;
}
i++;
}
int j = n - 1;
while (j >= 0) {
if (arr[j] % 2 == 0) {
break ;
}
j--;
}
if (i >= j) {
return 0;
}
return j - i;
}
public static void Main()
{
int [] arr = { 3, 4, 5, 6, 7, 8 };
int N = arr.Length;
Console.Write(maximizeDistance(arr, N));
}
}
|
Javascript
<script>
function maximizeDistance(arr, n)
{
let i = 0;
while (i < n) {
if (arr[i] % 2 == 0) {
break ;
}
i++;
}
let j = n - 1;
while (j >= 0) {
if (arr[j] % 2 == 0) {
break ;
}
j--;
}
if (i >= j) {
return 0;
}
return j - i;
}
let arr = [3, 4, 5, 6, 7, 8];
let N = arr.length;
document.write(maximizeDistance(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...