Duration | Competitive Programming Questions with Answers in Java

Competitive Programming Questions with Answers in Java

Duration

Problem

Rahul is a very busy persion he dont wan't to waste his time . He keeps account of duration of each and every work. Now he don't even get time to calculate duration of works, So your job is to count the durations for each work and give it to rahul.
Input
First line will be given by N number of works
Next N line will be given SH,SM,EH and EM each separated by space(SH=starting hr, SM=starting min, EH=ending hr, EM=ending min)

Output
N lines with duration HH MM(hours and minutes separated by space)

Sample Input
2
1 44 2 14
2 42 8 23

Sample Output
0 30
5 41

Solution

import java.util.*;

class TestClass {
    public static void main(String args[] ) throws Exception {
        int i, n;
        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        int sh[] = new int[n];
        int sm[] = new int[n];
        int eh[] = new int[n];
        int em[] = new int[n];
        int h[] = new int[n];
        int m[] = new int[n];
        for(i=0; i<n; i++)
        {
            sh[i] = s.nextInt();
            sm[i] = s.nextInt();
            eh[i] = s.nextInt();
            em[i] = s.nextInt();
        }
        for(i=0; i<n; i++)
        {
            h[i] = eh[i] - sh[i];
            m[i] = em[i] - sm[i];
            if(m[i] < 0)
            {
                h[i]--;
                m[i] = 60 + em[i] - sm[i];
            }
            System.out.println(h[i] + " " + m[i] + "\n");
        }
        
    }
}

Comments