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++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
void countInverse( int arr[], int N, int M)
{
int XOR = 0;
for ( int i = 0; i < N; i++) {
int gcdOfMandelement
= gcd(M, arr[i]);
if (gcdOfMandelement == 1) {
XOR ^= arr[i];
}
}
cout << XOR << ' ' ;
}
int main()
{
int arr[] = { 1, 2, 3 };
int M = 4;
int N = sizeof (arr) / sizeof (arr[0]);
countInverse(arr, N, M);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static void countInverse( int [] arr, int N, int M)
{
int XOR = 0 ;
for ( int i = 0 ; i < N; i++)
{
int gcdOfMandelement = gcd(M, arr[i]);
if (gcdOfMandelement == 1 )
{
XOR ^= arr[i];
}
}
System.out.println(XOR);
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 };
int M = 4 ;
int N = arr.length;
countInverse(arr, N, M);
}
}
|
Python3
def gcd(a, b):
if (a = = 0 ):
return b
return gcd(b % a, a)
def countInverse(arr, N, M):
XOR = 0
for i in range ( 0 , N):
gcdOfMandelement = gcd(M, arr[i])
if (gcdOfMandelement = = 1 ):
XOR = XOR ^ arr[i]
print (XOR)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 ]
M = 4
N = len (arr)
countInverse(arr, N, M)
|
C#
using System;
class GFG{
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static void countInverse( int [] arr, int N, int M)
{
int XOR = 0;
for ( int i = 0; i < N; i++)
{
int gcdOfMandelement = gcd(M, arr[i]);
if (gcdOfMandelement == 1)
{
XOR ^= arr[i];
}
}
Console.WriteLine(XOR);
}
public static void Main()
{
int [] arr = { 1, 2, 3 };
int M = 4;
int N = arr.Length;
countInverse(arr, N, M);
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function countInverse(arr, N, M)
{
var XOR = 0;
for ( var i = 0; i < N; i++)
{
var gcdOfMandelement = gcd(M, arr[i]);
if (gcdOfMandelement == 1)
{
XOR ^= arr[i];
}
}
document.write(XOR);
}
var arr = [ 1, 2, 3 ];
var M = 4;
var N = arr.length;
countInverse(arr, N, M);
</script>
|
Time Complexity: O(N*log M)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...