HackerRank : Week of Code 26 – Music on the Street

By | December 1, 2016
Music on the Street

There is a big music festival happening on Main street in Bytetown! We can consider this street to be along an infinite number line where, at every point on the line, some genre of music playing. More precisely, there are points, , which are borders between different genres of music. So, at every point from to , the first genre of music is playing (say, Techno). At every point from to , the second genre of music is playing (say, Disco). This continues until the last genre of music (say, House), which is playing from to . All coordinates are given in miles.

Conor wants to visit this event. He plans to walk exactly miles at a constant speed of mile per hour in the positive direction. For each genre of music he passes, he wants to listen to it for a minimum of hours (to determine whether he likes it or not) and a a maximum of hours (so he will not get bored).

Given integers denoting the respective border points for each music genre and the values of , , and , find and print an integer denoting the optimal location for Conor to start his walk through the festival such that all of his above requirements are satisfied.

Input Format

The first line contains a single integer, , denoting the number of border points.
The second line contains distinct space-separated integers describing the respective values of .
The third line contains three space-separated integers describing the respective values of , , and .


  • , all are pairwise different and given in increasing order.
  • It’s guaranteed that at least one solution exists.

Output Format

Print a single integer denoting the possible start point for Conor’s walk. If there are several solutions, print any one of them.

Sample Input 0

1 3
7 2 3

Sample Output 0


Explanation 0

If Conor starts at point , he will hear music in segment for hours, segment for hours, and segment for his last hours. Similarly, he could start walking at and still satisfy all of his conditions.



import java.util.Scanner;

public class MusicOnTheStreet {
	public static void main(String[] args) {
		Scanner scan= new Scanner(System.in);
		int n= scan.nextInt();
		int a[]= new int[n];
		for(int i=0;i<=n;i++)
		int m=scan.nextInt();
		int hmin=scan.nextInt();
		int hmax=scan.nextInt();
		int lengthpatch=a[n-1]-a[0];
		int newm= m=lengthpatch;