Given an array **arr[]** containing **N** numbers, the task is to merge the subarray of consecutive even numbers by replacing all consecutive even numbers by the first even element of that subarray.

**Note:** A series of even integers are said to be consecutive if there are at least three even numbers in the given series. Therefore, merge elements in subarray which have at least 3 consecutive even numbers.

**Examples:**

Input:arr[] = {2, 2, 2, 100, 5, 4, 2, 9, 10, 88, 24}

Output:2 5 4 2 9 10

Explanation:

The given series contains two consecutive even subsequences. They are:

{2, 2, 2, 100}, {10, 88, 24}. The two subsequences have to be merged to the first element in the subseries.

Therefore, {2, 2, 2, 100} is replaced by 2 and {10, 88, 24} is replaced by 10 in the original series.

Input:arr[] = {2, 4, 5, 3, 6, 8, 10, 3, 4}

Output:2 4 5 3 6 3 4

**Approach:** In order to solve this problem, we need to first find if there exists a consecutive even subsequence with size greater than three. Therefore, the idea is to iterate through the given array and check if a number is even or not.

- Traverse through the array.
- Check if the element is even or not.
- If it is even, creates a temporary array to store the next continuous even numbers until the next number is odd.
- Continue adding the elements in the temp array until an odd number occurs.
- If the size of this temporary array is greater than three, then remove these elements from the given array and replace them with the first element of this temporary array.
- Empty the temporary array to compute the next set of even subsequence.

Below is the implementation of the above approach:

## C++

