Question: BNY Mellon, Online Assessments (OA) 15 Mar, 2026 | The Puzzle of Harmony | Hiking Cost | Complete Solutions & Tips | GCD Optimization & Array Pathing | Mar 15th Campus Assessment
0
Entering edit mode

Question 1: The Puzzle of Harmony

Problem Statement: In a mystical world of balance and order, you are faced with an intricate challenge involving N magical creatures. Each creature carries a specific energy level, represented in the array E. To restore harmony to the realm, you must pair them wisely according to their energy compatibility.

The energy cost of creating harmony between two creatures is determined by the greatest common divisor (GCD) of their energy levels. But there's a twist; you can only choose creatures from the beginning, middle or end of the array to form pairs. Your task is to find and return an integer value denoting the minimum total energy required to harmonize the realm.

Note: The length of N is always even, so always take the second middle magical creature.

Input Specification:

  • input1: An integer value N, denoting the number of magical creatures
  • input2: An integer array E, denoting the energy level of each creature respectively

 


Question 2: Hiking Cost

Problem Statement: John, an avid hiker, wants to go on a hiking trip across a series of trails with varying difficulty levels. The difficulty levels of the trails are given in an array A of size N. John's objective is to start at the first trail, hike through all the trails, and then return to the starting trail.

The cost of the trip is calculated as follows:

  • The cost of shifting from one trail to another is determined by the absolute difference in difficulty levels between the current trail and the trail John shifts to.
  • The overall cost of the hiking experience is defined by the maximum cost incurred towards all the shifts John makes during his journey in both directions.

Your task is to help John find and return an integer value representing the minimum cost among the highest costs of all the shifts he makes during the hiking trip.

 

ADD COMMENTlink 17 days ago Rohit • 40
0
Entering edit mode

Problem 1 Solution:

The Puzzle of Harmony

Topics Involved / Prerequisites

  • Greatest Common Divisor (GCD)
  • Array Indexing
  • Optimization (Min-Max)

Overview

In this "mystical" scenario, we are limited to three specific candidates for pairing: the creature at the beginning, the second middle, and the end of the array. The cost of a pair is the GCD of their energy levels. To minimize the total energy required to "harmonize" (create a pair), we calculate the GCD for all three possible combinations of these candidates and pick the smallest result.

Approach

  1. Identify Indices:
    • Beginning: index = 0
    • Second Middle: Since N is even, the middle two indices are \frac{N}{2}-1 and \frac{N}{2}. The "second" middle is index = \frac{N}{2}.
    • End: index = N - 1
  2. Calculate GCDs: Use the built-in __gcd function (or a manual Euclidean algorithm) to find:
    • g_1 = {gcd}(E[0], E[N/2])
    • g_2 = {gcd}(E[0], E[N-1])
    • g_3 = {gcd}(E[N/2], E[N-1])
  3. Find Minimum: Return the minimum value among g_1, g_2, and g_3.

Code Implementation (C++)

#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>

using namespace std;

// Euclidean algorithm for GCD
int findGCD(int a, int b) {
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

int getMinHarmony(int n, vector<int>& e) {
    if (n < 2) return 0;

    // Identify the three candidate energy levels
    int start = e[0];
    int middle = e[n / 2]; // Second middle as per problem note
    int end = e[n - 1];

    // Calculate GCD for all 3 possible pairings
    int gcd1 = findGCD(start, middle);
    int gcd2 = findGCD(start, end);
    int gcd3 = findGCD(middle, end);

    // Return the minimum GCD found
    return min({gcd1, gcd2, gcd3});
}

Time and Space Complexity

  • Time Complexity: O(\log(\min(E_i))) — The time taken to calculate the GCD of two numbers.

Space Complexity: O(1) — No additional data structures are required.

ADD COMMENTlink 4 days ago admin 1.9k
0
Entering edit mode

Problem 2 Solution

Question 2: Hiking Cost

Topics Involved / Prerequisites

  • Sorting
  • Greedy Algorithms
  • Minimax Problems

Overview

John needs to visit all trails and return to the start while minimizing the "highest shift cost" (the maximum jump between two difficulty levels). This is a round-trip bottleneck problem. To minimize the maximum gap, the best strategy is to visit trails in a way that avoids large differences. By sorting the trails, we can move from A_0 to A_{N-1} visiting every other trail, and then return visiting the ones we skipped. This effectively limits the maximum jump.

Approach

  1. Sort the Array: Arrange the difficulty levels A in ascending order.
  2. Analyze the Path: To minimize the maximum jump in a round trip, John should visit A_0 to A_2 to A_4 dots End} dots to A_3 to A_1 to A_0.
  3. Calculate Gaps: The maximum jump in this circular route will be the largest of:
    • The jump from A_0 to A_1 (start of the sequence).
    • The jump from A_{N-2} to A_{N-1} (end of the sequence).
    • Any jump between A_i and A_{i+2} (skipping one trail in the sorted list).
  4. Result: The answer is the maximum value among these calculated gaps.

Time and Space Complexity

  • Time Complexity: O(N log N) — Dominated by the sorting of the difficulty levels.
  • Space Complexity: O(1) — Sorting is done in-place or uses O(log N) stack space.

Code Implementation (C++)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int getMinMaxHikingCost(int n, vector<int>& a) {
    if (n < 2) return 0;

    // Sort difficulty levels to find the most efficient path
    sort(a.begin(), a.end());

    // Initialize max_jump with the unavoidable jumps at the start and end
    int max_jump = max(a[1] - a[0], a[n - 1] - a[n - 2]);

    // Check all "skip-one" jumps (Ai to Ai+2)
    // This represents the jumps made on the way there and the way back
    for (int i = 0; i <= n - 3; ++i) {
        max_jump = max(max_jump, a[i + 2] - a[i]);
    }

    return max_jump;
}

 

ADD COMMENTlink 4 days ago admin 1.9k

Login before adding your answer.

Similar Posts
Loading Similar Posts