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

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : akhilsaini