PANDORA-Editorial

PROBLEM LINK:

Contest

Setter: sayan_kashyapi

Tester: debrc

Editorialist: priyansh2802

DIFFICULTY:

Simple

PREREQUISITES:

Basic observations

PROBLEM:

Pandora has a box. She wants to open it but the catch is, there is a lock of four distinct numbers. The decryption of the lock is such a way that, the four numbers follow a progression either arithmetic progression or geometric progression. Pandora has already guessed the first three numbers correctly. The three successive numbers of the sequence are A, B, C. The problem is that she forgot the sequence was on arithmetic progression or on geometric progression. Help her to determine the type of the progression (arithmetic or geometric) and hence the next successive member.

You worked hard to guess the 44th number. So it’s time to reveal the Treasure.

EXPLANATION

There are two possibilities either the sequence is an Arithematic progression or a Geometric progression therefore if it’s not Arithmetic progression the sequence has to be Geometric progression

  • So what we do is just check the condition for Arithmetic progression which is 2*b==a+c
    if this condition is true that means that the sequence is an Arithmetic progression which means we just need to print the common difference + the last element in the progression

  • If the progression is Geometric progression we just need to print the common ratio (i.e b/a) multiplied by the last element in the progression

TIME COMPLEXITY

Time complexity is O(1).

SOLUTIONS:

Setter's Solution
C++
#include<iostream>
#include<bits/stdc++.h>
#define int long long int
using namespace std;
void solve();
int32_t main(void)
{
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
}
void solve()
{
    int a,b,c;
    cin>>a>>b>>c;
    if(2*b==c+a)
    {
        cout<<c+(b-a)<<endl;
    }
    else
    {
        int diff=b/a;
        cout<<diff*c<<endl;
    }
}
1 Like