Count of elements having Euler’s Totient value one less than itself
Last Updated :
18 Jul, 2022
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 = 3
Output: 2
Explanation:
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 = 5
Output: 0
Explanation :
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++
#include <bits/stdc++.h>
using namespace std;
long long prime[1000001] = { 0 };
void seiveOfEratosthenes()
{
for ( int i = 2; i < 1000001; i++)
prime[i] = 1;
for ( int i = 2; i * i < 1000001; i++) {
if (prime[i] == 1)
for ( int j = i * i; j < 1000001; j += i)
prime[j] = 0;
}
}
void CountElements( int arr[], int n, int L, int R)
{
seiveOfEratosthenes();
long long countPrime[n + 1] = { 0 };
for ( int i = 1; i <= n; i++)
countPrime[i] = countPrime[i - 1] + prime[arr[i - 1]];
printf ( "%lld" , (countPrime[R] - countPrime[L - 1]));
return ;
}
int main()
{
int arr[] = { 2, 4, 5, 8 };
int N = sizeof (arr) / sizeof ( int );
int L = 1, R = 3;
CountElements(arr, N, L, R);
return 0;
}
|
C
#include <stdio.h>
#include <string.h>
long long prime[1000001];
void seiveOfEratosthenes()
{
memset (prime, 0, sizeof (prime));
for ( int i = 2; i < 1000001; i++)
prime[i] = 1;
for ( int i = 2; i * i < 1000001; i++) {
if (prime[i] == 1)
for ( int j = i * i; j < 1000001; j += i)
prime[j] = 0;
}
}
void CountElements( int arr[], int n, int L, int R)
{
seiveOfEratosthenes();
long long countPrime[n + 1];
memset (countPrime, 0, sizeof (countPrime));
for ( int i = 1; i <= n; i++)
countPrime[i]
= countPrime[i - 1] + prime[arr[i - 1]];
printf ( "%lld" , (countPrime[R] - countPrime[L - 1]));
return ;
}
int main()
{
int arr[] = { 2, 4, 5, 8 };
int N = sizeof (arr) / sizeof ( int );
int L = 1, R = 3;
CountElements(arr, N, L, R);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int prime[] = new int [ 1000001 ];
static void seiveOfEratosthenes()
{
for ( int i = 2 ; i < 1000001 ; i++)
{
prime[i] = 1 ;
}
for ( int i = 2 ; i * i < 1000001 ; i++)
{
if (prime[i] == 1 )
{
for ( int j = i * i;
j < 1000001 ; j += i)
{
prime[j] = 0 ;
}
}
}
}
static void CountElements( int arr[], int n,
int L, int R)
{
seiveOfEratosthenes();
int countPrime[] = new int [n + 1 ];
for ( int i = 1 ; i <= n; i++)
{
countPrime[i] = countPrime[i - 1 ] +
prime[arr[i - 1 ]];
}
System.out.print(countPrime[R] -
countPrime[L - 1 ] + "\n" );
return ;
}
public static void main(String[] args)
{
int arr[] = { 2 , 4 , 5 , 8 };
int N = arr.length;
int L = 1 , R = 3 ;
CountElements(arr, N, L, R);
}
}
|
Python3
prime = [ 0 ] * ( 1000001 )
def seiveOfEratosthenes():
for i in range ( 2 , 1000001 ):
prime[i] = 1
i = 2
while (i * i < 1000001 ):
if (prime[i] = = 1 ):
for j in range (i * i, 1000001 , i):
prime[j] = 0
i + = 1
def CountElements(arr, n, L, R):
seiveOfEratosthenes()
countPrime = [ 0 ] * (n + 1 )
for i in range ( 1 , n + 1 ):
countPrime[i] = (countPrime[i - 1 ] +
prime[arr[i - 1 ]])
print (countPrime[R] -
countPrime[L - 1 ])
return
arr = [ 2 , 4 , 5 , 8 ]
N = len (arr)
L = 1
R = 3
CountElements(arr, N, L, R)
|
C#
using System;
class GFG{
static int []prime = new int [1000001];
static void seiveOfEratosthenes()
{
for ( int i = 2; i < 1000001; i++)
{
prime[i] = 1;
}
for ( int i = 2; i * i < 1000001; i++)
{
if (prime[i] == 1)
{
for ( int j = i * i;
j < 1000001; j += i)
{
prime[j] = 0;
}
}
}
}
static void CountElements( int []arr, int n,
int L, int R)
{
seiveOfEratosthenes();
int []countPrime = new int [n + 1];
for ( int i = 1; i <= n; i++)
{
countPrime[i] = countPrime[i - 1] +
prime[arr[i - 1]];
}
Console.Write(countPrime[R] -
countPrime[L - 1] + "\n" );
return ;
}
public static void Main(String[] args)
{
int []arr = { 2, 4, 5, 8 };
int N = arr.Length;
int L = 1, R = 3;
CountElements(arr, N, L, R);
}
}
|
Javascript
<script>
let prime = new Uint8Array(1000001);
function seiveOfEratosthenes()
{
for (let i = 2; i < 1000001;
i++) {
prime[i] = 1;
}
for (let i = 2; i * i < 1000001;
i++) {
if (prime[i] == 1) {
for (let j = i * i;
j < 1000001; j += i) {
prime[j] = 0;
}
}
}
}
function CountElements(arr, n, L, R)
{
seiveOfEratosthenes();
countPrime = new Uint8Array(n + 1);
for (let i = 1; i <= n; i++) {
countPrime[i] = countPrime[i - 1]
+ prime[arr[i - 1]];
}
document.write((countPrime[R]
- countPrime[L - 1])
+ "<br>" );
return ;
}
let arr = [ 2, 4, 5, 8 ];
let N = arr.length;
let L = 1, R = 3;
CountElements(arr, N, L, R);
</script>
|
Time Complexity: O(N * log(logN))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...