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++
// C++ program to of the above approach #include <bits/stdc++.h> 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
<script> // JavaScript code for the above approach // Function to calculate the maximum // distance between any two occurrence // of even integers in the given array function maximizeDistance(arr, n) { // Stores the index of // 1st even integer let i = 0; // Traverse array arr[] while (i < n) { if (arr[i] % 2 == 0) { break ; } i++; } // Stores the index of // last even integer let 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 let arr = [3, 4, 5, 6, 7, 8]; let N = arr.length; document.write(maximizeDistance(arr, N)); // This code is contributed by Potta Lokesh </script> |
4
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!