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 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 = 13Output: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 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, thenYis modulo inverse ofX.

Therefore, IfYisXitself, then(X × X) % Pmust be1.

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++

`// C++ program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to get the count` `// of elements that satisfy` `// the given condition.` `int` `equvInverse(` `int` `arr[],` ` ` `int` `N, ` `int` `P)` `{` ` ` `// Stores count of elements` ` ` `// that satisfy the condition` ` ` `int` `cntElem = 0;` ` ` `// Traverse the given array.` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `// If square of current` ` ` `// element is equal to 1` ` ` `if` `((arr[i] * arr[i]) % P` ` ` `== 1) {` ` ` `cntElem++;` ` ` `}` ` ` `}` ` ` `return` `cntElem;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `arr[] = { 1, 6, 4, 5 };` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `int` `P = 7;` ` ` `cout << equvInverse(arr, N, P);` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement` `// the above approach` `import` `java.io.*;` `class` `GFG{` `// Function to get the count` `// of elements that satisfy` `// the given condition.` `static` `int` `equvInverse(` `int` `[] arr, ` ` ` `int` `N, ` `int` `P)` `{` ` ` ` ` `// Stores count of elements` ` ` `// that satisfy the condition` ` ` `int` `cntElem = ` `0` `;` ` ` `// Traverse the given array.` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++)` ` ` `{` ` ` ` ` `// If square of current` ` ` `// element is equal to 1` ` ` `if` `((arr[i] * arr[i]) % P == ` `1` `)` ` ` `{` ` ` `cntElem++;` ` ` `}` ` ` `}` ` ` `return` `cntElem;` `}` `// Driver Code` `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));` `}` `}` `// This code is contributed by akhilsaini` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement` `# the above approach` `# Function to get the count` `# of elements that satisfy` `# the given condition.` `def` `equvInverse(arr, N, P):` ` ` ` ` `# Stores count of elements` ` ` `# that satisfy the condition` ` ` `cntElem ` `=` `0` ` ` `# Traverse the given array.` ` ` `for` `i ` `in` `range` `(` `0` `, N):` ` ` ` ` `# If square of current` ` ` `# element is equal to 1` ` ` `if` `((arr[i] ` `*` `arr[i]) ` `%` `P ` `=` `=` `1` `):` ` ` `cntElem ` `=` `cntElem ` `+` `1` ` ` `return` `cntElem` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` ` ` `arr ` `=` `[ ` `1` `, ` `6` `, ` `4` `, ` `5` `]` ` ` `N ` `=` `len` `(arr)` ` ` `P ` `=` `7` ` ` ` ` `print` `(equvInverse(arr, N, P))` `# This code is contributed by akhilsaini` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement` `// the above approach` `using` `System;` `class` `GFG{` `// Function to get the count` `// of elements that satisfy` `// the given condition.` `static` `int` `equvInverse(` `int` `[] arr, ` `int` `N, ` `int` `P)` `{` ` ` ` ` `// Stores count of elements` ` ` `// that satisfy the condition` ` ` `int` `cntElem = 0;` ` ` `// Traverse the given array.` ` ` `for` `(` `int` `i = 0; i < N; i++)` ` ` `{` ` ` ` ` `// If square of current` ` ` `// element is equal to 1` ` ` `if` `((arr[i] * arr[i]) % P == 1)` ` ` `{` ` ` `cntElem++;` ` ` `}` ` ` `}` ` ` `return` `cntElem;` `}` `// Driver Code` `public` `static` `void` `Main()` `{` ` ` `int` `[] arr = { 1, 6, 4, 5 };` ` ` `int` `N = arr.Length;` ` ` `int` `P = 7;` ` ` ` ` `Console.WriteLine(equvInverse(arr, N, P));` `}` `}` `// This code is contributed by akhilsaini` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(N)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Nearest smaller number to N having multiplicative inverse under modulo N equal to that number
- XOR of array elements whose modular inverse with a given number exists
- Modular Exponentiation (Power in Modular Arithmetic)
- Modular multiplicative inverse from 1 to n
- Find the value of P and modular inverse of Q modulo 998244353
- Modular multiplicative inverse
- Count of elements having Euler's Totient value one less than itself
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Sum of array elements after every element x is XORed itself x times
- Find smallest number with given number of digits and sum of digits under given constraints
- Count numbers in a given range whose count of prime factors is a Prime Number
- Find power of power under mod of a prime
- Largest Divisor for each element in an array other than 1 and the number itself
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Number of solutions to Modular Equations
- Longest increasing sub-sequence formed by concatenating array to itself N times
- Print all permutations of a number N greater than itself
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.