Codechef : Problem name " Running Comparison " solution in C#

 ==============================Problem statement==============================

Alice and Bob recently got into running, and decided to compare how much they ran on different days.

They each ran for N days — on the ith day, Alice ran Ai meters and Bob ran Bi meters.

On each day,

·  Alice is unhappy if Bob ran strictly more than twice her distance, and happy otherwise.

·  Similarly, Bob is unhappy if Alice ran strictly more than twice his distance, and happy otherwise.

For example, on a given day

·  If Alice ran 200200 meters and Bob ran 500500, Alice would be unhappy but Bob would be happy.

·  If Alice ran 500500 meters and Bob ran 240240, Alice would be happy and Bob would be unhappy.

·  If Alice ran 300300 meters and Bob ran 500500, both Alice and Bob would be happy.

On how many of the N days were both Alice and Bob happy?

Input Format

·  The first line of input will contain a single integer T, denoting the number of test cases.

·  Each test case consists of three lines of input.

o   The first line of each test case contains a single integer N — the number of days.

o   The second line of each test case contains N space-separated integers 1,2,…,A1​,A2​,…,AN — the distances run by Alice on the N days.

o   The third line of each test case contains N space-separated integers 1,2,…,B1​,B2​,…,BN — the distances run by Bob on the N days.

Output Format

For each test case, output on a new line the number of days when both Alice and Bob were happy.

==============================Solution in C#================================

using System;


public class Test

{

public static void Main()

{

    int t=Convert.ToInt32(Console.ReadLine());

for(int i=0;i<t;i++){

    int N=Convert.ToInt32(Console.ReadLine());  

    string[] Alice=Console.ReadLine().Split(' ');//inputs of Alice run

    string[] Bob=Console.ReadLine().Split(' ');//inputs of Bob run

    int count=0;

    for(int k=0;k<N;k++)

    {

        if((Convert.ToInt32(Alice[k])<=2*Convert.ToInt32(Bob[k]))&&(Convert.ToInt32(Bob[k])<=2*Convert.ToInt32(Alice[k]))){

            count=count+1;

        }

    }     

    Console.WriteLine(count);

}

}

}

=================================Explanation=============================
For each element form array of Alice and Bob we are checking if value is less than equal to twice the other value , means both are happy.
If this above condition is satisfied we are increasing the count and finally printing the count.

Comments

Popular Posts