Skip to content

Instantly share code, notes, and snippets.

@youssef3wi
Created August 28, 2025 18:46
Show Gist options
  • Select an option

  • Save youssef3wi/530c9c71bf120bd23e0d1d8dbbd998ae to your computer and use it in GitHub Desktop.

Select an option

Save youssef3wi/530c9c71bf120bd23e0d1d8dbbd998ae to your computer and use it in GitHub Desktop.
CountPeaks
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Your company builds radioactivity sensors.
* You are responsible for their verification process.
* You know that when a sensor is failing, the value it outputs have big variations on short time periods.
* <p>
* The input parameter {@code values}, a list of decimal numbers,
* contains the radioactivity measured by the sensor every second.
* The unit is arbitrary.
* <p>
* You want to count the quantity of "peaks" in these values, which would help you determine if the sensor fails.
* <ul>
* <li>When a value is at least 5 units higher than its two neighbors, it's a "top peak".</li>
* <li>When a value is at least 5 units lower than its two neighbors, it's a "bottom peak".</li>
* <li>You must return an integer: the total number of top peaks and bottom peaks.</li>
* </ul>
* <p>
* The first and last elements of the list {@code values} can never be peaks.
* <p>
* {@code values} is always defined (it is never null, None, etc.). It contains between 0 and 20 values.
* <p>
* The radioactivity values are between 0 and 100.
* <p>
* <h2>Example</h2>
* With these values :
* <p>
* {@code 8; 10.7; 17.1; 11.2; 13.5; 9.9; 14.9; 9.4; 3.1; 12.7}
* <ul>
* <li>The value at index 2 is 17.1. The value just before is 10.7 and the value just
* after is 11.2. The value at index 2 si more than 5 units higher than its two neighbors.
* So, it's a "top peak"</li>
* <li>When the difference is exactly 5, it can validate a peak.
* For example,
* the value at index 6 is 14.9. The value before is 9.9 and the one after is 9.4. It is another top peak.</li>
* <li>The value at index 9 is 3.1, it's neighbors are 9.4 and 12.7. So, the value at index 9 is a "bottom peak".</li>
* </ul>
* <p>
* With this example, your code must return {@code 3}.
*/
public class CountPeaks {
/**
* @param values The radioactivity values measured by the sensor.
* @return The total number of top peaks and bottom peaks found in the radioactivity values.
*/
public static int countPeaks(List<Double> values) {
int count = 0;
for (int idx = 1; idx < values.size() - 1; idx++) {
double current = values.get(idx);
double previous = values.get(idx - 1);
double next = values.get(idx + 1);
// arbitrary
if (current - previous >= 5 && current - next >= 5) {
count++;
} else if (previous - current >= 5 && next - current >= 5) {
count++;
}
}
return count;
}
public static void main(String[] args) {
double[] values = new double[]{8, 10.7, 17.1, 11.2, 13.5, 9.9, 14.9, 9.4, 3.1, 12.7};
System.out.println("Using example: 3 = " + countPeaks(Arrays.stream(values).collect(ArrayList::new, ArrayList::add, ArrayList::addAll)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment