Skip to content

Instantly share code, notes, and snippets.

@naveenwashere
Created June 14, 2015 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save naveenwashere/5a5117e4326165d4524f to your computer and use it in GitHub Desktop.
Save naveenwashere/5a5117e4326165d4524f to your computer and use it in GitHub Desktop.
Fiberlink online coding challenge - Program | Railway ticket counter and revenue problem
import java.util.Arrays;
import java.util.Scanner;
public class RailwayTickets {
int numOfTickets, ticketsToSell;
int[] ticketsPerBooth;
public RailwayTickets(int numOfTickets, int ticketsToSell, int[] ticketsPerBooth)
{
this.numOfTickets = numOfTickets;
this.ticketsToSell = ticketsToSell;
this.ticketsPerBooth = ticketsPerBooth;
}
public int findMaxRevenue()
{
int[] perBooth = this.ticketsPerBooth;
Arrays.sort(perBooth);
int i = perBooth.length-1;
int revenue = 0;
while(i >= 0 && this.ticketsToSell != 0)
{
int nxtBooth = 0;
int currBooth = perBooth[i];
if(i == 0)
{
nxtBooth = 0;
}
else
{
nxtBooth = perBooth[i-1];
}
int diff = currBooth - nxtBooth;
while(diff != 0 && this.ticketsToSell != 0)
{
revenue += currBooth;
currBooth--;
diff--;
this.ticketsToSell--;
}
i--;
}
return revenue;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int numOfBooths = sc.nextInt();
int ticketsToSell = sc.nextInt();
int perBooth[] = new int[numOfBooths];
for(int i = 0; i < numOfBooths; i++)
{
perBooth[i] = sc.nextInt();
}
RailwayTickets rt = new RailwayTickets(numOfBooths, ticketsToSell, perBooth);
System.out.println("Max Revenue = " + rt.findMaxRevenue());
}
}
@naveenwashere
Copy link
Author

There are "n" ticket windows in the railway station. i'th window has Ai tickets available. Price of a ticket is equal to the number of tickets remaining in that window at that time. When "m" tickets have been sold, what's the maximum amount of money the railway station can earn?
Ex. n=2, m=4
in 2 window available tickets are : 2 , 5
2nd window sold 4 tickets so 5+4+3+2=14.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment