Given an array **arr[]** of **N** integers, and a range **L** to **R**, the task is to find the total number of elements in the array from index **L to R** that satisfies the following condition:

where

F(x)is Euler’s Totient Function.

**Examples:**

Input:arr[] = {2, 4, 5, 8}, L = 1, R = 3Output:2Explanation:

Here in the given range, F(2) = 1 and (2 – 1) = 1. Similarly, F(5) = 4 and (5 – 1) = 4.

So the total count of indices which satisfy the condition is 2.

Input:arr[] = {9, 3, 4, 6, 8}, L = 3, R = 5Output:0Explanation :

In the given range there is no element that satisfies the given condition.

So the total count is 0.

**Naive Approach:** The naive approach to solve this problem is to iterate over all the elements of the array and check if the Euler’s Totient value of the current element is one less than itself. If yes then increment the count.**Time Complexity:** O(N * sqrt(N)) **Auxiliary Space:** O(1)

**Efficient Approach:**

Euler’s Totient function F(n) for an input n is the count of numbers in {1, 2, 3, …, n} that are relatively prime to n, i.e., the numbers whose Greatest Common Divisor with n is 1.

- If we observe, we can notice that the above given condition is only satisfied by the Prime numbers.
- So, all we need to do is to compute the total numbers of prime numbers in the given range.
- We will use Sieve of Eratosthenes to compute prime numbers efficiently.
- Also, we will pre-compute the numbers of primes numbers in a count array.

