Given an array **arr[]** and a positive integer **P** where **P** is **prime** and non of the elements of array are divisible by **P**. Find sum of all the elements of the array raised to the power **P – 1** i.e. **arr[0] ^{P – 1} + arr[1]^{P – 1} + … + arr[n – 1]^{P – 1}** and print the result modulo

**P**.

**Examples:**

Input:arr[] = {2, 5}, P = 3

Output:2

2^{2}+ 5^{2}= 29 and 29 % 3 = 2

Input:arr[] = {5, 6, 8}, P = 7

Output:3

**Approach:** This problem is a direct application of Fermats’s Little Theorem, **a ^{(P-1)} = 1 (mod p)** where

**a**is not divisible by

**P**. Since, non of the elements of array

**arr[]**are divisible by

**P**, each element

**arr[i]**will give the value

**1**with the given operation.

Therefore, our answer will be

**1 + 1 + … (upto n(size of array)) = n**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <iostream> ` `#include <vector> ` ` ` `using` `namespace` `std; ` ` ` `// Function to return the required sum ` `int` `getSum(vector<` `int` `> arr, ` `int` `p) ` `{ ` ` ` `return` `arr.size(); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<` `int` `> arr = { 5, 6, 8 }; ` ` ` `int` `p = 7; ` ` ` `cout << getSum(arr, p) << endl; ` ` ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `public` `class` `GFG { ` ` ` ` ` `// Function to return the required sum ` ` ` `public` `static` `int` `getSum(` `int` `arr[], ` `int` `p) ` ` ` `{ ` ` ` `return` `arr.length; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `arr[] = { ` `5` `, ` `6` `, ` `8` `}; ` ` ` `int` `p = ` `7` `; ` ` ` `System.out.print(getSum(arr, p)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `# Function to return the required sum ` `def` `getSum(arr, p) : ` ` ` ` ` `return` `len` `(arr) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[` `5` `, ` `6` `, ` `8` `] ` ` ` `p ` `=` `7` ` ` `print` `(getSum(arr, p)) ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` ` ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` ` ` `// Function to return the required sum ` ` ` `public` `static` `int` `getSum(` `int` `[]arr, ` `int` `p) ` ` ` `{ ` ` ` `return` `arr.Length; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main (){ ` ` ` `int` `[]arr = { 5, 6, 8 }; ` ` ` `int` `p = 7; ` ` ` `Console.WriteLine(getSum(arr, p)); ` ` ` `} ` ` ` `//This Code is contributed by akt_mit ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the required sum ` `function` `getSum(` `$arr` `, ` `$p` `) ` `{ ` ` ` `return` `count` `(` `$arr` `); ` `} ` ` ` `// Driver code ` `$arr` `= ` `array` `( 5, 6, 8 ); ` `$p` `= 7; ` `echo` `(getSum(` `$arr` `, ` `$p` `)); ` ` ` `// This code is contributed ` `// by Sach_Code ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

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:

- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Find the sum of power of bit count raised to the power B
- Sum of absolute difference of all pairs raised to power K
- Check if a number can be expressed as x^y (x raised to power y)
- Print last k digits of a^b (a raised to power b)
- Find multiple of x closest to or a ^ b (a raised to power b)
- Find unit digit of x raised to power y
- K-th digit in 'a' raised to power 'b'
- Find value of y mod (2 raised to power x)
- Number of digits in 2 raised to power n
- GCD of a number raised to some power and another number
- Minimum removals in a number to be divisible by 10 power raised to K
- Find last five digits of a given five digit number raised to power five
- Last digit of a number raised to last digit of N factorial
- Check if each element of the given array is the product of exactly K prime numbers
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Sum of largest prime factor of each number less than equal to n
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Check if a prime number can be expressed as sum of two Prime Numbers

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.