Table of Contents
Methods of finding solutions
brute force methods
Efficient method
Output
Explanation of the above code
Conclusion
Home Backend Development C++ Written in C++, find the number of quadruples whose first three terms are arithmetic sequences and the last three terms are geometric sequences.

Written in C++, find the number of quadruples whose first three terms are arithmetic sequences and the last three terms are geometric sequences.

Aug 30, 2023 pm 02:09 PM
arithmetic progression c programming Quantity of Quads geometric sequence

Written in C++, find the number of quadruples whose first three terms are arithmetic sequences and the last three terms are geometric sequences.

In this article we will describe all the possible ways to find quaternions using A.P. for the first 3 terms and G.P. for the last 3 terms. First, we will explain the basic definitions of arithmetic progression (A.P.) and geometric progression (G.P.).

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

Geometric Progression (G.P.) - This is a sequence of numbers where the common ratios (r) are the same, which means So we can multiply 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). As a result, arr[a], arr[b], and arr[c] are in A.P., while arr[d], arr[c], and arr[b] are in G.P. All four-tuples in it should be deterministic. 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.
Copy after login

Methods of finding solutions

Now, we will describe two different methods of finding solutions-

brute force methods

Here is a simple way to solve this problem using four nested loops and then check if the first three elements are in A.P. If yes, then check if the last 3 elements are in G.P. If so, add 1 to the count variable. However, this method is very time-consuming as its time complexity is O(n4).

Efficient method

< p>In this method we first find the count of each array element and then consider these two elements as second and third numbers and run both Nested loop, then the first element will 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;
}
Copy after login

Output

Number of quadruples: 2
Copy after login

Explanation of the above code

In this code, we use combinatorics to do the second and third elements using two nested loops and find the first element using arr[a] – (arr[c] – arr[b]) and the fourth element arr[c] * arr[c] / arr[b]. So, by keeping the second and third elements fixed, the number of quaternions indexed by A and B is the count of the first number * the fourth number. The time complexity of the above code is O(n2).

Conclusion

In this article, we solved the problem of finding quaternions, where the first three terms are in AP and the last three terms are in GP. We discussed using Bruteforce[ O( n4) ] and Efficient method [ O(n2) ] are two ways to solve this problem.

We used C to solve this problem, which can also be solved in various other languages, such as java, python , C or any other programming language.

The above is the detailed content of Written in C++, find the number of quadruples whose first three terms are arithmetic sequences and the last three terms are geometric sequences.. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use C++ to write code to find the Nth non-square number Use C++ to write code to find the Nth non-square number Aug 30, 2023 pm 10:41 PM

We all know numbers that are not the square of any number, such as 2, 3, 5, 7, 8, etc. There are N non-square numbers, and it is impossible to know every number. So, in this article, we will explain everything about squareless or non-square numbers and ways to find the Nth non-square number in C++. Nth non-square number If a number is the square of an integer, then the number is called a perfect square. Some examples of perfect square numbers are -1issquareof14issquareof29issquareof316issquareof425issquareof5 If a number is not the square of any integer, then the number is called non-square. For example, the first 15 non-square numbers are -2,3,5,6,

Inversion algorithm for right rotation of array written in C++ Inversion algorithm for right rotation of array written in C++ Sep 08, 2023 pm 08:17 PM

In this article, we will learn about the reversal algorithm to rotate a given array to the right by k elements, for example −Input:arr[]={4,6,2,6,43,7,3,7},k= 4Output:{43,7,3,7,4,6,2,6}Explanation:Rotatingeachelementofarrayby4-elementtotherightgives{43,7,3,7,4,6,2,6}.Input:arr[]={8 ,5,8,2,1,4,9,3},k=3Output:{4,9,3,8,5,8,2,1} Find the solution

In C programming, find the area of ​​a circle In C programming, find the area of ​​a circle Aug 25, 2023 pm 10:57 PM

A circle is a closed figure. All points on a circle are equidistant from a point inside the circle. The center point is called the center of the circle. The distance from a point to the center of a circle is called the radius. Area is a quantitative representation of the span of dimensions of a closed figure. The area of ​​a circle is the area enclosed within the dimensions of the circle. The formula to calculate the area of ​​a circle, Area=π*r*r To calculate the area, we give the radius of the circle as input, we will use the formula to calculate the area, algorithm STEP1: Takeradiusasinputfromtheuserusingstdinput.STEP2: Calculatetheareaofcircleusing, area=(

How to find an arithmetic sequence in JavaScript How to find an arithmetic sequence in JavaScript Feb 10, 2023 am 09:56 AM

How to find an arithmetic sequence in JavaScript: 1. Create a js sample file; 2. Define a print1 function through "function print1(start, value, endKey) {...}"; 3. Through "for (let i = 0 ; i < endKey; i++) {arr.push(start + (i * value));}" Just implement the calculation logic of the arithmetic sequence.

Find the sum of an arithmetic sequence of staggered signs Find the sum of an arithmetic sequence of staggered signs Sep 16, 2023 pm 05:01 PM

An arithmetic progression (AP) is a sequence of numbers in which the difference between two consecutive terms is the same. The difference is calculated by subtracting the second term from the first term. Let us understand AP with an example sequence, 5,7,9,11,13,15,... The tolerance (d) of this arithmetic series is 2. This means that each subsequent element differs from the previous element by 2. The first item (a) in this sequence is 5. The general formula to find the nth term is a{n}=a+(n-1)(d) In this problem we are given an AP and we need to find the sum of a series of alternating signed squares, the series will be As shown below, a12-a22+a32-a42+a52+... Let us take an example for clearer understanding&amp;

Find the number of unique pairs in an array using C++ Find the number of unique pairs in an array using C++ Sep 07, 2023 am 11:53 AM

We need proper knowledge to create several unique pairs in array syntax of C++. While finding the number of unique pairs, we count all the unique pairs in the given array i.e. all possible pairs can be formed where each pair should be unique. For example -Input:array[]={5,5,9}Output:4Explanation:Thenumberoffalluniquepairsare(5,5),(5,9),(9,5)and(9,9).Input:array[]= {5,4,3,2,2}Output:16 Ways to Find Solution There are two ways to solve this problem, they are −

Write a code using C++ to find the number of subarrays with the same minimum and maximum values Write a code using C++ to find the number of subarrays with the same minimum and maximum values Aug 25, 2023 pm 11:33 PM

In this article, we will use C++ to solve the problem of finding the number of subarrays whose maximum and minimum values ​​are the same. The following is an example of the problem −Input:array={2,3,6,6,2,4,4,4}Output:12Explanation:{2},{3},{6},{6},{2 },{4},{4},{4},{6,6},{4,4},{4,4}and{4,4,4}arethesubarrayswhichcanbeformedwithmaximumandminimumelementsame.Input:array={3,3, 1,5,

Written in C++, find the number of reflexive relations on a set Written in C++, find the number of reflexive relations on a set Aug 26, 2023 pm 08:17 PM

In this article we will explain ways to find reflexive relations on a set. In this problem, we are given a number n, and a set of n natural numbers, and we must determine the number of reflexive relations. Reflexive relation - A relation R is said to be a reflexive relation on the set A if for every 'a' in the set A, (a, a) belongs to the relation R. For example -Input:x=1Output:1Explanation:set={1},reflexiverelationsonA*A:{{1}}Input:x=2Output:4Explanation:set={1,2},reflexiverelationsonA*

See all articles