- Related Questions & Answers
- Remove the child node of a specific element in JavaScript?
- Super Prime in c programming
- Sibling of a list element in JavaScript?
- Insert a specified element in a specified position in JavaScript?
- Value of the class attribute node of an element in JavaScript?
- An element inside another element in JavaScript?
- Tag names of body element's children in JavaScript?
- Count distinct elements in an array in Python
- Retrieve element from local storage in JavaScript?
- Java program to verify whether a given element exists in an array.
- All-Pairs Shortest Paths
- How to find all pairs of elements in Java array whose sum is equal to a given number?
- Write a Java program to find the first array element whose value is repeated an integer array?
- What happens if try to access an element with an index greater than the size of the array in Java?
- How can I generate random number in a given range in Android?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Here we will see how to count number of co-prime pairs from the range, where a number will not appear more than a single pair.

Before discussing the logic, let us see what are the co-prime numbers? The co-prime numbers are those numbers which has only one positive integer divisor, that is 1. In other words, we can say the GCD of these two numbers is 1.

Here we are providing the lower and upper limit. If the lower and upper limits are 1 and 6, then there are three pairs. These are (1, 2), (3, 4) and (5, 6)

The approach for solving this problem is like that: If the numbers are consecutive, they are always co-prime. So the count will be (R – L + 1)/2. If (R – L + 1) is odd then there will be one number left, that will not be places into any pair, if this is even, then all will make pair

Begin return (R – L + 1)/2 End

#include <iostream> using namespace std; int countCoPrimePairs(int L, int R) { return (R - L + 1)/2; } main() { int l = 1, r = 6; cout << "Number of co-prime pairs: " << countCoPrimePairs(l, r); }

Number of co-prime pairs: 3

Advertisements