Below is the implementation of the above approach.

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `long` `long` `prime[1000001] = { 0 }; ` ` ` `// Seiev of Erotosthenes method to ` `// compute all primes ` `void` `seiveOfEratosthenes() ` `{ ` ` ` ` ` `for` `(` `int` `i = 2; i < 1000001; ` ` ` `i++) { ` ` ` `prime[i] = 1; ` ` ` `} ` ` ` ` ` `for` `(` `int` `i = 2; i * i < 1000001; ` ` ` `i++) { ` ` ` ` ` `// If current number is ` ` ` `// marked prime then mark ` ` ` `// its multiple as non-prime ` ` ` `if` `(prime[i] == 1) { ` ` ` `for` `(` `int` `j = i * i; ` ` ` `j < 1000001; j += i) { ` ` ` `prime[j] = 0; ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to count the number ` `// of element satisfying the condition ` `void` `CountElements(` `int` `arr[], ` ` ` `int` `n, ` `int` `L, ` ` ` `int` `R) ` `{ ` ` ` `seiveOfEratosthenes(); ` ` ` ` ` `long` `long` `countPrime[n + 1] ` ` ` `= { 0 }; ` ` ` ` ` `// Compute the number of primes ` ` ` `// in count prime array ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` `countPrime[i] = countPrime[i - 1] ` ` ` `+ prime[arr[i - 1]]; ` ` ` `} ` ` ` ` ` `// Print the number of elements ` ` ` `// satisfying the condition ` ` ` `cout << countPrime[R] ` ` ` `- countPrime[L - 1] ` ` ` `<< endl; ` ` ` ` ` `return` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `// Given array ` ` ` `int` `arr[] = { 2, 4, 5, 8 }; ` ` ` ` ` `// Size of the array ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` `int` `L = 1, R = 3; ` ` ` ` ` `// Function Call ` ` ` `CountElements(arr, N, L, R); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `static` `int` `prime[] = ` `new` `int` `[` `1000001` `]; ` ` ` `// Seiev of Erotosthenes method to ` `// compute all primes ` `static` `void` `seiveOfEratosthenes() ` `{ ` ` ` `for` `(` `int` `i = ` `2` `; i < ` `1000001` `; i++) ` ` ` `{ ` ` ` `prime[i] = ` `1` `; ` ` ` `} ` ` ` ` ` `for` `(` `int` `i = ` `2` `; i * i < ` `1000001` `; i++) ` ` ` `{ ` ` ` ` ` `// If current number is ` ` ` `// marked prime then mark ` ` ` `// its multiple as non-prime ` ` ` `if` `(prime[i] == ` `1` `) ` ` ` `{ ` ` ` `for` `(` `int` `j = i * i; ` ` ` `j < ` `1000001` `; j += i) ` ` ` `{ ` ` ` `prime[j] = ` `0` `; ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to count the number ` `// of element satisfying the condition ` `static` `void` `CountElements(` `int` `arr[], ` `int` `n, ` ` ` `int` `L, ` `int` `R) ` `{ ` ` ` `seiveOfEratosthenes(); ` ` ` ` ` `int` `countPrime[] = ` `new` `int` `[n + ` `1` `]; ` ` ` ` ` `// Compute the number of primes ` ` ` `// in count prime array ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `{ ` ` ` `countPrime[i] = countPrime[i - ` `1` `] + ` ` ` `prime[arr[i - ` `1` `]]; ` ` ` `} ` ` ` ` ` `// Print the number of elements ` ` ` `// satisfying the condition ` ` ` `System.out.print(countPrime[R] - ` ` ` `countPrime[L - ` `1` `] + ` `"\n"` `); ` ` ` ` ` `return` `; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Given array ` ` ` `int` `arr[] = { ` `2` `, ` `4` `, ` `5` `, ` `8` `}; ` ` ` ` ` `// Size of the array ` ` ` `int` `N = arr.length; ` ` ` `int` `L = ` `1` `, R = ` `3` `; ` ` ` ` ` `// Function Call ` ` ` `CountElements(arr, N, L, R); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` `prime ` `=` `[` `0` `] ` `*` `(` `1000001` `) ` ` ` `# Seiev of Erotosthenes method to ` `# compute all primes ` `def` `seiveOfEratosthenes(): ` ` ` ` ` `for` `i ` `in` `range` `(` `2` `, ` `1000001` `): ` ` ` `prime[i] ` `=` `1` ` ` ` ` `i ` `=` `2` ` ` `while` `(i ` `*` `i < ` `1000001` `): ` ` ` ` ` `# If current number is ` ` ` `# marked prime then mark ` ` ` `# its multiple as non-prime ` ` ` `if` `(prime[i] ` `=` `=` `1` `): ` ` ` `for` `j ` `in` `range` `(i ` `*` `i, ` `1000001` `, i): ` ` ` `prime[j] ` `=` `0` ` ` ` ` `i ` `+` `=` `1` ` ` `# Function to count the number ` `# of element satisfying the condition ` `def` `CountElements(arr, n, L, R): ` ` ` ` ` `seiveOfEratosthenes() ` ` ` ` ` `countPrime ` `=` `[` `0` `] ` `*` `(n ` `+` `1` `) ` ` ` ` ` `# Compute the number of primes ` ` ` `# in count prime array ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` `countPrime[i] ` `=` `(countPrime[i ` `-` `1` `] ` `+` ` ` `prime[arr[i ` `-` `1` `]]) ` ` ` ` ` `# Print the number of elements ` ` ` `# satisfying the condition ` ` ` `print` `(countPrime[R] ` `-` ` ` `countPrime[L ` `-` `1` `]) ` ` ` ` ` `return` ` ` `# Driver Code ` ` ` `# Given array ` `arr ` `=` `[ ` `2` `, ` `4` `, ` `5` `, ` `8` `] ` ` ` `# Size of the array ` `N ` `=` `len` `(arr) ` `L ` `=` `1` `R ` `=` `3` ` ` `# Function call ` `CountElements(arr, N, L, R) ` ` ` `# This code is contributed by sanjoy_62 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` `class` `GFG{ ` ` ` `static` `int` `[]prime = ` `new` `int` `[1000001]; ` ` ` `// Seiev of Erotosthenes method to ` `// compute all primes ` `static` `void` `seiveOfEratosthenes() ` `{ ` ` ` `for` `(` `int` `i = 2; i < 1000001; i++) ` ` ` `{ ` ` ` `prime[i] = 1; ` ` ` `} ` ` ` ` ` `for` `(` `int` `i = 2; i * i < 1000001; i++) ` ` ` `{ ` ` ` ` ` `// If current number is ` ` ` `// marked prime then mark ` ` ` `// its multiple as non-prime ` ` ` `if` `(prime[i] == 1) ` ` ` `{ ` ` ` `for` `(` `int` `j = i * i; ` ` ` `j < 1000001; j += i) ` ` ` `{ ` ` ` `prime[j] = 0; ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to count the number ` `// of element satisfying the condition ` `static` `void` `CountElements(` `int` `[]arr, ` `int` `n, ` ` ` `int` `L, ` `int` `R) ` `{ ` ` ` `seiveOfEratosthenes(); ` ` ` ` ` `int` `[]countPrime = ` `new` `int` `[n + 1]; ` ` ` ` ` `// Compute the number of primes ` ` ` `// in count prime array ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `{ ` ` ` `countPrime[i] = countPrime[i - 1] + ` ` ` `prime[arr[i - 1]]; ` ` ` `} ` ` ` ` ` `// Print the number of elements ` ` ` `// satisfying the condition ` ` ` `Console.Write(countPrime[R] - ` ` ` `countPrime[L - 1] + ` `"\n"` `); ` ` ` ` ` `return` `; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `// Given array ` ` ` `int` `[]arr = { 2, 4, 5, 8 }; ` ` ` ` ` `// Size of the array ` ` ` `int` `N = arr.Length; ` ` ` `int` `L = 1, R = 3; ` ` ` ` ` `// Function Call ` ` ` `CountElements(arr, N, L, R); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** *O(N * log(logN))* **Auxiliary Space:** *O(N)*

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:

- Count integers in a range which are divisible by their euler totient value
- Count elements less than or equal to a given value in a sorted rotated array
- Longest subarray having count of 1s one more than count of 0s
- Maximum sub-matrix area having count of 1's one more than count of 0's
- First element of every K sets having consecutive elements with exactly K prime factors less than N
- Count all subsequences having product less than K
- Count of subsets having sum of min and max element less than K
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Euler's Totient function for all numbers smaller than or equal to n
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Sum of all array elements less than X and greater than Y for Q queries
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Largest Divisor for each element in an array other than 1 and the number itself
- Print all permutations of a number N greater than itself
- Maximum sum subarray having sum less than or equal to given sum
- Number of subarrays having product less than K
- Number of subarrays having sum less than K
- Maximum sum subarray having sum less than or equal to given sum using Set
- Largest subset having with sum less than equal to sum of respective indices
- Find all possible subarrays having product less than or equal to 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.