XOR of array elements whose modular inverse with a given number exists
Last Updated :
19 Apr, 2021
Given an array arr[] of length N and a positive integer M, the task is to find the Bitwise XOR of all the array elements whose modular inverse with M exists.
Examples:
Input: arr[] = {1, 2, 3}, M = 4
Output: 2
Explanation:
Initialize the value xor with 0:
For element indexed at 0 i.e., 1, its mod inverse with 4 is 1 because (1 * 1) % 4 = 1 i.e., it exists. Therefore, xor = (xor ^ 1) = 1.
For element indexed at 1 i.e., 2, its mod inverse does not exist.
For element indexed at 2 i.e., 3, its mod inverse with 4 is 3 because (3 * 3) % 4 = 1 i.e., it exists. Therefore, xor = (xor ^ 3) = 2.
Hence, xor is 2.
Input: arr[] = {3, 6, 4, 5, 8}, M = 9
Output: 9
Explanation:
Initialize the value xor with 0:
For element indexed at 0 i.e., 3, its mod inverse does not exist.
For element indexed at 1 i.e., 6, its mod inverse does not exist.
For element indexed at 2 i.e., 4, its mod inverse with 9 is 7 because (4 * 7) % 9 = 1 i.e., it exists. Therefore, xor = (xor ^ 4) = 4.
For element indexed at 3 i.e., 5, its mod inverse with 9 is 2 because (5 * 2) % 9 = 1 i.e., it exists. Therefore, xor = (xor ^ 5) = 1.
For element indexed at 4 i.e., 8, its mod inverse with 9 is 8 because (8 * 8) % 9 = 1 i.e., it exists. Therefore, xor = (xor ^ 8) = 9.
Hence, xor is 9.
Naive Approach: The simplest approach is to print the XOR of all the elements of the array for which there exists any j where (1 <= j < M) such that (arr[i] * j) % M = 1 where 0 ? i < N.
Time Complexity: O(N * M)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to use the property that the modular inverse of any number X under mod M exists if and only if the GCD of M and X is 1 i.e., gcd(M, X) is 1. Follow the steps below to solve the problem:
- Initialize a variable xor with 0, to store the xor of all the elements whose modular inverse under M exists.
- Traverse the array over the range [0, N - 1].
- If gcd(M, arr[i]) is 1 then update xor as xor = (xor^arr[i]).
- After traversing, print the value xor as the required result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the gcd of a & b
int gcd(int a, int b)
{
// Base Case
if (a == 0)
return b;
// Recursively calculate GCD
return gcd(b % a, a);
}
// Function to print the Bitwise XOR of
// elements of arr[] if gcd(arr[i], M) is 1
void countInverse(int arr[], int N, int M)
{
// Initialize xor
int XOR = 0;
// Traversing the array
for (int i = 0; i < N; i++) {
// GCD of M and arr[i]
int gcdOfMandelement
= gcd(M, arr[i]);
// If GCD is 1, update xor
if (gcdOfMandelement == 1) {
XOR ^= arr[i];
}
}
// Print xor
cout << XOR << ' ';
}
// Drive Code
int main()
{
// Given array arr[]
int arr[] = { 1, 2, 3 };
// Given number M
int M = 4;
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
countInverse(arr, N, M);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to return the gcd of a & b
static int gcd(int a, int b)
{
// Base Case
if (a == 0)
return b;
// Recursively calculate GCD
return gcd(b % a, a);
}
// Function to print the Bitwise XOR of
// elements of arr[] if gcd(arr[i], M) is 1
static void countInverse(int[] arr, int N, int M)
{
// Initialize xor
int XOR = 0;
// Traversing the array
for(int i = 0; i < N; i++)
{
// GCD of M and arr[i]
int gcdOfMandelement = gcd(M, arr[i]);
// If GCD is 1, update xor
if (gcdOfMandelement == 1)
{
XOR ^= arr[i];
}
}
// Print xor
System.out.println(XOR);
}
// Drive Code
public static void main(String[] args)
{
// Given array arr[]
int[] arr = { 1, 2, 3 };
// Given number M
int M = 4;
// Size of the array
int N = arr.length;
// Function Call
countInverse(arr, N, M);
}
}
// This code is contributed by akhilsaini
Python3
# Python3 program for the above approach
# Function to return the gcd of a & b
def gcd(a, b):
# Base Case
if (a == 0):
return b
# Recursively calculate GCD
return gcd(b % a, a)
# Function to print the Bitwise XOR of
# elements of arr[] if gcd(arr[i], M) is 1
def countInverse(arr, N, M):
# Initialize xor
XOR = 0
# Traversing the array
for i in range(0, N):
# GCD of M and arr[i]
gcdOfMandelement = gcd(M, arr[i])
# If GCD is 1, update xor
if (gcdOfMandelement == 1):
XOR = XOR ^ arr[i]
# Print xor
print(XOR)
# Drive Code
if __name__ == '__main__':
# Given array arr[]
arr = [ 1, 2, 3 ]
# Given number M
M = 4
# Size of the array
N = len(arr)
# Function Call
countInverse(arr, N, M)
# This code is contributed by akhilsaini
C#
// C# program for the above approach
using System;
class GFG{
// Function to return the gcd of a & b
static int gcd(int a, int b)
{
// Base Case
if (a == 0)
return b;
// Recursively calculate GCD
return gcd(b % a, a);
}
// Function to print the Bitwise XOR of
// elements of arr[] if gcd(arr[i], M) is 1
static void countInverse(int[] arr, int N, int M)
{
// Initialize xor
int XOR = 0;
// Traversing the array
for(int i = 0; i < N; i++)
{
// GCD of M and arr[i]
int gcdOfMandelement = gcd(M, arr[i]);
// If GCD is 1, update xor
if (gcdOfMandelement == 1)
{
XOR ^= arr[i];
}
}
// Print xor
Console.WriteLine(XOR);
}
// Drive Code
public static void Main()
{
// Given array arr[]
int[] arr = { 1, 2, 3 };
// Given number M
int M = 4;
// Size of the array
int N = arr.Length;
// Function Call
countInverse(arr, N, M);
}
}
// This code is contributed by akhilsaini
JavaScript
<script>
// Javascript program for the above approach
// Function to return the gcd of a & b
function gcd(a, b)
{
// Base Case
if (a == 0)
return b;
// Recursively calculate GCD
return gcd(b % a, a);
}
// Function to print the Bitwise XOR of
// elements of arr[] if gcd(arr[i], M) is 1
function countInverse(arr, N, M)
{
// Initialize xor
var XOR = 0;
// Traversing the array
for(var i = 0; i < N; i++)
{
// GCD of M and arr[i]
var gcdOfMandelement = gcd(M, arr[i]);
// If GCD is 1, update xor
if (gcdOfMandelement == 1)
{
XOR ^= arr[i];
}
}
// Print xor
document.write(XOR);
}
// Driver Code
// Given array arr[]
var arr = [ 1, 2, 3 ];
// Given number M
var M = 4;
// Size of the array
var N = arr.length;
// Function Call
countInverse(arr, N, M);
// This code is contributed by Kirti
</script>
Time Complexity: O(N*log M)
Auxiliary Space: O(N)
Similar Reads
Count array elements having modular inverse under given prime number P equal to itself Given an array arr[] of size N and a prime number P, the task is to count the elements of the array such that modulo multiplicative inverse of the element under modulo P is equal to the element itself. Examples: Input: arr[] = {1, 6, 4, 5}, P = 7Output: 2Explanation:Modular multiplicative inverse of
5 min read
Find numbers in range [L, R] that are coprime with given Array elements Given an array arr[] consisting of N distinct positive integers and a range [L, R], the task is to find the element in the given range [L, R] that are coprime with all array elements. Examples: Input: L = 3, R = 11, arr[ ] = {4, 7, 9, 6, 13, 21}Output: {5, 11}Explanation:The elements in the range [3
7 min read
Count of elements such that its sum/difference with X also exists in the Array Given an array arr[] and an integer X, the task is to count the elements of the array such that their exist a element arr[i] - X or arr[i] + X in the array.Examples: Input: arr[] = {3, 4, 2, 5}, X = 2 Output: 4 Explanation: In the above-given example, there are 4 such numbers - For Element 3: Possib
9 min read
Find a number X such that XOR of given Array after adding X to each element is 0 Given an array arr[] of odd length N containing positive integers. The task is to find a positive integer X such that, adding X to all the elements of arr[] and then taking XOR of all the elements gives 0. Return -1 if no such X exists. Examples: Input: arr[] = {2, 4, 5}Output: 1Explanation: Followi
6 min read
Split array into minimum number of subarrays having GCD of its first and last element exceeding 1 Given an array arr[] of size N, the task is to split the entire array into a minimum number of subarrays such that for each subarray, the GCD of the first and last element of the subarray is greater than 1. Examples: Input: arr[] = {2, 3, 4, 4, 4, 3} Output: 2 Explanation: Split the given array into
7 min read
Find Kth smallest number among multiples of all elements of array Consider an array arr[] of size N consisting of prime numbers and an integer K. The task is to find the Kth smallest number among the multiples of all elements of the array. Constraints: 1 <= N <= 101 < arr[i] < 301<=K<=1e12 Examples: Input: N = 3, arr[] = {3, 5, 7}, K=5Output: 9Ex
11 min read