Snowfall

J&K is suffering from a heavily snowy winter.The heavy snow even causes the closure of an important highway connecting J&K and rest of India. You’ve got several reports containing the start and end points of highway segments covered by heavy snow. Given those reports , you are to return the total length of highway segments covered by snow. Note that the reported segments may overlap.

Input

Input consists of T(T<=100) number of test cases. T cases follow. Each input case starts with a number N , the number of road segments covered by snow. Next line contains N numbers , the start points of the segment. Next line contains N numbers , the end points of the segment. The points will always be <= 10000. For any particular segment start point will always be less than the end point.

 

Output

For each test case print case number followed by the total length of highway segments covered by snow. Suppose ur output for 1st test case is 25 , then print "Case 1:25".

 

Sample Input

2
3
17 85 57
33 86 84
8
45 100 125 10 15 35 30 9
46 200 175 20 25 45 40 10

 

Sample Output

Case 1:44
Case 2:132

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.Write("Enter number of road segments covered by snow: ");

            int numberOfRoadSegments = int.Parse(Console.ReadLine().ToString());

            int[] segmentStartPoint = new int[numberOfRoadSegments];

            int[] segmentEndPoint = new int[numberOfRoadSegments];

            for (int i = 0; i < numberOfRoadSegments; i++)

            {

                Console.Write("Enter start point of segment(" + (i + 1) + "): ");

                segmentStartPoint[i] = int.Parse(Console.ReadLine().ToString());

            }

            int globalStart = segmentStartPoint.Min();

            for (int i = 0; i < numberOfRoadSegments; i++)

            {

                Console.Write("Enter end point of segment(" + (i + 1) + "): ");

                segmentEndPoint[i] = int.Parse(Console.ReadLine().ToString());

            }

            int globalEnd = segmentEndPoint.Max();

            int[] highWayStretch = new int[globalEnd – globalStart + 1];

            //Initializing the High Way Stretch

            for (int i = 0; i < numberOfRoadSegments; i++)

            {

                for (int j = segmentStartPoint[i]; j < segmentEndPoint[i]; j++)

                {

                    highWayStretch[j – globalStart] = 1;

                }

            }

            //Console.WriteLine("Printing the highway array: ");

            //for (int i = 0; i < highWayStretch.Length; i++)

            //{

            //    Console.Write(highWayStretch[i] + " ");

            //}

            //Console.WriteLine();

            int totalLength = 0;

            //Calculating the total length of the highway segments covered by snow

            for (int i = 0; i < highWayStretch.Length – 1; i++)

            {

                if (highWayStretch[i] == 1)

                {

                    totalLength++;

                }

            }

            Console.WriteLine("Total length of the highway segments covered by snow: " + totalLength);

        }

    }

}

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s