- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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

- Related Questions & Answers
- Prime numbers within a range in JavaScript
- Generating desired pairs within a range using JavaScript
- Finding all possible prime pairs that sum upto input number using JavaScript
- Delete array element in given index range [L – R] in C++?
- Checking for co-prime numbers - JavaScript
- Delete array element in given index range [L – R] in C++ Program
- XOR of all the elements in the given range [L, R] in C++
- Counting prime numbers that reduce to 1 within a range using JavaScript
- Counting all possible palindromic subsequence within a string in JavaScript
- Queries to count the number of unordered co-prime pairs from 1 to N in C++
- Python - Split list into all possible tuple pairs
- Finding sum of all numbers within a range in JavaScript
- Python - Find the number of prime numbers within a given range of numbers
- Minimum insertions to make a Co-prime array in C++
- Count pairs formed by distinct element sub-arrays in C++

Advertisements