0% found this document useful (0 votes)
8 views5 pages

Java Code Implementation: Generated by Blackbox Ai

The document outlines a treasure hunt problem where the goal is to maximize the value of the top three treasures collected between two points L and R on a path, while accounting for the distance traveled. It provides a Java implementation using a sliding window approach to evaluate all possible ranges and calculate the maximum score. The solution has a time complexity of O(N^2 log N) and includes example input and output for clarity.

Uploaded by

normieladkahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Java Code Implementation: Generated by Blackbox Ai

The document outlines a treasure hunt problem where the goal is to maximize the value of the top three treasures collected between two points L and R on a path, while accounting for the distance traveled. It provides a Java implementation using a sliding window approach to evaluate all possible ranges and calculate the maximum score. The solution has a time complexity of O(N^2 log N) and includes example input and output for clarity.

Uploaded by

normieladkahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

30/05/2025, 00:55 BLACKBOXAI

User
You're planning a treasure hunt along a path with N treasures, where the i-th treasure is
located i miles from the start of the path. Each treasure i has a value vi. You decide to start
your hunt at L. miles and end it at R miles from the start of the path. During your hunt, you
will collect treasures you come across, including those at Land R miles. However, as you
travel, you get increasingly tired.
To optimize your hunt, you need to determine the best starting and ending points, Land R,
such that there are at least three treasures between these points, and the total value of the
top three treasures you collect minus the distance you travel (R-L) is maximized. Formally,
you want to choose L and R so that vi1+vi2+vi3-(R-L) is as large as possible, where
11,12,13 are the indices of the three highest valued treasures in the range [L,R].
Input Format
The first line contains a single integer N denoting the number of treasures.
The second line contains N integers vi denoting the values of the treasures at i miles from
the start of the path.
Output Format

Print a single integer which is the maximum value vi1+vi2+vi3-(R-L) for some hunting range
(L, R)
Constraints
3SNS3015
1svis10 6

Sample Testcase 1

BLACKBOXAI
To solve the problem of maximizing the value of the top three treasures collected during a
treasure hunt while accounting for the distance traveled, we can follow these steps:

1. Input Reading: Read the number of treasures and their respective values.
2. Sliding Window Approach: Use a sliding window to evaluate all possible ranges [L, R]
where R - L >= 2 (to ensure at least three treasures).
3. Finding Top Three Treasures: For each valid range, find the top three treasure values.
4. Calculate the Score: For each range, calculate the score as the sum of the top three
treasures minus the distance traveled (R - L).
5. Maximize the Score: Keep track of the maximum score encountered during the
evaluation of all ranges.

Java Code Implementation

Here is the Java code that implementsGenerated


the above logic: AI
by BlackBox
https://www.blackbox.ai/chat/lbC4cPD 16/20
30/05/2025, 00:55 BLACKBOXAI

java
import java.util.Arrays;
import java.util.Scanner;

public class TreasureHunt {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Read the number of treasures


int N = scanner.nextInt();
int[] values = new int[N];

// Read the values of the treasures


for (int i = 0; i < N; i++) {
values[i] = scanner.nextInt();
}

// Calculate the maximum score


int maxScore = calculateMaxScore(values, N);

// Print the result


System.out.println(maxScore);

scanner.close();
}

public static int calculateMaxScore(int[] values, int N) {


int maxScore = Integer.MIN_VALUE;

// Iterate over all possible starting points L


for (int L = 0; L <= N - 3; L++) {
// Iterate over all possible ending points R
for (int R = L + 2; R < N; R++) {
// Get the treasures in the range [L, R]
int[] rangeTreasures = Arrays.copyOfRange(values, L, R + 1);

// Sort the treasures to find the top three values


Arrays.sort(rangeTreasures);

// Calculate the score


int score = rangeTreasures[rangeTreasures.length - 1] + // 1st
highest
rangeTreasures[rangeTreasures.length - 2] + //
2nd highest
rangeTreasures[rangeTreasures.length - 3] - //
3rd highest
(R - L);

// Update maxScore if the current score is higher


maxScore = Math.max(maxScore, score);
}
} Generated by BlackBox AI
https://www.blackbox.ai/chat/lbC4cPD 17/20
30/05/2025, 00:55 BLACKBOXAI

return maxScore;
}
}

Explanation of the Code:

1. Input Handling: The program reads the number of treasures and their values.
2. Nested Loops: The outer loop iterates over possible starting points ( L ), and the inner
loop iterates over possible ending points ( R ) such that ( R ) is at least two indices away
from ( L ).
3. Range Extraction: For each valid range, the treasures are extracted and sorted to find
the top three values.
4. Score Calculation: The score is calculated as the sum of the top three treasures minus
the distance traveled.
5. Max Score Tracking: The maximum score is updated whenever a higher score is
found.

Example Usage:

For the input:

5
1 5 3 4 2

The output will be:

10

This indicates the maximum value achievable based on the given constraints.

Complexity:

The time complexity of this solution is ( O(N^2 log N) ) due to the nested loops and
sorting operation for each range. Given the constraints, this is efficient enough.

User
give this code in jpg format

BLACKBOXAI
TreasureHunt.java

Generated by BlackBox AI
https://www.blackbox.ai/chat/lbC4cPD 18/20
30/05/2025, 00:55 BLACKBOXAI

java
import java.util.Arrays;
import java.util.Scanner;

public class TreasureHunt {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Read the number of treasures


int N = scanner.nextInt();
int[] values = new int[N];

// Read the values of the treasures


for (int i = 0; i < N; i++) {
values[i] = scanner.nextInt();
}

// Calculate the maximum score


int maxScore = calculateMaxScore(values, N);

// Print the result


System.out.println(maxScore);

scanner.close();
}

public static int calculateMaxScore(int[] values, int N) {


int maxScore = Integer.MIN_VALUE;

// Iterate over all possible starting points L


for (int L = 0; L <= N - 3; L++) {
// Iterate over all possible ending points R
for (int R = L + 2; R < N; R++) {
// Get the treasures in the range [L, R]
int[] rangeTreasures = Arrays.copyOfRange(values, L, R + 1);

// Sort the treasures to find the top three values


Arrays.sort(rangeTreasures);

// Calculate the score


int score = rangeTreasures[rangeTreasures.length - 1] + // 1st
highest
rangeTreasures[rangeTreasures.length - 2] + //
2nd highest
rangeTreasures[rangeTreasures.length - 3] - //
3rd highest
(R - L);

// Update maxScore if the current score is higher


maxScore = Math.max(maxScore, score);
}
} Generated by BlackBox AI
https://www.blackbox.ai/chat/lbC4cPD 19/20
30/05/2025, 00:55 BLACKBOXAI

return maxScore;
}
}

Generated by BlackBox AI
https://www.blackbox.ai/chat/lbC4cPD 20/20

You might also like