# Count of non co-prime pairs from the range [1, arr[i]] for every array element

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 4Explanation: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 82Naive 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 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