Given an array A[] of length N along with an integer X, the task is to make the parity of all the elements in A[] the same by using either of the given functions on two distinct indexed elements C and D, the minimum number of times.
- Operation 1: (C | D) ^ X
- Operation 2: (C ^ D) | X
Note: It is not allowed to use both functions.
Examples:
Input: N = 3, X = 2, A[] = {2, 4, 4}
Output: 0
Explanations: All the elements have the same even parity already. Therefore, the minimum number of operations required is 0.Input: N = 6, X = 5, A[] = {3, 1, 4, 2, 1, 6}
Output: 2
Explanations: It will be optimal to use convert odds into evens using function 1((C |D)^X). The operations are performed as:
- First operation: Choose C = A2 = 1, D = A5 = 1
- (C | D)^X = (1 | 1)^5 = 4. Now put 4 in the place of A2 and A5. Then updated A[] = {3, 4, 4, 2, 4, 6}
- Second operation: Choose C = A1 = 3 and D = A6 = 6
- (C | D)^X = (3 | 6)^5 = 2. Now put 2 in place of A1 and A6. Then updated A[] = {2, 4, 4, 2, 4, 2}
Now all the elements have even parity, the Minimum number of operations is required = 2. It should be noted that it is also possible to convert all into odds using function 2 in 2 operations but not possible to make parity the same in less than 2 operations in any possible way.
Approach: Implement the idea below to solve the problem:
The problem is based on Bitwise concept and can be solved by using some observations. For more clarification see the Concept of approach section.
Concept of approach:
There are two functions given by which we have to make parity of elements same.
Formally, We have to see all the possible cases.
- Making all odd from function 1 ((C | D) ^ X)
- Making all even from function 1 ((C | D) ^ X)
- Making all odd from function 2 ((C ^ D) | X)
- Making all even from function 2 ((C ^ D) | X)
Let us take all the above cases one by one and create approach. We will understand approach in terms of LSB(Least significant bit), Which is 1 and 0 for odd and even respectively.
- Making all odd from function 1 (( C | D ) ^ X):
- Making all even from function 1 (( C | D ) ^ X):
- Making all odd from function 2 (( C ^ D ) | X):
- Making all even from function 2 (( C ^ D ) | X):
Steps were taken to solve the problem:
- Create variables let’s say A, B, C, and D.
- Initialize the above four variables as follows (Discussed approach in the Concept of approach section):
- A = Min. operations to convert odds into evens using function (( C | D ) ^ X)
- B = Min. operations to convert evens into odds using function (( C | D ) ^ X)
- C = Min. operations to convert odds into evens using function (( C ^ D ) | X)
- D = Min. operations to convert evens into odds using function (( C ^ D ) | X)
- Output the value of Min(A, B, C, D).
Below is the code to implement the approach:
#include <bits/stdc++.h> using namespace std;
// Function to return minimum operation by ((C|D)^X) to // convert evens into odds int oddByFunction1( int odd, int even, int N, int X)
{ if (odd == N)
return 0;
else if (even == N)
return (X % 2 == 1
? (even % 2 == 0 ? even / 2 : INT_MAX)
: INT_MAX);
else
return (X % 2 == 1
? (even % 2 == 0 ? even / 2 : INT_MAX)
: even);
} // Function to return minimum operation by ((C|D)^X) to // convert odds into evens int evenByFunction1( int odd, int even, int N, int X)
{ if (even == N)
return 0;
else if (odd == N)
return (X % 2 == 1 ? (odd % 2 == 0 ? odd / 2
: (odd / 2) + 1)
: INT_MAX);
else
return (X % 2 == 1 ? (odd % 2 == 0 ? odd / 2
: (odd / 2) + 1)
: INT_MAX);
} // Function to return minimum operation by ((C^D)|X) to // convert odds into evens int evenByFunction2( int odd, int even, int N, int X)
{ if (even == N)
return 0;
else if (odd == N)
return (X % 2 == 1
? INT_MAX
: (odd % 2 == 0 ? odd / 2 : INT_MAX));
else
return (X % 2 == 1
? INT_MAX
: (odd % 2 == 0 ? odd / 2 : INT_MAX));
} // Function to return minimum operation by ((C^D)|X) to // convert evens into odds int oddByFunction2( int odd, int even, int N, int X)
{ if (odd == N)
return 0;
else if (even == N)
return (X % 2 == 1
? (even % 2 == 0 ? even / 2
: (even / 2) + 1)
: INT_MAX);
else
return (X % 2 == 1
? (even % 2 == 0 ? even / 2
: (even / 2) + 1)
: even);
} // Driver function int main()
{ // Inputs
int A[] = { 3, 4, 5, 2, 1 };
int N = sizeof (A) / sizeof (A[0]);
int X = 5;
// Variables for storing count of odd and even elements
int odd = 0;
int even = 0;
// Loop for traversing A[] and initializing count of odd
// and even
for ( int i = 0; i < N; i++) {
if ((A[i] & 1) == 1)
odd++;
else
even++;
}
// Variables for holding minimum operations for all
// possible ways from both functions
int a = oddByFunction1(odd, even, N, X);
int b = evenByFunction1(odd, even, N, X);
int c = evenByFunction2(odd, even, N, X);
int d = oddByFunction2(odd, even, N, X);
// Condition if conversion is not
// possible by any function
if (a == INT_MAX && b == INT_MAX && c == INT_MAX
&& d == INT_MAX)
cout << ( "Not Possible" );
// Printing minimum operations from
// all possible cases
else
cout << (min(a, min(b, min(c, d))));
} |
// Java code to implement the approach import java.util.*;
public class GFG {
// Driver Function
public static void main(String[] args)
{
// Inputs
int A[] = { 3 , 4 , 5 , 2 , 1 };
int N = A.length;
int X = 5 ;
// Variables for storing count of
// odd and even elements
int odd = 0 ;
int even = 0 ;
// Loop for traversing A[] and
// initializing count of
// odd and even
for ( int i = 0 ; i < N; i++) {
if ((A[i] & 1 ) == 1 )
odd++;
else
even++;
}
// Variables for holding minimum
// operations for all possibles
// ways from both functions
int a = OddByFunction1(odd, even, N, X);
int b = EvenByFunction1(odd, even, N, X);
int c = EvenByFunction2(odd, even, N, X);
int d = OddByFunction2(odd, even, N, X);
// Condition if conversion is not
// possible by any function
if (a == Integer.MAX_VALUE && b == Integer.MAX_VALUE
&& c == Integer.MAX_VALUE
&& d == Integer.MAX_VALUE)
System.out.println( "Not Possible" );
// Printing minimum operations from
// all possible cases
else
System.out.print(
Math.min(a, Math.min(b, Math.min(c, d))));
}
// Method for returning minimum
// operation By ((C|D)^X) to convert
// evens into odds
static int OddByFunction1( int odd, int even, int N,
int X)
{
if (odd == N)
return 0 ;
else if (even == N)
return (X % 2 == 1 ? (even % 2 == 0
? even / 2
: Integer.MAX_VALUE)
: Integer.MAX_VALUE);
else
return (X % 2 == 1 ? (even % 2 == 0
? even / 2
: Integer.MAX_VALUE)
: even);
}
// Method for returning minimum
// operation By ((C|D)^X) to convert
// odds into evens
static int EvenByFunction1( int odd, int even, int N,
int X)
{
if (even == N)
return 0 ;
else if (odd == N)
return (X % 2 == 1
? (odd % 2 == 0 ? (odd / 2 )
: +(odd / 2 ) + 1 )
: Integer.MAX_VALUE);
else
return (X % 2 == 1
? (odd % 2 == 0 ? (odd / 2 )
: +(odd / 2 ) + 1 )
: Integer.MAX_VALUE);
}
// Method for returning minimum
// operation By ((C^D)|X) to convert
// odds into evens
static int EvenByFunction2( int odd, int even, int N,
int X)
{
if (even == N)
return 0 ;
else if (odd == N)
return (X % 2 == 1 ? Integer.MAX_VALUE
: (odd % 2 == 0
? odd / 2
: Integer.MAX_VALUE));
else
return (X % 2 == 1 ? Integer.MAX_VALUE
: (odd % 2 == 0
? odd / 2
: Integer.MAX_VALUE));
}
// Method for returning minimum
// operation By ((C^D)|X) to convert
// evens into odds
static int OddByFunction2( int odd, int even, int N,
int X)
{
if (odd == N)
return 0 ;
else if (even == N)
return (X % 2 == 1
? (even % 2 == 0 ? (even / 2 )
: (even / 2 ) + 1 )
: Integer.MAX_VALUE);
else
return (X % 2 == 1
? (even % 2 == 0 ? (even / 2 )
: (even / 2 ) + 1 )
: even);
}
} |
# Python3 code to implement the approach. import sys
# Function to return minimum operation by ((C|D)^X) to # convert evens into odds def oddByFunction1(odd, even, N, X):
if odd = = N:
return 0
elif even = = N:
return (even / / 2 ) if (X % 2 = = 1 and even % 2 = = 0 ) else sys.maxsize
else :
return (even / / 2 ) if (X % 2 = = 1 and even % 2 = = 0 ) else even
# Function to return minimum operation by ((C|D)^X) to # convert odds into evens def evenByFunction1(odd, even, N, X):
if even = = N:
return 0
elif odd = = N:
return (odd / / 2 ) if (X % 2 = = 1 and odd % 2 = = 0 ) else ((odd / / 2 ) + 1 ) if (X % 2 = = 1 and odd % 2 = = 1 ) else sys.maxsize
else :
return (odd / / 2 ) if (X % 2 = = 1 and odd % 2 = = 0 ) else ((odd / / 2 ) + 1 ) if (X % 2 = = 1 and odd % 2 = = 1 ) else sys.maxsize
# Function to return minimum operation by ((C^D)|X) to # convert odds into evens def evenByFunction2(odd, even, N, X):
if even = = N:
return 0
elif odd = = N:
return odd / / 2 if X % 2 = = 0 and odd % 2 = = 0 else sys.maxsize
else :
return odd / / 2 if X % 2 = = 0 and odd % 2 = = 0 else sys.maxsize
# Function to return minimum operation by ((C^D)|X) to # convert evens into odds def oddByFunction2(odd, even, N, X):
if odd = = N:
return 0
elif even = = N:
return ((even / / 2 ) + 1 ) if X % 2 = = 1 and even % 2 = = 1 else sys.maxsize
else :
return ((even / / 2 ) + 1 ) if X % 2 = = 1 and even % 2 = = 1 else even
# Driver function def GFG():
# Inputs
A = [ 3 , 4 , 5 , 2 , 1 ]
N = len (A)
X = 5
# Variables for storing count of odd and even elements
odd = 0
even = 0
# Loop for traversing A[] and initializing count of odd and even
for i in range (N):
if A[i] & 1 = = 1 :
odd + = 1
else :
even + = 1
# Variables for holding minimum operations for all possible ways from both functions
a = oddByFunction1(odd, even, N, X)
b = evenByFunction1(odd, even, N, X)
c = evenByFunction2(odd, even, N, X)
d = oddByFunction2(odd, even, N, X)
# Condition if conversion is not possible by any function
if a = = sys.maxsize and b = = sys.maxsize and c = = sys.maxsize and d = = sys.maxsize:
print ( "Not Possible" )
# Printing minimum operations from
# all possible cases
else :
print ( min (a, min (b, min (c, d))))
# Driver Code if __name__ = = "__main__" :
GFG()
|
using System;
public class Program {
// Function to return minimum operation by ((C|D)^X) to
// convert evens into odds
public static int oddByFunction1( int odd, int even,
int N, int X)
{
if (odd == N)
return 0;
else if (even == N)
return (X % 2 == 1
? (even % 2 == 0 ? even / 2
: int .MaxValue)
: int .MaxValue);
else
return (X % 2 == 1
? (even % 2 == 0 ? even / 2
: int .MaxValue)
: even);
}
// Function to return minimum operation by ((C|D)^X) to
// convert odds into evens
public static int evenByFunction1( int odd, int even,
int N, int X)
{
if (even == N)
return 0;
else if (odd == N)
return (X % 2 == 1
? (odd % 2 == 0 ? odd / 2
: (odd / 2) + 1)
: int .MaxValue);
else
return (X % 2 == 1
? (odd % 2 == 0 ? odd / 2
: (odd / 2) + 1)
: int .MaxValue);
}
// Function to return minimum operation by ((C^D)|X) to
// convert odds into evens
public static int evenByFunction2( int odd, int even,
int N, int X)
{
if (even == N)
return 0;
else if (odd == N)
return (X % 2 == 1
? int .MaxValue
: (odd % 2 == 0 ? odd / 2
: int .MaxValue));
else
return (X % 2 == 1
? int .MaxValue
: (odd % 2 == 0 ? odd / 2
: int .MaxValue));
}
// Function to return minimum operation by ((C^D)|X) to
// convert evens into odds
public static int oddByFunction2( int odd, int even,
int N, int X)
{
if (odd == N)
return 0;
else if (even == N)
return (X % 2 == 1
? (even % 2 == 0 ? even / 2
: (even / 2) + 1)
: int .MaxValue);
else
return (X % 2 == 1
? (even % 2 == 0 ? even / 2
: (even / 2) + 1)
: even);
}
// Driver function
public static void Main()
{
// Inputs
int [] A = { 3, 4, 5, 2, 1 };
int N = A.Length;
int X = 5;
// Variables for storing count of odd and even
// elements
int odd = 0;
int even = 0;
// Loop for traversing A[] and initializing count of
// odd and even
for ( int i = 0; i < N; i++) {
if ((A[i] & 1) == 1)
odd++;
else
even++;
}
// Variables for holding minimum operations for all
// possible ways from both functions
int a = oddByFunction1(odd, even, N, X);
int b = evenByFunction1(odd, even, N, X);
int c = evenByFunction2(odd, even, N, X);
int d = oddByFunction2(odd, even, N, X);
// Condition if conversion is not
// possible by any function
if (a == int .MaxValue
&& b == int .MaxValue&& c == int .MaxValue&& d
== int .MaxValue)
Console.WriteLine( "Not Possible" );
// Printing minimum operations from
// all possible cases
else
Console.WriteLine(
Math.Min(a, Math.Min(b, Math.Min(c, d))));
}
} // This code is contributed by rudra1807raj |
<script> // JavaScript code to implement the approach let A = [3, 4, 5, 2, 1]; let N = A.length; let X = 5; // Variables for storing count of odd and even elements let odd = 0; let even = 0; // Loop for traversing A[] and initializing count of odd and even for (let i = 0; i < N; i++) {
if ((A[i] & 1) === 1) {
odd++;
} else {
even++;
}
} // Variables for holding minimum operations for all possibles // ways from both functions let a = oddByFunction1(odd, even, N, X); let b = evenByFunction1(odd, even, N, X); let c = evenByFunction2(odd, even, N, X); let d = oddByFunction2(odd, even, N, X); // condition if conversion is not possible by any Function if (a === Number.MAX_SAFE_INTEGER && b === Number.MAX_SAFE_INTEGER
&& c === Number.MAX_SAFE_INTEGER && d === Number.MAX_SAFE_INTEGER) { document.write( 'Not Possible' );
} // Printing minimum operations from all possible cases else {
document.write(Math.min(a, Math.min(b, Math.min(c, d))));
} // Function to return minimum operation By ((C|D)^X) to // convert evens into odds function oddByFunction1(odd, even, N, X) {
if (odd === N) {
return 0;
} else if (even === N) {
return (X % 2 === 1 ? (even % 2 === 0 ? even / 2 : Number.MAX_SAFE_INTEGER) : Number.MAX_SAFE_INTEGER);
} else {
return (X % 2 === 1 ? (even % 2 === 0 ? even / 2 : Number.MAX_SAFE_INTEGER) : even);
}
} // Function to return minimum operation By ((C|D)^X) to // convert odds into evens function evenByFunction1(odd, even, N, X) {
if (even === N) {
return 0;
} else if (odd === N) {
return (X % 2 === 1 ? (odd % 2 === 0 ? odd / 2 : Math.floor(odd / 2) + 1) : Number.MAX_SAFE_INTEGER);
} else {
return (X % 2 === 1 ? (odd % 2 === 0 ? odd / 2 : Math.floor(odd / 2) + 1) : Number.MAX_SAFE_INTEGER);
}
} // Function to return minimum operation By ((C^D)|X) to // convert odds into evens function evenByFunction2(odd, even, N, X) {
if (even === N) {
return 0;
} else if (odd === N) {
return (X % 2 === 1 ? Number.MAX_SAFE_INTEGER : (odd % 2 === 0 ? odd / 2 : Number.MAX_SAFE_INTEGER));
} else {
return (X % 2 === 1 ? Number.MAX_SAFE_INTEGER : (odd % 2 === 0 ? odd / 2 : Number.MAX_SAFE_INTEGER));
}
} // Function to return minimum operation By ((C^D)|X) to // convert evens into odds function oddByFunction2(odd, even, N, X) {
if (odd === N) {
return 0;
} else if (even === N) {
return (X % 2 === 1 ? (even % 2 === 0 ? even / 2 : Math.floor(even / 2) + 1) : Number.MAX_SAFE_INTEGER);
} else {
return (X % 2 === 1 ? (even % 2 === 0 ? even / 2 : Math.floor(even / 2) + 1) : even);
}
} // This code is contributed by lokesh. </script> |
1
Time Complexity: O(N), The time complexity of the given code depends on the size of the input array “A”. The loop for traversing the array has a time complexity of O(N), where N is the size of the array. The time complexity of all the functions is O(1). Therefore, the overall time complexity of the code is O(N).
Auxiliary Space: O(1), The space complexity of the given code is O(1) because the amount of memory used by the program does not depend on the size of the input array or any other variable related to the input size. The program only uses a fixed amount of memory to store the variables and constants used in the code.