Make all array elements even by replacing adjacent pair of array elements with their sum
Given an array arr[] of size N, the task is to make all array elements even by replacing a pair of adjacent elements with their sum.
Examples:
Input: arr[] = { 2, 4, 5, 11, 6 }
Output: 1
Explanation:
Replacing a pair (arr[2], arr[3]) with their sum ( = 5 + 11 = 16) modifies arr[] to { 2, 4, 16, 16, 6 }
Since all array elements are even, the required output is 1.
Input: arr[] = { 1, 2, 4, 3, 11 }
Output: 3
Explanation:
Replacing the pair (arr[3], arr[4]) and replacing them with their sum ( = 3 + 11 = 14) modifies arr[] to { 1, 2, 4, 14, 14 }
Replacing the pair (arr[0], arr[1]) and replacing them with their sum ( = 1 + 2 = 3) modifies arr[] to { 3, 3, 4, 14, 14 }
Replacing the pair (arr[0], arr[1]) with their sum ( = 3 + 3 = 6) modifies arr[] to { 6, 6, 4, 14, 14 }.
Therefore, the required output is 3.
Approach: The idea is to use the fact that the sum of two odd numbers generates an even number. Follow the steps below to solve the problem:
- Initialize two integers, say res, to count the number of replacements, and odd_continuous_segment, to count the number of continuous odd numbers
- Traverse the array and check the following conditions for every array element:
- If arr[i] is odd, then increment the count of odd_continuous_segment by 1
- Otherwise, if odd_continuous_segment is odd, then increment res by odd_continuous_segment/2. Otherwise, increment res by odd_continuous_segment / 2 + 2 and assign odd_continuous_segment to 0.
- Check if odd_continuous_segment is odd. If found to be true, then increment res by odd_continuous_segment / 2. Otherwise increment res by (odd_continuous_segment / 2 + 2)
- Finally, print the obtained value of res
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int make_array_element_even( int arr[], int N)
{
int res = 0;
int odd_cont_seg = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] % 2 == 1) {
odd_cont_seg++;
}
else {
if (odd_cont_seg > 0) {
if (odd_cont_seg % 2 == 0) {
res += odd_cont_seg / 2;
}
else {
res += (odd_cont_seg / 2) + 2;
}
odd_cont_seg = 0;
}
}
}
if (odd_cont_seg > 0) {
if (odd_cont_seg % 2 == 0) {
res += odd_cont_seg / 2;
}
else {
res += odd_cont_seg / 2 + 2;
}
}
return res;
}
int main()
{
int arr[] = { 2, 4, 5, 11, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << make_array_element_even(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int make_array_element_even( int arr[], int N)
{
int res = 0 ;
int odd_cont_seg = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (arr[i] % 2 == 1 )
{
odd_cont_seg++;
}
else
{
if (odd_cont_seg > 0 )
{
if (odd_cont_seg % 2 == 0 )
{
res += odd_cont_seg / 2 ;
}
else
{
res += (odd_cont_seg / 2 ) + 2 ;
}
odd_cont_seg = 0 ;
}
}
}
if (odd_cont_seg > 0 )
{
if (odd_cont_seg % 2 == 0 )
{
res += odd_cont_seg / 2 ;
}
else
{
res += odd_cont_seg / 2 + 2 ;
}
}
return res;
}
public static void main(String[] args)
{
int arr[] = { 2 , 4 , 5 , 11 , 6 };
int N = arr.length;
System.out.print(make_array_element_even(arr, N));
}
}
|
Python3
def make_array_element_even(arr, N):
res = 0
odd_cont_seg = 0
for i in range ( 0 , N):
if (arr[i] % 2 = = 1 ):
odd_cont_seg + = 1
else :
if (odd_cont_seg > 0 ):
if (odd_cont_seg % 2 = = 0 ):
res + = odd_cont_seg / / 2
else :
res + = (odd_cont_seg / / 2 ) + 2
odd_cont_seg = 0
if (odd_cont_seg > 0 ):
if (odd_cont_seg % 2 = = 0 ):
res + = odd_cont_seg / / 2
else :
res + = odd_cont_seg / / 2 + 2
return res
arr = [ 2 , 4 , 5 , 11 , 6 ]
N = len (arr)
print (make_array_element_even(arr, N))
|
C#
using System;
public class GFG
{
static int make_array_element_even( int []arr, int N)
{
int res = 0;
int odd_cont_seg = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] % 2 == 1)
{
odd_cont_seg++;
}
else
{
if (odd_cont_seg > 0)
{
if (odd_cont_seg % 2 == 0)
{
res += odd_cont_seg / 2;
}
else
{
res += (odd_cont_seg / 2) + 2;
}
odd_cont_seg = 0;
}
}
}
if (odd_cont_seg > 0)
{
if (odd_cont_seg % 2 == 0)
{
res += odd_cont_seg / 2;
}
else
{
res += odd_cont_seg / 2 + 2;
}
}
return res;
}
public static void Main(String[] args)
{
int []arr = { 2, 4, 5, 11, 6 };
int N = arr.Length;
Console.Write(make_array_element_even(arr, N));
}
}
|
Javascript
<script>
function make_array_element_even(arr, N)
{
let res = 0;
let odd_cont_seg = 0;
for (let i = 0; i < N; i++)
{
if (arr[i] % 2 == 1)
{
odd_cont_seg++;
}
else
{
if (odd_cont_seg > 0)
{
if (odd_cont_seg % 2 == 0)
{
res += odd_cont_seg / 2;
}
else
{
res += (odd_cont_seg / 2) + 2;
}
odd_cont_seg = 0;
}
}
}
if (odd_cont_seg > 0)
{
if (odd_cont_seg % 2 == 0)
{
res += odd_cont_seg / 2;
}
else
{
res += odd_cont_seg / 2 + 2;
}
}
return res;
}
let arr = [ 2, 4, 5, 11, 6 ];
let N = arr.length;
document.write(make_array_element_even(arr, N));
</script>
|
Time complexity: O(N)
Auxiliary space: O(1)
Last Updated :
28 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...