Saturday 13 August 2016

Destination: Together : Booking.com backend code sprint

Problem : 

John and Zizi are planning their respective autumn vacations. John has exactly  cities he plans to visit, Zizi has exactly  cities she plans to visit, and they want to have exactly  destination cities in common. Having been a couple for over two years without ever vacationing together, they used  to find a romantic final destination that appeals to both of their passions: marvellous L'viv in the Ukraine.
John and Zizi are software engineers who love creating and solving interesting problems. They want to know the number of different sequences in which they can visit their respective vacation destinations such that they visit each city exactly once and both meet up in L'viv as their final destination. Given , and , find and print the number of different ways they can do this.
Sample Input:
3 4 2
Sample Output:
24
Solution : 
Number of different ways is simply equal to factorial(total unique cities -1
Implementation: 
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        
        //Read input
        Scanner sc= new Scanner(System.in);
        int n= sc.nextInt();
        int m= sc.nextInt();
        int c= sc.nextInt();
        //check constraints
        if(n>0&&n<11&&m>0&&m<11){
            
            if(c<=n && c<=m){
                int uniqueCities= n-c+m-c+c; // total unique cities planned to visit

            // now number of ways to visit these cities is uniqueCities-1! 
            long ways=1;
            for(int i=1;i<uniqueCities;i++){
                ways*=i;

            }
            //print result
            System.out.println(ways); 
            }// end inner if
        }// end outer if
       
    }
}

1 comment:

  1. Can you explain how 24 is the solution ? I just need an explanation ....

    ReplyDelete