给定一个由N个整数组成的数组,任务是找到对(i,j)的对数,以使A [i] ^ A [j]为偶数。
例子:
Input: A[] = { 5, 4, 7, 2, 1}
Output: 4
Since pair of A[] =
( 5, 4 ) = 1( 5, 7 ) = 2( 5, 2 ) = 7( 5, 1 ) = 4
( 4, 7 ) = 3( 4, 2 ) = 6( 4, 1 ) = 5
( 7, 2 ) = 5( 7, 1 ) = 6
( 2, 1 ) = 3
Total XOR even pair = 4
Input: A[] = { 7, 2, 8, 1, 0, 5, 11 }
Output: 9
Since pair of A[] =
( 7, 2 ) = 5( 7, 8 ) = 15( 7, 1 ) = 6( 7, 0 ) = 7( 7, 5 ) = 2( 7, 11 ) = 12
( 2, 8 ) = 10( 2, 1 ) = 3( 2, 0 ) = 2( 2, 5 ) = 7( 2, 11 ) = 9
( 8, 1 ) = 9( 8, 0 ) = 8( 8, 5 ) = 13( 8, 11 ) = 3
( 1, 0 ) = 1( 1, 5 ) = 4( 1, 11 ) = 10
( 0, 5 ) = 5( 0, 11 ) = 11
( 5, 11 ) = 14
天真的方法是检查每个对,并打印偶数对。
下面是上述方法的实现:
C++
// C++ program to count pairs
// with XOR giving a even number
#include
using namespace std;
// Function to count number of even pairs
int findevenPair(int A[], int N)
{
int i, j;
// variable for counting even pairs
int evenPair = 0;
// find all pairs
for (i = 0; i < N; i++) {
for (j = i + 1; j < N; j++) {
// find XOR operation
// check even or even
if ((A[i] ^ A[j]) % 2 == 0)
evenPair++;
}
}
// return number of even pair
return evenPair;
}
// Driver Code
int main()
{
int A[] = { 5, 4, 7, 2, 1 };
int N = sizeof(A) / sizeof(A[0]);
// calling function findevenPair
// and print number of even pair
cout << findevenPair(A, N) << endl;
return 0;
}
Java
// Java program to count pairs
// with XOR giving a even number
import java.io.*;
class GFG
{
// Function to count number of even pairs
static int findevenPair(int []A, int N)
{
int i, j;
// variable for counting even pairs
int evenPair = 0;
// find all pairs
for (i = 0; i < N; i++)
{
for (j = i + 1; j < N; j++)
{
// find XOR operation
// check even or even
if ((A[i] ^ A[j]) % 2 == 0)
evenPair++;
}
}
// return number of even pair
return evenPair;
}
// Driver Code
public static void main (String[] args)
{
int A[] = { 5, 4, 7, 2, 1 };
int N = A.length;
// calling function findevenPair
// and print number of even pair
System.out.println(findevenPair(A, N));
}
}
// This code is contributed by inder_verma..
Python3
# Python3 program to count pairs
# with XOR giving a even number
# Function to count number of even pairs
def findevenPair(A, N):
# variable for counting even pairs
evenPair = 0
# find all pairs
for i in range(0, N):
for j in range(i+1, N):
# find XOR operation
# check even or even
if ((A[i] ^ A[j]) % 2 == 0):
evenPair+=1
# return number of even pair
return evenPair;
# Driver Code
def main():
A = [ 5, 4, 7, 2, 1 ]
N = len(A)
# calling function findevenPair
# and prnumber of even pair
print(findevenPair(A, N))
if __name__ == '__main__':
main()
# This code is contributed by PrinciRaj1992
C#
// C# program to count pairs
// with XOR giving a even number
using System;
class GFG
{
// Function to count number of
// even pairs
static int findevenPair(int []A, int N)
{
int i, j;
// variable for counting even pairs
int evenPair = 0;
// find all pairs
for (i = 0; i < N; i++)
{
for (j = i + 1; j < N; j++)
{
// find XOR operation
// check even or even
if ((A[i] ^ A[j]) % 2 == 0)
evenPair++;
}
}
// return number of even pair
return evenPair;
}
// Driver Code
public static void Main ()
{
int []A = { 5, 4, 7, 2, 1 };
int N = A.Length;
// calling function findevenPair
// and print number of even pair
Console.WriteLine(findevenPair(A, N));
}
}
// This code is contributed
// by inder_verma..
PHP
Javascript
C++
// C++ program to count pairs
// with XOR giving a even number
#include
using namespace std;
// Function to count number of even pairs
int findEvenPair(int A[], int N)
{
int count = 0;
// find all pairs
for (int i = 0; i < N; i++) {
if (A[i] % 2 != 0)
count++;
}
int totalPairs = (N * (N - 1) / 2);
int oddEvenPairs = count * (N - count);
// return number of even pair
return totalPairs - oddEvenPairs;
}
// Driver Code
int main()
{
int a[] = { 5, 4, 7, 2, 1 };
int n = sizeof(a) / sizeof(a[0]);
// calling function findEvenPair
// and print number of even pair
cout << findEvenPair(a, n) << endl;
return 0;
}
Java
// Java program to count pairs
// with XOR giving a even number
import java.io.*;
class GFG {
// Function to count number of even pairs
static int findEvenPair(int A[], int N)
{
int count = 0;
// find all pairs
for (int i = 0; i < N; i++) {
if (A[i] % 2 != 0)
count++;
}
int totalPairs = (N * (N - 1) / 2);
int oddEvenPairs = count * (N - count);
// return number of even pair
return totalPairs - oddEvenPairs;
}
// Driver Code
public static void main (String[] args) {
int a[] = { 5, 4, 7, 2, 1 };
int n = a.length;
// calling function findEvenPair
// and print number of even pair
System.out.println(findEvenPair(a, n));
}
//This code is contributed by akt_mit
}
Python3
# python program to count pairs
# with XOR giving a even number
# Function to count number of even pairs
def findEvenPair(A, N):
count = 0
# find all pairs
for i in range(0,N):
if (A[i] % 2 != 0):
count+=1
totalPairs = (N * (N - 1) / 2)
oddEvenPairs = count * (N - count)
# return number of even pair
return (int)(totalPairs - oddEvenPairs)
# Driver Code
def main():
a = [ 5, 4, 7, 2, 1 ]
n = len(a)
# calling function findEvenPair
# and pr number of even pair
print(findEvenPair(a, n))
if __name__ == '__main__':
main()
# This code is contributed by 29AjayKumar
C#
// C# program to count pairs
// with XOR giving a even number
using System;
public class GFG {
// Function to count number of even pairs
static int findEvenPair(int []A, int N)
{
int count = 0;
// find all pairs
for (int i = 0; i < N; i++) {
if (A[i] % 2 != 0)
count++;
}
int totalPairs = (N * (N - 1) / 2);
int oddEvenPairs = count * (N - count);
// return number of even pair
return totalPairs - oddEvenPairs;
}
// Driver Code
public static void Main() {
int []a = { 5, 4, 7, 2, 1 };
int n = a.Length;
// calling function findEvenPair
// and print number of even pair
Console.Write(findEvenPair(a, n));
}
}
// This code is contributed by 29AjayKumar
PHP
输出:
4
时间复杂度: O(n ^ 2)
一种有效的解决方案是计数与位异或对作为奇数即oddEvenpairs。然后返回totalPairs –奇数偶数对,其中totalPairs =(N *(N-1)/ 2)和oddEvenPairs =数*(N –计数) 。作为,将提供偶按位异或的对是:
Even, Even
Odd, Odd
因此,找到同时具有奇数和偶数元素的对数并从总数中减去。对。
下面是上述方法的实现:
C++
// C++ program to count pairs
// with XOR giving a even number
#include
using namespace std;
// Function to count number of even pairs
int findEvenPair(int A[], int N)
{
int count = 0;
// find all pairs
for (int i = 0; i < N; i++) {
if (A[i] % 2 != 0)
count++;
}
int totalPairs = (N * (N - 1) / 2);
int oddEvenPairs = count * (N - count);
// return number of even pair
return totalPairs - oddEvenPairs;
}
// Driver Code
int main()
{
int a[] = { 5, 4, 7, 2, 1 };
int n = sizeof(a) / sizeof(a[0]);
// calling function findEvenPair
// and print number of even pair
cout << findEvenPair(a, n) << endl;
return 0;
}
Java
// Java program to count pairs
// with XOR giving a even number
import java.io.*;
class GFG {
// Function to count number of even pairs
static int findEvenPair(int A[], int N)
{
int count = 0;
// find all pairs
for (int i = 0; i < N; i++) {
if (A[i] % 2 != 0)
count++;
}
int totalPairs = (N * (N - 1) / 2);
int oddEvenPairs = count * (N - count);
// return number of even pair
return totalPairs - oddEvenPairs;
}
// Driver Code
public static void main (String[] args) {
int a[] = { 5, 4, 7, 2, 1 };
int n = a.length;
// calling function findEvenPair
// and print number of even pair
System.out.println(findEvenPair(a, n));
}
//This code is contributed by akt_mit
}
Python3
# python program to count pairs
# with XOR giving a even number
# Function to count number of even pairs
def findEvenPair(A, N):
count = 0
# find all pairs
for i in range(0,N):
if (A[i] % 2 != 0):
count+=1
totalPairs = (N * (N - 1) / 2)
oddEvenPairs = count * (N - count)
# return number of even pair
return (int)(totalPairs - oddEvenPairs)
# Driver Code
def main():
a = [ 5, 4, 7, 2, 1 ]
n = len(a)
# calling function findEvenPair
# and pr number of even pair
print(findEvenPair(a, n))
if __name__ == '__main__':
main()
# This code is contributed by 29AjayKumar
C#
// C# program to count pairs
// with XOR giving a even number
using System;
public class GFG {
// Function to count number of even pairs
static int findEvenPair(int []A, int N)
{
int count = 0;
// find all pairs
for (int i = 0; i < N; i++) {
if (A[i] % 2 != 0)
count++;
}
int totalPairs = (N * (N - 1) / 2);
int oddEvenPairs = count * (N - count);
// return number of even pair
return totalPairs - oddEvenPairs;
}
// Driver Code
public static void Main() {
int []a = { 5, 4, 7, 2, 1 };
int n = a.Length;
// calling function findEvenPair
// and print number of even pair
Console.Write(findEvenPair(a, n));
}
}
// This code is contributed by 29AjayKumar
的PHP
输出:
4
时间复杂度: O(n)