Merge the elements in subarray of all even elements of the Array
Last Updated :
24 Apr, 2023
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 a 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 subsequences.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
vector< int > merge(vector< int > arr)
{
vector< int > ans;
vector< int > e;
int i = 0;
int j = 0;
int count = 0;
while (i < arr.size())
{
if (arr[i] % 2 == 0)
{
j = i;
while (j < arr.size())
{
if (arr[j] % 2 == 0)
{
e.push_back(arr[j]);
count += 1;
}
else
break ;
j += 1;
}
if (count >= 3)
ans.push_back(e[0]);
else
{
for ( auto i: e)
ans.push_back(i);
}
count = 0;
e.clear();
i = j;
}
else
{
ans.push_back(arr[i]);
i += 1;
}
}
return ans;
}
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] << ", " ;
}
}
|
Java
import java.util.*;
class GFG{
static Vector<Integer> merge( int []arr)
{
Vector<Integer> ans = new Vector<Integer>();
Vector<Integer> e = new Vector<Integer>();
int i = 0 ;
int j = 0 ;
int count = 0 ;
while (i < arr.length)
{
if (arr[i] % 2 == 0 )
{
j = i;
while (j < arr.length)
{
if (arr[j] % 2 == 0 )
{
e.add(arr[j]);
count += 1 ;
}
else
break ;
j += 1 ;
}
if (count >= 3 )
ans.add(e.get( 0 ));
else
{
for ( int ii : e)
ans.add(ii);
}
count = 0 ;
e.clear();
i = j;
}
else
{
ans.add(arr[i]);
i += 1 ;
}
}
return ans;
}
public static void main(String[] args)
{
int []arr = { 2 , 2 , 2 , 100 , 5 , 4 ,
2 , 9 , 10 , 88 , 24 };
Vector<Integer> ans = merge(arr);
System.out.print( "[" );
for ( int i= 0 ; i < ans.size(); i++)
{
if (i == ans.size() - 1 )
System.out.print(ans.get(i) + "]" );
else
System.out.print(ans.get(i) + ", " );
}
}
}
|
Python3
def merge(arr):
ans = []
e = []
i = 0
j = 0
count = 0
while i< len (arr):
if (arr[i] % 2 = = 0 ):
j = i
while j< len (arr):
if arr[j] % 2 = = 0 :
e.append(arr[j])
count + = 1
else :
break
j + = 1
if (count> = 3 ):
ans.append(e[ 0 ])
else :
for i in e:
ans.append(i)
count = 0
e = []
i = j
else :
ans.append(arr[i])
i + = 1
return ans
if __name__ = = "__main__" :
arr = [ 2 , 2 , 2 , 100 , 5 , 4 , 2 , 9 , 10 , 88 , 24 ]
print (merge(arr))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static List< int > merge( int []arr)
{
List< int > ans = new List< int >();
List< int > e = new List< int >();
int i = 0;
int j = 0;
int count = 0;
while (i < arr.Length)
{
if (arr[i] % 2 == 0)
{
j = i;
while (j < arr.Length)
{
if (arr[j] % 2 == 0)
{
e.Add(arr[j]);
count += 1;
}
else
break ;
j += 1;
}
if (count >= 3)
ans.Add(e[0]);
else
{
foreach ( int ii in e)
ans.Add(ii);
}
count = 0;
e.Clear();
i = j;
}
else
{
ans.Add(arr[i]);
i += 1;
}
}
return ans;
}
public static void Main(String[] args)
{
int []arr = {2, 2, 2, 100, 5, 4,
2, 9, 10, 88, 24};
List< int > ans = merge(arr);
Console.Write( "[" );
for ( int i= 0; i < ans.Count; i++)
{
if (i == ans.Count - 1)
Console.Write(ans[i] + "]" );
else
Console.Write(ans[i] + ", " );
}
}
}
|
Javascript
class GFG
{
static merge(arr)
{
var ans = new Array();
var e = new Array();
var i = 0;
var j = 0;
var count = 0;
while (i < arr.length)
{
if (arr[i] % 2 == 0)
{
j = i;
while (j < arr.length)
{
if (arr[j] % 2 == 0)
{
(e.push(arr[j]) > 0);
count += 1;
}
else
{
break ;
}
j += 1;
}
if (count >= 3)
{
(ans.push(e[0]) > 0);
}
else
{
for ( const ii of e) {(ans.push(ii) > 0);}
}
count = 0;
e = new Array();
i = j;
}
else
{
(ans.push(arr[i]) > 0);
i += 1;
}
}
return ans;
}
static main(args)
{
var arr = [2, 2, 2, 100, 5, 4, 2, 9, 10, 88, 24];
var ans = GFG.merge(arr);
console.log( "[" );
for ( var i=0; i < ans.length; i++)
{
if (i == ans.length - 1)
{
console.log(ans[i] + "]" );
}
else
{
console.log(ans[i] + ", " );
}
}
}
}
GFG.main([]);
|
Output:
[2, 5, 4, 2, 9, 10]
Time Complexity: O(N2), where N is the length of the array.
Share your thoughts in the comments
Please Login to comment...