- 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

We are given an array of integer elements and the task is to find the sub-sequences from the given array which are having GCD as 1. GCD is the greatest common divisor of two or more integers that divides the given numbers entirely and greatest amongst all.

**Input** − int arr[] = {3, 4, 8, 16}

**Output** − Count of number of sub-sequences with GCD 1 are − 7

**Explanation** −

The sub-sequences that can be formed from the given array with GCD as 1 are (3, 4), (3, 8), (3, 16), (4, 3), (8, 3), (16, 3), (3, 4, 8)

**Input** − int arr[] = {5, 7, 10}

**Output** − Count of number of sub-sequences with GCD 1 are − 3

**Explanation** −

The sub-sequences that can be formed from the given array with GCD as 1 are (5, 7), (7, 10), (5, 7, 10)

Input an array of integer elements of any given size.

Calculate the size of an array and pass the data to the function for further processing

Declare a temporary variable count to store the count of sub-sequences with GCD as 1

Start loop FOR from i to 0 till the size of an array

Inside the loop start another loop FOR from j to 0 till the size of an array

Inside the loop, check IF GCD(arr[i], arr[j]) = TRUE then increment the count by 1

Return count

Print the result.

# include <bits/stdc++.h> using namespace std; int gcd(int a, int b){ if (a == 0) return b; return gcd(b % a, a); } int GCD_1(int arr[],int size){ int count = 0; for(int i=0;i<size;i++){ for(int j=0;j<=size;j++){ if(gcd(arr[i],arr[j])==1){ count++; } } } return count; } int main(){ int arr[] = {3, 4, 8, 16}; int size = sizeof(arr)/sizeof(arr[0]); cout<<"Count of number of sub-sequences with GCD 1 are: "<<GCD_1(arr, size); return 0; }

If we run the above code it will generate the following output −

Count of number of sub-sequences with GCD 1 are: 7

- Related Questions & Answers
- Find sum of sum of all sub-sequences in C++
- Count pairs of natural numbers with GCD equal to given number in C++
- Count number of subsets of a set with GCD equal to a given number in C++
- Count all sub-sequences having product <= K – Recursive approach in C++
- Count numbers in a range having GCD of powers of prime factors equal to 1 in C++
- Print all longest common sub-sequences in lexicographical order in C++
- Count total number of digits from 1 to N in C++
- Maximum sub-matrix area having count of 1’s one more than count of 0’s in C++
- Count subarrays with equal number of 1’s and 0’s in C++
- Maximum sub-matrix area having count of 1's one more than count of 0’s in C++ program
- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*.. in C++
- Count number of binary strings without consecutive 1's in C
- Count pairs of parentheses sequences such that parentheses are balanced in C++
- Write a program in C++ to count the Number of substrings that starts with ‘1’ and ends with ‘1’
- Count even length binary sequences with same sum of first and second half bits in C++

Advertisements