Meeting of Old Friends

Today an outstanding event is going to happen in the forest  hedgehog Filya will come to his old fried Sonya!

Sonya is an owl and she sleeps during the day and stay awake from minute l1 to minute r1 inclusive. Also, during the minute k she prinks and is unavailable for Filya.Filya works a lot and he plans to visit Sonya from minute l2 to minute r2 inclusive.
Calculate the number of minutes they will be able to spend together.

Input Format
You will be given a function with five integers  l1, r1, l2, r2 and k, providing the segments of time for Sonya and Filya and the moment of time when Sonya prinks as arguments. 


Program:

public class MinutesStayTogether {

    public static void main(String args[] ) throws Exception {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter l1, r1, l2 , r2 and k :");
        int l1 = sc.nextInt();
        int r1 = sc.nextInt();
        int l2 = sc.nextInt();
        int r2 = sc.nextInt();
        int k = sc.nextInt();
        System.out.print("Number of Mins Stay together : " + numberOfMinsStayTogether(l1, r1, l2, r2, k));
   }

   public static int numberOfMinsStayTogether(int l1, int r1, int l2, int r2, int k){
       int count = 0;
       if( r1 > l2 ){
           count = (r1 - l2 ) + 1;
           
           if(l2 < k && k < r1){
               count = count - 1;
           }
       }
       return count;
   }
}

Output 1 : 

Enter l1, r1, l2 , r2 and k : 1  10   9   20   1                                                                     
Number of Mins Stay together : 2                                                                                      


Output 2 : 

Enter l1, r1, l2 , r2 and k : 1   100    50    200   75                                                             
Number of Mins Stay together : 50                                                                                    

No comments:

Post a Comment