`// C++ program to merge the array ` `// as per the given condition ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to merge the array ` `// as per the given condition ` `vector<` `int` `> merge(vector<` `int` `> arr) ` `{ ` ` ` ` ` `// Variable to store the final ` ` ` `// sequence ` ` ` `vector<` `int` `> ans; ` ` ` ` ` `// Temporary array to store the ` ` ` `// even numbers while traversing ` ` ` `vector<` `int` `> e; ` ` ` `int` `i = 0; ` ` ` `int` `j = 0; ` ` ` `int` `count = 0; ` ` ` ` ` `// Iterating through the array ` ` ` `while` `(i < arr.size()) ` ` ` `{ ` ` ` ` ` `// If the element is even ` ` ` `if` `(arr[i] % 2 == 0) ` ` ` `{ ` ` ` `j = i; ` ` ` ` ` `// Iterating till an odd element ` ` ` `// is found ` ` ` `while` `(j < arr.size()) ` ` ` `{ ` ` ` ` ` `// Keep appending into the ` ` ` `// temporary array if the ` ` ` `// even number is occurred ` ` ` `if` `(arr[j] % 2 == 0) ` ` ` `{ ` ` ` `e.push_back(arr[j]); ` ` ` `count += 1; ` ` ` `} ` ` ` ` ` `// Break if an odd number ` ` ` `// has occurred ` ` ` `else` ` ` `break` `; ` ` ` ` ` `j += 1; ` ` ` `} ` ` ` ` ` `// If the series has at least ` ` ` `// three elements, then merge ` ` ` `if` `(count >= 3) ` ` ` `ans.push_back(e[0]); ` ` ` ` ` `// Else, add all elements to the ` ` ` `// answer array ` ` ` `else` ` ` `{ ` ` ` `for` `(` `auto` `i: e) ` ` ` `ans.push_back(i); ` ` ` `} ` ` ` ` ` `// Reseting the count and ` ` ` `// temp array ` ` ` `count = 0; ` ` ` `e.clear(); ` ` ` `i = j; ` ` ` `} ` ` ` ` ` `// If the element is odd, add ` ` ` `// it to the answer array ` ` ` `else` ` ` `{ ` ` ` `ans.push_back(arr[i]); ` ` ` `i += 1; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `vector<` `int` `> arr({ 2, 2, 2, 100, 5, 4, ` ` ` `2, 9, 10, 88, 24 }); ` ` ` `vector<` `int` `> ans = merge(arr); ` ` ` ` ` `cout << ` `"["` `; ` ` ` `for` `(` `int` `i= 0; i < ans.size(); i++) ` ` ` `{ ` ` ` `if` `(i == ans.size() - 1) ` ` ` `cout << ans[i] << ` `"]"` `; ` ` ` `else` ` ` `cout << ans[i] << ` `", "` `; ` ` ` `} ` ` ` `} ` ` ` `// This code is contributed by Samarth ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to merge the array ` `# as per the given condition ` ` ` `# Function to merge the array ` `# as per the given condition ` `def` `merge(arr): ` ` ` ` ` `# Variable to store the final ` ` ` `# sequence ` ` ` `ans ` `=` `[] ` ` ` ` ` `# Temporary array to store the ` ` ` `# even numbers while traversing ` ` ` `e ` `=` `[] ` ` ` `i ` `=` `0` ` ` `j ` `=` `0` ` ` `count ` `=` `0` ` ` ` ` `# Iterating through the array ` ` ` `while` `i<` `len` `(arr): ` ` ` ` ` `# If the element is even ` ` ` `if` `(arr[i]` `%` `2` `=` `=` `0` `): ` ` ` `j ` `=` `i ` ` ` ` ` `# Iterating till an odd element ` ` ` `# is found ` ` ` `while` `j<` `len` `(arr): ` ` ` ` ` `# Keep appending into the ` ` ` `# temporary array if the ` ` ` `# even number is occurred ` ` ` `if` `arr[j]` `%` `2` `=` `=` `0` `: ` ` ` ` ` `e.append(arr[j]) ` ` ` `count` `+` `=` `1` ` ` ` ` `# Break if an odd number ` ` ` `# has occurred ` ` ` `else` `: ` ` ` `break` ` ` ` ` `j` `+` `=` `1` ` ` ` ` `# If the series has at least ` ` ` `# three elements, then merge ` ` ` `if` `(count>` `=` `3` `): ` ` ` `ans.append(e[` `0` `]) ` ` ` ` ` `# Else, add all elements to the ` ` ` `# answer array ` ` ` `else` `: ` ` ` `for` `i ` `in` `e: ` ` ` `ans.append(i) ` ` ` ` ` `# Reseting the count and ` ` ` `# temp array ` ` ` `count ` `=` `0` ` ` `e ` `=` `[] ` ` ` `i ` `=` `j ` ` ` ` ` `# If the element is odd, add ` ` ` `# it to the answer array ` ` ` `else` `: ` ` ` `ans.append(arr[i]) ` ` ` `i` `+` `=` `1` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[` `2` `, ` `2` `, ` `2` `, ` `100` `, ` `5` `, ` `4` `, ` `2` `, ` `9` `, ` `10` `, ` `88` `, ` `24` `] ` ` ` ` ` `print` `(merge(arr)) ` |

*chevron_right*

*filter_none*

**Output:**

[2, 5, 4, 2, 9, 10]

**Time Complexity:** *O(N ^{2})*, where N is the length of the array

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Rearrange array such that all even-indexed elements in the Array is even
- Maximum sum of even indexed elements obtained by right shift on an even sized subarray
- Maximum length of subarray such that sum of the subarray is even
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Maximum length of subarray such that all elements are equal in the subarray
- Merge first two minimum elements of the array until all the elements are greater than K
- Reverse a subarray of the given array to minimize the sum of elements at even position
- Reverse a subarray to maximize sum of even-indexed elements of given array
- Length of the longest Subarray with only Even Elements
- Length of longest Subarray with equal number of odd and even elements
- Count of odd and even parity elements in subarray using MO's algorithm
- Query to count odd and even parity elements in subarray after XOR with K
- Check in binary array the number represented by a subarray is odd or even
- Minimum cost to merge all elements of List
- Rotate all odd numbers right and all even numbers left in an Array of 1 to N
- Shortest subarray to be removed to make all Array elements unique
- Minimum Subarray flips required to convert all elements of a Binary Array to K
- Maximize Sum possible by subtracting same value from all elements of a Subarray of the given Array
- Maximize sum of an Array by flipping sign of all elements of a single subarray
- Minimize subarray increments/decrements required to reduce all array elements to 0

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.