Monday, July 19, 2021

Code Gladiators 2021 - The Prime Game Coding Problem in C#

Rax, a school student, was bored at home in the pandemic. He wanted to play but there was no one to play with. He was doing some mathematics questions including prime numbers and thought of creating a game using the same. After a few days of work, he was ready with his game. He wants to play the game with you.

GAME:
Rax will randomly provide you a range [ L , R ] (both inclusive) and you have to tell him the maximum difference between the prime numbers in the given range. There are three answers possible for the given range.

There are two distinct prime numbers in the given range so the maximum difference can be found.
There is only one distinct prime number in the given range. The maximum difference in this case would be 0.
There are no prime numbers in the given range. The output for this case would be -1.
To win the game, the participant should answer the prime difference correctly for the given range.

Example:
Range: [ 1, 10 ]
The maximum difference between the prime numbers in the given range is 5.
Difference = 7 - 2 = 5
Range: [ 5, 5 ]
There is only one distinct prime number so the maximum difference would be 0.
Range: [ 8 , 10 ]
There is no prime number in the given range so the output for the given range would be -1.

Can you win the game?

Input Format
The first line of input consists of the number of test cases, T
Next T lines each consists of two space-separated integers, L and R

Constraints
1<= T <=10
2<= L<= R<=10^6

Output Format
For each test case, print the maximum difference in the given range in a separate line.

Sample TestCase 1
Input
5
5 5
2 7
8 10
10 20
4 5

Output
0
5
-1
8
0

Explanation
Test Case 1: [ 5 - 5 ] = 0
Test Case 2: [ 7 - 2 ] = 5
Test Case 3: No prime number in the given range. Output = -1
Test Case 4: [ 19 – 11 ] = 8
Test Case 5: The difference would be 0 since there is only one prime number in the given range.

Solution:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class PrimeGame
    {
        public static void Main()
        {
         
            int T = Convert.ToInt16(Console.ReadLine());
            int[] Result = new int[T];
            for (int a = 0; a < T; a++)
            {
                int[] LR = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), arrTemp => int.Parse(arrTemp));
                Result[a] = CheckDifference(LR[0], LR[1]);
            }
            for (int b = 0; b < T; b++)
            {
                Console.WriteLine(Result[b]);
            }
            Console.Read();
        }
     
        static int CheckDifference(int m, int n)
        {
            int FN = 0;
            int LN = 0;
            bool[] prime = new bool[n + 1];
            for (int i = 0; i < n + 1; i++)
                prime[i] = true;
            for (int p = 2; p * p <= n; p++)
            {
                if (prime[p] == true)
                {
                    for (int i = p * 2; i <= n; i += p)
                        prime[i] = false;
                }
            }
            for (int i = m; i <= n; i++)
            {
                if (prime[i] == true)
                {
                    if (FN == 0)
                        FN = i;
                    else
                        LN = i;
                }
            }
            if (FN > 0 && LN == 0)
            {
                return 0;
            }
            else if (FN > 0 && LN > 0)
            {
                return LN - FN;
            }
            else
            {
                return -1;
            }
        }
    }
}

Wednesday, July 14, 2021

Code Gladiators 2021 - The Virus Outbreak Coding Problem in C#

In the Martian land faraway, a new virus has evolved and is attacking the individuals at a fast pace. The scientists have figured out the virus composition, V. The big task is to identify the people who are infected.The sample of N people is taken to check if they are POSITIVE or NEGATIVE. A report is generated which provides the current blood composition B of the person.

POSITIVE or NEGATIVE ?
If the blood composition of the person is a subsequence of the virus composition V, then the person is identified as POSITIVE otherwise NEGATIVE.

Example:
Virus Composition, V = coronavirus
Blood Composition of the person , B = ravus
The person in question is POSITIVE as B is the subsequence of the V


The scientists are busy with their research for medicine and request you to build a program which can quickly figure out if the person is POSITIVE or NEGATIVE. They will provide you with the virus composition V and all the people's current blood composition. Can you help them?

Note: The virus and blood compositions are lowercase alphabet strings.

Input Format
The first line of the input consists of the virus composition, V
The second line of he input consists of the number of people, N
Next N lines each consist of the blood composition of the ith person, Bi

Constraints
1<= N <=10
1<= |B|<= |V|<= 10^5


Output Format
For each person, print POSITIVE or NEGATIVE in a separate line.

Input
coronavirus
3
abcde
crnas
onarous
 
Output
NEGATIVE
POSITIVE
NEGATIVE


Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class VirusOutBreak
    {

        public static void Main()
        {
            string V = Console.ReadLine();
            int N = Convert.ToInt32(Console.ReadLine());
            string[] Msg = new string[N];
            for (int k = 0; k < N; k++)
            {
                string B = Console.ReadLine();
                bool result = TestReport(B, V, B.Length, V.Length);
                if (result)
                    Msg[k] = "POSITIVE";
                else
                    Msg[k] = "NEGATIVE";
            }
            for (int p = 0; p < N; p++)
            {
                Console.WriteLine(Msg[p]);
            }
            Console.Read();
        }

        static bool TestReport(string Blood, string Virus, int m, int n)
        {
            int j = 0;
            for (int i = 0; i < n && j < m; i++)
                if (Blood[j] == Virus[i])
                    j++;
            return (j == m);
        }
    }
}


