
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
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.