Anshul, Usama And Punishment - A

Anshul and Usama are fond of games and devote most of their time playing them. On one hand Anshul likes to play outdoor games like football, cricket, etc whereas on the other hand Usama likes to play mobile games like Clash of Clans, Mini Militia, etc. One day their teacher decided to punish them. As punishment, their teacher sent them to a building and locked all the exit points of that building. After locking the exit points, their teacher informed them from outside that the only way to come out of the building was by solving a problem.


The problem he gave was that they will be given some initial terms of a series and they have to use the series and tell him the maximum value of (|Ai + Aj|+|A- Aj|) where Ai and Aj are some ith and jth term of the series with the condition 0 <= i,j < n. Also, 'i' can not be equal to 'j'.

The initial terms of the series are 20, 30, 40, 90, 180, 270,...

Example:

Input:  n = 5
Output: 180

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int maximumValue(int n)
{
    int arr[20];
    arr[0] = 20;
    arr[1] = 30;
    for (int i = 2i < ni += 2)
        arr[i] = arr[i - 2] * 2;
    for (int i = 3i < ni += 2)
        arr[i] = arr[i - 2] * 3;
    int max1 = INT_MIN;
    for (int i = 0i < n - 1i++)
    {
        for (int j = i + 1j < nj++)
        {
            int ans = abs(arr[i] + arr[j]) +
 abs(arr[i] - arr[j]);
            max1 = max(ansmax1);
        }
    }
    return max1;
}
int main()
{

    int n = 5;

    cout << maximumValue(n<< "\n";

    return 0;
}


No comments:

Post a Comment