import java.util.HashMap;
import java.util.Map;
public class MaxIntersectionCount {
public static int maxIntersectionCount(int[] y)
{
// Multiply each element in y by 2 to create spaces
// between numbers. This is done to avoid dealing
// with floating point numbers when calculating
// slopes.
for (int i = 0; i < y.length; i++) {
y[i] *= 2;
}
// Create a map to store the count of each
// y-coordinate.
Map<Integer, Integer> ys = new HashMap<>();
// Initialize the previous slope to 0.
int prev_dy = 0;
// Add the first y-coordinate to the map with a
// count of 1.
ys.put(y[0], 1);
// Add the next y-coordinate to the map with a count
// of -1. This is done to create a "valley" between
// the first two points.
ys.put(y[0] + 1, -1);
// Iterate over the remaining y-coordinates.
for (int i = 1; i < y.length; i++) {
// Calculate the slope between the current and
// previous y-coordinates.
int dy = y[i] < y[i - 1] ? -1 : 1;
// If the slope has changed and the previous
// slope was not 0, then we have encountered a
// valley or a hill.
if (prev_dy != dy && prev_dy != 0) {
// If the slope is positive, then we have
// encountered a valley.
if (dy > 0) {
// Increment the count of the
// y-coordinate at the bottom of the
// valley.
ys.put(y[i - 1] + 1,
ys.getOrDefault(y[i - 1] + 1, 0)
+ 1);
// Decrement the count of the
// y-coordinate at the top of the
// valley.
ys.put(y[i] + 1,
ys.getOrDefault(y[i] + 1, 0)
- 1);
}
// Otherwise, we have encountered a hill.
else {
// Decrement the count of the
// y-coordinate at the bottom of the
// hill.
ys.put(y[i - 1],
ys.getOrDefault(y[i - 1], 0)
- 1);
// Increment the count of the
// y-coordinate at the top of the hill.
ys.put(y[i],
ys.getOrDefault(y[i], 0) + 1);
}
}
// Otherwise, we have encountered a straight
// line.
else {
// If the slope is negative, then we are
// going down.
if (dy < 0) {
// Decrement the count of the
// y-coordinate at the top of the line.
ys.put(y[i - 1],
ys.getOrDefault(y[i - 1], 0)
- 1);
// Increment the count of the
// y-coordinate at the bottom of the
// line.
ys.put(y[i],
ys.getOrDefault(y[i], 0) + 1);
}
// Otherwise, we are going up.
else {
// Increment the count of the
// y-coordinate at the top of the line.
ys.put(y[i - 1] + 1,
ys.getOrDefault(y[i - 1] + 1, 0)
+ 1);
// Decrement the count of the
// y-coordinate at the bottom of the
// line.
ys.put(y[i] + 1,
ys.getOrDefault(y[i] + 1, 0)
- 1);
}
}
// Update the previous slope.
prev_dy = dy;
}
// Initialize the answer to -1.
int answer = -1;
// Initialize the current count to 0.
int curr = 0;
// Iterate over the map.
for (Map.Entry<Integer, Integer> entry :
ys.entrySet()) {
// Add the count of the current y-coordinate to
// the current count.
curr += entry.getValue();
// Update the answer to the maximum of the
// current answer and the current count.
answer = Math.max(answer, curr);
}
// Return the answer.
return answer;
}
public static void main(String[] args)
{
int[] y = { 1, 2, 1, 2, 1, 3, 2 };
System.out.println(maxIntersectionCount(y));
}
}
// This code is contributed by shivamgupta0987654321