Make Array sum even using minimum operations
Last Updated :
05 Jan, 2023
Given an arr[] of length N containing positive elements. the task is to make arr[] sum even using minimum operations. In an operation, you can select any element arr[i] for(1 ? i ? N) of arr[] and convert arr[i] into power(arr[i],j), Where j = max(0, (arr[i] / 2) – 1).
Examples:
Input: N = 4, arr[] = {1, 4, 6, 3}
Output: 0
Explanation: The sum of arr[] is: 1 + 4 + 6 + 3 = 14, Which is already even. Therefore, no operations are required to perform.
Input: N = 3, arr[] = {1, 1, 1}
Output: -1
Explanation: It can be verified that performing any number of operations on any element can’t make the sum of arr[] even.
Approach: Implement the idea below to solve the problem:
- It can be observed that by the given operation only 2 can be converted into 1, Which is a change from even parity to odd parity. No other any element can change its parity from even to odd or odd to even by using above given operation.
- This idea gives us concept that if the sum of arr[] is already even, Then 0 operation is required.
- If arr[] sum is odd, Then sum can be converted into even only and only if at least one time 2 is present in the arr[] else sum can’t be made even in any number of operations and return -1.
Follow the below steps to implement the idea:
- Initialize sum = 0, to calculate the sum of all the elements in arr[].
- Maintain a flag that is initially false, when 2 occurs in arr[] change it to true.
- If the sum is already even, return 0.
- If the sum is odd, then:
- If flag = true, return 1, which is the minimum number of operations required.
- If flag = false, return -1, not possible to make the sum even.
Below is the code to implement the approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minOperations( int N, int arr[])
{
long sum = 0;
bool flag = false ;
for ( int i = 0; i < N; i++) {
sum += arr[i];
if (arr[i] == 2) {
flag = true ;
}
}
if (sum % 2 == 0) {
cout << "0" << endl;
}
else if (flag) {
cout << "1" << endl;
}
else {
cout << "-1" << endl;
}
}
int main() {
int N = 4;
int arr[] = { 1, 1, 1, 2 };
minOperations(N, arr);
int N2 = 5;
int arr2[] = { 9, 4, 6, 3, 7 };
minOperations(N2, arr2);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
throws java.lang.Exception
{
int N = 4 ;
int arr[] = { 1 , 1 , 1 , 2 };
minOperations(N, arr);
int N2 = 5 ;
int arr2[] = { 9 , 4 , 6 , 3 , 7 };
minOperations(N2, arr2);
}
static void minOperations( int N, int arr[])
{
long sum = 0 ;
boolean flag = false ;
for ( int i = 0 ; i < N; i++) {
sum += arr[i];
if (arr[i] == 2 ) {
flag = true ;
}
}
if (sum % 2 == 0 ) {
System.out.println( 0 );
}
else if (flag) {
System.out.println( 1 );
}
else {
System.out.println(- 1 );
}
}
}
|
Python3
def min_operations(N, arr):
sum = 0
flag = False
for i in range (N):
sum + = arr[i]
if arr[i] = = 2 :
flag = True
if sum % 2 = = 0 :
print ( 0 )
elif flag:
print ( 1 )
else :
print ( - 1 )
N1 = 4
arr1 = [ 1 , 1 , 1 , 2 ]
min_operations(N1, arr1)
N2 = 5
arr2 = [ 9 , 4 , 6 , 3 , 7 ]
min_operations(N2, arr2)
|
C#
using System;
namespace GFG
{
class Program
{
static void Main( string [] args)
{
int N1 = 4;
int [] arr1 = { 1, 1, 1, 2 };
MinOperations(N1, arr1);
int N2 = 5;
int [] arr2 = { 9, 4, 6, 3, 7 };
MinOperations(N2, arr2);
}
static void MinOperations( int N, int [] arr)
{
long sum = 0;
bool flag = false ;
for ( int i = 0; i < N; i++)
{
sum += arr[i];
if (arr[i] == 2)
{
flag = true ;
}
}
if (sum % 2 == 0)
{
Console.WriteLine(0);
}
else if (flag)
{
Console.WriteLine(1);
}
else
{
Console.WriteLine(-1);
}
}
}
}
|
Javascript
function minOperations(N, arr) {
let sum = 0;
let flag = false ;
for (let i = 0; i < N; i++) {
sum += arr[i];
if (arr[i] === 2) {
flag = true ;
}
}
if (sum % 2 === 0) {
console.log(0);
}
else if (flag) {
console.log(1);
}
else {
console.log(-1);
}
}
let N1 = 4;
let arr1 = [1, 1, 1, 2];
minOperations(N1, arr1);
let N2 = 5;
let arr2 = [9, 4, 6, 3, 7];
minOperations(N2, arr2);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Articles:
Share your thoughts in the comments
Please Login to comment...