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 = 7
Output: 2
Explanation:
Modular multiplicative inverse of arr[0](=1) under modulo P(= 7) is arr[0](= 1) itself.
Modular multiplicative inverse of arr1](= 6) under modulo P(= 7) is arr[1](= 6) itself.
Therefore, the required output is 2.
Input: arr[] = {1, 3, 8, 12, 12}, P = 13
Output: 3
Naive Approach: The simplest approach is to solve this problem is to traverse the array and print the count of array elements such that modulo multiplicative inverse of the element under modulo P is equal to the element itself.
Time Complexity: O(N * log P)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the following observations:
If X and Y are two numbers such that (X × Y) % P = 1, then Y is modulo inverse of X.
Therefore, If Y is X itself, then (X × X) % P must be 1.
Follow the steps below to solve the problem:
- Initialize a variable, say cntElem to store the count of elements that satisfy the given condition.
- Traverse the given array and check if (arr[i] * arr[i]) % P equal to 1 or not. If found to be true then increment the count of cntElem by 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int equvInverse( int arr[],
int N, int P)
{
int cntElem = 0;
for ( int i = 0; i < N; i++) {
if ((arr[i] * arr[i]) % P
== 1) {
cntElem++;
}
}
return cntElem;
}
int main()
{
int arr[] = { 1, 6, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
int P = 7;
cout << equvInverse(arr, N, P);
}
|
Java
import java.io.*;
class GFG{
static int equvInverse( int [] arr,
int N, int P)
{
int cntElem = 0 ;
for ( int i = 0 ; i < N; i++)
{
if ((arr[i] * arr[i]) % P == 1 )
{
cntElem++;
}
}
return cntElem;
}
public static void main(String[] args)
{
int [] arr = { 1 , 6 , 4 , 5 };
int N = arr.length;
int P = 7 ;
System.out.println(equvInverse(arr, N, P));
}
}
|
Python3
def equvInverse(arr, N, P):
cntElem = 0
for i in range ( 0 , N):
if ((arr[i] * arr[i]) % P = = 1 ):
cntElem = cntElem + 1
return cntElem
if __name__ = = "__main__" :
arr = [ 1 , 6 , 4 , 5 ]
N = len (arr)
P = 7
print (equvInverse(arr, N, P))
|
C#
using System;
class GFG{
static int equvInverse( int [] arr, int N, int P)
{
int cntElem = 0;
for ( int i = 0; i < N; i++)
{
if ((arr[i] * arr[i]) % P == 1)
{
cntElem++;
}
}
return cntElem;
}
public static void Main()
{
int [] arr = { 1, 6, 4, 5 };
int N = arr.Length;
int P = 7;
Console.WriteLine(equvInverse(arr, N, P));
}
}
|
Javascript
<script>
function equvInverse(arr, N, P)
{
let cntElem = 0;
for (let i = 0; i < N; i++) {
if ((arr[i] * arr[i]) % P
== 1) {
cntElem++;
}
}
return cntElem;
}
let arr = [ 1, 6, 4, 5 ];
let N = arr.length;
let P = 7;
document.write(equvInverse(arr, N, P));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
21 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...