Created
June 14, 2015 15:22
-
-
Save naveenwashere/5a5117e4326165d4524f to your computer and use it in GitHub Desktop.
Fiberlink online coding challenge - Program | Railway ticket counter and revenue problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.