- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

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

# Find the Number of quadruples where the first three terms are in AP and last three terms are in GP using C++

In this article, we will describe every possible approach to find the number of quadruples in which the first 3 terms are in A.P., and the last 3 are in G.P. First, we will explain the basic definition of arithmetic progression(A.P.) and geometric progression (G.P.).

**Arithmetic progression(A.P.)** − It is a sequence of numbers in which the common difference (d) is the same or constant that means a difference of two consecutive numbers is constant. For example: 1,3,5,7,9 | d = 2

**Geometric Progression(G.P.)** − It is a sequence of numbers in which the common ratios (r) are the same that means we can find each term by multiplying the previous number with the fixed number. For example: 3, 6, 12, 24,.... | r = 2

In this problem, we need to determine how many index quadruples(a, b, c, d) there are from an array arr[ ] of N integers. As a result, arr[a], arr[b], and arr[c] are in A.P., and arr[d], arr[c] and arr[b] are in G.P. where all the quadruples should be definite. So here is the example −

Input : arr[ ] = { 9, 6, 4, 2, 1, 2 } Output : 2 Explanation: Elements in the quadruples are at { 3, 2, 1, 0 } and { 5, 2, 1, 0 } indexes where quadruples are { 2, 4, 6, 9 } for both positions. Input : arr[ ] = { 2, 6, 1, 4, 2 } Output : 2 Explanation: Elements in the quadruples are at { 1, 3, 0, 2 } and { 1, 3, 4, 2 } indexes where quadruples are { 6, 4, 2, 1 } for both positions.

## Approach to Find the Solution

Now, we will describe two different approaches to find the solution −

## Brute Force Approach

It is a simple approach to solve this problem using four nested loops, then check whether the first three elements are in A.P. If yes, then check that the last 3 elements are in G.P or not. If yes, then increment the count variable by 1. However, this approach is time-consuming as its **time complexity is O(n4)**.

## Efficient Approach

In this approach, we first find the count of every array element, then considering both elements to be second and third number and run two nested loops, then the first element would be **arr[b] – (arr[c] – arr[b])** and the fourth element will be **arr[c] * arr[c] / arr[b]**.

## Example

#include <bits/stdc++.h> using namespace std; int main (){ unordered_map < int, int >map; int arr[] = { 2, 6, 1, 4, 2 }; int size = sizeof (arr) / sizeof (arr[0]); // Processing every elent and increasing the count for (int a = 0; a < size; a++) map[arr[a]]++; int count = 0; // Running two nested loops for second & third element for (int b = 0; b < size; b++){ for (int c = 0; c < size; c++){ if (b == c) continue; // Decreasing the count map[arr[b]]--; map[arr[c]]--; // Finding the first element using common difference int first = arr[b] - (arr[c] - arr[b]); // Finding the fourth element using GP int fourth = (arr[c] * arr[c]) / arr[b]; if ((arr[c] * arr[c]) % arr[b] == 0){ // Increment count if not equal if (arr[b] != arr[c]) count += map[first] * map[fourth]; else count += map[first] * (map[fourth] - 1); } map[arr[b]]++; map[arr[c]]++; } } cout <<"Number of quadruples: " << count; return 0; }

## Output

Number of quadruples: 2

## Explanation of the Above Code

In this code we are using Combinatorics and using two nested loops for second and third element and finding first element with **arr[a] – (arr[c] – arr[b])** and the fourth element with **arr[c] * arr[c] / arr[b]**. Therefore the number of the quadruples by A and B indexes is the count of the first number * fourth number, by keeping the second and third element fixed. Here the **time complexity** of the above code is **O(n2)**.

## Conclusion

In this article, we solve the problem of finding the Number of quadruples where the first three terms are in AP, and the last three terms are in GP, and we discussed two approaches to solve this using Bruteforce[ O(n4) ] and Efficient approach [ O(n2) ].

We solved this problem using C++, and this can solve this problem in various other languages like java, python, C, or any other programming language.

- Related Articles
- An AP consists of 37 terms. The sum of the three middle most terms is 225 and the sum of the last three is 429 . Find the AP.
- The sum of the 4th and 8th terms of an AP is 24 and the sum of the 6th and 10th terms is 44. Find the first three terms of the AP.
- The sum of three terms of an A.P. is 21 and the product of the first and the third terms exceeds the second term by 6, find three terms.
- The first and the last terms of an AP are 7 and 49 respectively. If sum of all its terms is 420, find its common difference.
- The first and the last terms of an AP are 5 and 45 respectively. If the sum of all its terms is 400, Find its common difference.
- The sum of three terms which are in A.P is $33$, if the product of the $1st$ and $3rd$ terms exceeds the $2nd$ term by $29$, find AP.
- In an AP of 50 terms, the sum of first 10 terms is 210 and the sum of its last 15 terms is 2565. Find the A.P.
- Find Pth term of a GP if Mth and Nth terms are given in C++
- Find the sum of first 51 terms of an AP whose second and third terms are 14 and 18 respectively.
- An A.P. consists of $37$ terms. The sum of the three middle most terms is $225$ and the sum of the last three is $429$. Find the A.P.
- The sum of the first five terms of an AP and the sum of the first seven terms of the same AP is 167. If the sum of the first ten terms of this AP is 235 , find the sum of its first twenty terms.
- The first and the last terms of an AP are 17 and 350 respectively. If the common difference is 9, how many terms are there and what is their sum?
- The first term of an AP is ( -5 ) and the last term is 45 . If the sum of the terms of the AP is 120 , then find the number of terms and the common difference.
- If sum of first 6 terms of an AP is 36 and that of the first 16 terms is 256 , find the sum of first 10 terms.
- The sum of first 7 terms of an AP is 49 and that of 17 terms is 289. Find the sum of first n terms .