Saturday, May 15, 2021

Finding index of a user defined value in user defined array in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Example
    {
        static void Main()
        {
            Console.WriteLine("Enter Array Size");
            int n = Convert.ToInt16(Console.ReadLine());
            Console.WriteLine(string.Format("Enter {0} Array Elements Separated by space", n));
            int[] ArrayElements = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), arrTemp => int.Parse(arrTemp));
            Console.WriteLine("Enter value whose index we want");            
            int PValue = Convert.ToInt16(Console.ReadLine());
            int low = 0;
            int high = n - 1;
            int mid = -1;
            int Answer = -1;

            while (low <= high)
            {
                mid = (low + high) / 2;
                if (ArrayElements[mid] == PValue)
                {
                    Answer = mid;
                    break;
                }
                else if (ArrayElements[mid] < PValue)
                {
                    low = mid + 1;
                }
                else if (ArrayElements[mid] > PValue)
                {
                    high = mid - 1;
                }
            }          
              
            if (Answer == -1)
            {
                Console.WriteLine("No Such Element Found in Array");
            }
            else
            {
                Console.WriteLine(string.Format("Index of value {0} is {1}", PValue, Answer));
            }
            Console.Read();
        }
    }
}

Output 1 :












Output 2 :












Time Complexity  :  0 (log2(n)) = 0(log(n))

Friday, January 8, 2021

Code Gladiators 2020 - The Beyblade World Championship Coding Problem in C#

Tyson is all prepared for the Beyblade World Championship. The tournament is team-based and each team can have N members. A player can fight against a single-player only. Team G-Revolution is all excited and pumped up as they have practiced a lot. Kenny, the mind of team G-Revolution, has created a database where he has the data about the power of other teams members and his own team members. The tournament is going to start in some time and Kenny moves to the cafeteria to have a snack before the competition.

The team G-Revolution is to fight in some time and they are tensed up as someone has kidnapped Kenny from the cafeteria. They have made a police complaint and the police are searching for Kenny. Luckily, they have found his device with all the data. The problem is, the data is present randomly and not in the order they have to fight the opponent. Team G-Revolution wants to win at any cost and for that, they need the order in which they have to fight optimally to win the maximum number of battles.

A player can win only when his/her Beyblade power is strictly greater than the opponent's Beyblade power.

Example:

Consider the team size is 3, N = 3

The 3 players of both the teams are shown with their Beyblade powers.


Team G-Revolution is presented in the order: Tyson, Max, Ray

Team, All Starz is presented in the order: Michael, Eddy, Steve

With the given arrangement, Team G-Revolution would be able to win only 1 fight. Team G-Revolution should be shuffled in an optimal manner as below:

The maximum number of fights Team G-Revolution can win is 2 when they are arranged optimally or fight in an optimal order.

Team G-Revolution needs help with the device. Tyson has heard about your skills and called you up to help them shuffle their positions in an order such that they would be able to win the maximum number of fights. Can you help Tyson and Team G-Revolution?

Input Format:

The first line of input consists of the number of test cases, T

The first line of each test case consists of the number of members each team can have, N.

The second line of each test case consists of N space-separated integers representing the power of Beyblades of Team G-Revolution members.

The third line of each test case consists of N space-separated integers representing the power of Beyblades of opponent team members.

Constraints:

1<= T <=100000
1<= N <=100000
0<= Power of Beyblade <= LLONG_MAX 

Output Format

For each test case, print the maximum number of fights Team G-Revolution can win if they go to fight in an optimal manner.

Sample TestCase
Input
1
10
3 6 7 5 3 5 6 2 9 1
2 7 0 9 3 6 0 6 2 6  
Output
7

Solution:

using System;
using System.Linq;
namespace ConsoleApplication1
{
    class BeyBladeFinal
    {
        public static void Main(string[] args)
        {
            int TestCases = Convert.ToInt32(Console.ReadLine());
            long count = 0;
            int k = 0;
            while (TestCases > 0)
            {
                long Members = Convert.ToInt64(Console.ReadLine());                                                            
                long[] G_Team_Array = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), arrTemp => long.Parse(arrTemp));
                long[] AllStars_Array = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), arrTemp => long.Parse(arrTemp));
                Array.Sort(G_Team_Array);
                Array.Sort(AllStars_Array);
                count = 0;
                k = 0;
                for (int i = 0; i < Members; i++)
                    while (k < Members)
                    {
                        if (G_Team_Array[i] <= AllStars_Array[k])
                            break;
                        else
                            count++;
                        k++;
                        break;
                    }
                Console.WriteLine(count);
                TestCases--;
            }
            Console.Read();
        }
    }
}