Make all the elements of array odd by incrementing odd-indexed elements of odd-length subarrays
Given an array arr[] of size N, the task is to make all the array elements odd by choosing an odd length subarray of arr[] and increment all odd positioned elements by 1 in this subarray. Print the count of such operations required.
Examples:
Input: arr[] = {2, 3, 4, 3, 5, 3, 2}
Output: 2
Explanation:
In first operation, choose the subarray {2, 3, 4} and increment all its elements at odd positions, i.e., 1 and 3 of this subarray. The updated array is {3, 3, 5, 3, 5, 3, 2}.
In second operation, choose the last index which is subarray of length 1 and increment its value. The updated array is {3, 3, 5, 3, 5, 3, 3}
Input: arr[] = {1, 5, 7}
Output: 0
Explanation: Since all array elements are odd, no changes required.
Approach: The idea is based on the observation that whenever a subarray is chosen, either the odd positioned values are changed or the even positioned values in the original array. The problem can be solved by choosing the subarray greedily in each operation. First, iterate over all the odd indices and mark the starting of the subarray as soon as an even value is found, and end the subarray when an odd value is found, simultaneously updating the number of operations. Repeat the same process for even indices. Follow the steps below to solve the problem:
- Initialize a variable, say flips, to store the minimum number of operations required.
- Traverse even indices of the array arr[] perform the following steps:
- If the current element is odd, then continue iterating.
- Otherwise, iterate every 2nd element starting from that index, until an even element is encountered. After complete traversal of the array or if an even element is encountered, increment flips by 1.
- Repeat step 2 for odd indices also.
- After completing the above steps, print the value of flips as the minimum number of operations as required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minOperations( int arr[], int n)
{
int flips = 0;
for ( int i = 0; i < n; i += 2) {
if (arr[i] % 2 == 1) {
continue ;
}
while (i < n && arr[i] % 2 == 0) {
i += 2;
}
flips++;
}
for ( int i = 1; i < n; i += 2) {
if (arr[i] % 2 == 1) {
continue ;
}
while (i < n && arr[i] % 2 == 0) {
i += 2;
}
flips++;
}
cout << flips;
}
int main()
{
int arr[] = { 2, 3, 4, 3, 5, 3, 2 };
int N = sizeof (arr) / sizeof ( int );
minOperations(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void minOperations( int arr[], int n)
{
int flips = 0 ;
for ( int i = 0 ; i < n; i += 2 )
{
if (arr[i] % 2 == 1 )
{
continue ;
}
while (i < n && arr[i] % 2 == 0 )
{
i += 2 ;
}
flips++;
}
for ( int i = 1 ; i < n; i += 2 )
{
if (arr[i] % 2 == 1 )
{
continue ;
}
while (i < n && arr[i] % 2 == 0 )
{
i += 2 ;
}
flips++;
}
System.out.println(flips);
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 4 , 3 , 5 , 3 , 2 };
int N = arr.length;
minOperations(arr, N);
}
}
|
C#
using System;
class GFG
{
static void minOperations( int []arr, int n)
{
int flips = 0;
for ( int i = 0; i < n; i += 2)
{
if (arr[i] % 2 == 1)
{
continue ;
}
while (i < n && arr[i] % 2 == 0)
{
i += 2;
}
flips++;
}
for ( int i = 1; i < n; i += 2)
{
if (arr[i] % 2 == 1)
{
continue ;
}
while (i < n && arr[i] % 2 == 0)
{
i += 2;
}
flips++;
}
Console.WriteLine(flips);
}
public static void Main(String[] args)
{
int []arr = { 2, 3, 4, 3, 5, 3, 2 };
int N = arr.Length;
minOperations(arr, N);
}
}
|
Python3
def minOperations(arr, n) :
flips = 0 ;
i = 0 ;
while i < n :
if (arr[i] % 2 = = 1 ) :
i + = 2 ;
continue ;
while (i < n and arr[i] % 2 = = 0 ) :
i + = 2 ;
flips + = 1 ;
i + = 2 ;
i = 1
while i < n :
if (arr[i] % 2 = = 1 ) :
i + = 2 ;
continue ;
while (i < n and arr[i] % 2 = = 0 ) :
i + = 2 ;
flips + = 1 ;
i + = 2 ;
print (flips);
if __name__ = = "__main__" :
arr = [ 2 , 3 , 4 , 3 , 5 , 3 , 2 ];
N = len (arr);
minOperations(arr, N);
|
Javascript
<script>
function minOperations(arr, n)
{
let flips = 0;
for (let i = 0; i < n; i += 2)
{
if (arr[i] % 2 == 1)
{
continue ;
}
while (i < n && arr[i] % 2 == 0)
{
i += 2;
}
flips++;
}
for (let i = 1; i < n; i += 2)
{
if (arr[i] % 2 == 1)
{
continue ;
}
while (i < n && arr[i] % 2 == 0)
{
i += 2;
}
flips++;
}
document.write(flips);
}
let arr = [ 2, 3, 4, 3, 5, 3, 2 ];
let N = arr.length;
minOperations(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
23 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...