Count of non co-prime pairs from the range [1, arr[i]] for every array element
Last Updated :
10 Nov, 2021
Given an array arr[] consisting of N integers, the task for every ith element of the array is to find the number of non co-prime pairs from the range [1, arr[i]].
Examples:
Input: N = 2, arr[] = {3, 4}
Output: 2 4
Explanation:
- All non-co-prime pairs from the range [1, 3] are (2, 2) and (3, 3).
- All non-co-prime pairs from the range [1, 4] are (2, 2), (2, 4), (3, 3) and (4, 4).
Input: N = 3, arr[] = {5, 10, 20}
Output: 5 23 82
Naive Approach: The simplest approach to solve the problem is to iterate over every ith array element and then, generate every possible pair from the range [1, arr[i]], and for every pair, check whether it is non-co-prime, i.e. gcd of the pair is greater than 1 or not.
Follow the steps below to solve this problem:
- Iterate over the range [0, N – 1] using a variable, say i, and perform the following steps:
- Initialize variables lastEle as arr[i] and count as 0 to store the last value of the current range and number of co-prime pairs respectively.
- Iterate over every pair from the range [1, arr[i]] using variables x and y and do the following:
- If GCD(x, y) is greater than 1, then the increment count by 1.
- Finally, print the count as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
void countPairs( int * arr, int N)
{
for ( int i = 0; i < N; i++) {
int count = 0;
for ( int x = 1; x <= arr[i]; x++) {
for ( int y = x; y <= arr[i]; y++) {
if (gcd(x, y) > 1)
count++;
}
}
cout << count << " " ;
}
}
int main()
{
int arr[] = { 5, 10, 20 };
int N = 3;
countPairs(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static void countPairs( int [] arr, int N)
{
for ( int i = 0 ; i < N; i++)
{
int count = 0 ;
for ( int x = 1 ; x <= arr[i]; x++)
{
for ( int y = x; y <= arr[i]; y++)
{
if (gcd(x, y) > 1 )
count++;
}
}
System.out.print(count + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 5 , 10 , 20 };
int N = 3 ;
countPairs(arr, N);
}
}
|
Python3
def gcd(a, b):
if b = = 0 :
return a
return gcd(b, a % b)
def countPairs(arr, N):
for i in range ( 0 , N):
count = 0
for x in range ( 1 , arr[i] + 1 ):
for y in range (x, arr[i] + 1 ):
if gcd(x, y) > 1 :
count + = 1
print (count, end = " " )
if __name__ = = '__main__' :
arr = [ 5 , 10 , 20 ]
N = len (arr)
countPairs(arr, N)
|
C#
using System;
class GFG{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void countPairs( int [] arr, int N)
{
for ( int i = 0; i < N; i++)
{
int count = 0;
for ( int x = 1; x <= arr[i]; x++)
{
for ( int y = x; y <= arr[i]; y++)
{
if (gcd(x, y) > 1)
count++;
}
}
Console.Write(count + " " );
}
}
public static void Main(String[] args)
{
int []arr = { 5, 10, 20 };
int N = 3;
countPairs(arr, N);
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function countPairs(arr, N)
{
for ( var i = 0; i < N; i++)
{
var count = 0;
for ( var x = 1; x <= arr[i]; x++)
{
for ( var y = x; y <= arr[i]; y++)
{
if (gcd(x, y) > 1)
count++;
}
}
document.write(count + " " );
}
}
var arr = [ 5, 10, 20 ];
var N = 3;
countPairs(arr, N);
</script>
|
Time Complexity: O(N*M2*log(M)), where M is the maximum element of the array.
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by using Euler’s totient function, and prefix sum array. Follow the steps below to solve the problem:
- Initialize two arrays, say phi[] and ans[] as 0, where the ith element of the array represents the count of integers that is coprime to i and the count of non-coprime pairs formed from the range [1, arr[i]].
- Iterate over the range [1, MAX] using a variable, say i, and assign i to phi[i].
- Iterate over the range [2, MAX] using a variable, say i, and perform the following steps:
- If phi[i] = i, then iterate over the range [i, MAX] using a variable j and perform the following steps:
- Decrement phi[j] / i from phi[j] and then increment j by i.
- Iterate over the range [1, MAX] using a variable, say i, and perform the following steps:
- Update ans[i] to ans[i – 1] + (i – phi[i]).
- Finally, after completing the above steps, print the array ans[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1005;
void preCalculate(vector< int >& phi,
vector< int >& ans)
{
phi[0] = 0;
phi[1] = 1;
for ( int i = 2; i <= MAX; i++)
phi[i] = i;
for ( int i = 2; i <= MAX; i++) {
if (phi[i] == i) {
for ( int j = i; j <= MAX; j += i)
phi[j] -= (phi[j] / i);
}
}
for ( int i = 1; i <= MAX; i++)
ans[i] = ans[i - 1] + (i - phi[i]);
}
void countPairs( int * arr, int N)
{
vector< int > phi(1e5, 0);
vector< int > ans(1e5, 0);
preCalculate(phi, ans);
for ( int i = 0; i < N; ++i) {
cout << ans[arr[i]] << " " ;
}
}
int main()
{
int arr[] = { 5, 10, 20 };
int N = 3;
countPairs(arr, N);
}
|
Java
import java.util.*;
class GFG {
static int MAX = 1005 ;
static void preCalculate( int [] phi,
int [] ans)
{
phi[ 0 ] = 0 ;
phi[ 1 ] = 1 ;
for ( int i = 2 ; i <= MAX; i++)
phi[i] = i;
for ( int i = 2 ; i <= MAX; i++) {
if (phi[i] == i) {
for ( int j = i; j <= MAX; j += i)
phi[j] -= (phi[j] / i);
}
}
for ( int i = 1 ; i <= MAX; i++)
ans[i] = ans[i - 1 ] + (i - phi[i]);
}
static void countPairs( int [] arr, int N)
{
int [] phi = new int [ 100000 ];
Arrays.fill(phi, 0 );
int [] ans = new int [ 100000 ];
Arrays.fill(ans, 0 );
preCalculate(phi, ans);
for ( int i = 0 ; i < N; ++i) {
System.out.print(ans[arr[i]] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 5 , 10 , 20 };
int N = 3 ;
countPairs(arr, N);
}
}
|
Python3
MAX = 1005 ;
def preCalculate(phi,ans):
phi[ 0 ] = 0
phi[ 1 ] = 1
for i in range ( 2 , MAX + 1 ):
phi[i] = i
for i in range ( 2 , MAX + 1 ):
if (phi[i] = = i):
for j in range (i, MAX + 1 , i):
phi[j] - = (phi[j] / / i);
for i in range ( 1 , MAX + 1 ):
ans[i] = ans[i - 1 ] + (i - phi[i]);
def countPairs(arr, N):
phi = [ 0 for i in range ( 100001 )]
ans = [ 0 for i in range ( 100001 )]
preCalculate(phi, ans);
for i in range (N):
print (ans[arr[i]],end = ' ' );
arr = [ 5 , 10 , 20 ]
N = 3 ;
countPairs(arr, N);
|
C#
using System;
class GFG{
static int MAX = 1005;
static void preCalculate( int [] phi,
int [] ans)
{
phi[0] = 0;
phi[1] = 1;
for ( int i = 2; i <= MAX; i++)
phi[i] = i;
for ( int i = 2; i <= MAX; i++) {
if (phi[i] == i) {
for ( int j = i; j <= MAX; j += i)
phi[j] -= (phi[j] / i);
}
}
for ( int i = 1; i <= MAX; i++)
ans[i] = ans[i - 1] + (i - phi[i]);
}
static void countPairs( int [] arr, int N)
{
int [] phi = new int [100000];
Array.Clear(phi, 0, 100000);
int [] ans = new int [100000];
Array.Clear(ans, 0, 100000);
preCalculate(phi, ans);
for ( int i = 0; i < N; ++i) {
Console.Write(ans[arr[i]] + " " );
}
}
public static void Main()
{
int [] arr = { 5, 10, 20 };
int N = 3;
countPairs(arr, N);
}
}
|
Javascript
<script>
const MAX = 1005;
function preCalculate(phi, ans) {
phi[0] = 0;
phi[1] = 1;
for (let i = 2; i <= MAX; i++) phi[i] = i;
for (let i = 2; i <= MAX; i++) {
if (phi[i] == i) {
for (let j = i; j <= MAX; j += i)
phi[j] -= Math.floor(phi[j] / i);
}
}
for (let i = 1; i <= MAX; i++)
ans[i] = ans[i - 1] + (i - phi[i]);
}
function countPairs(arr, N) {
let phi = new Array(1e5).fill(0);
let ans = new Array(1e5).fill(0);
preCalculate(phi, ans);
for (let i = 0; i < N; ++i) {
document.write(ans[arr[i]] + " " );
}
}
let arr = [5, 10, 20];
let N = 3;
countPairs(arr, N);
</script>
|
Time Complexity: O(N+ M*log(N)), where M is the maximum element of the array.
Auxiliary Space: O(M+N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...