Monday, December 7, 2020

Code Gladiators 2020 - The Powerpuff Girls Coding Problem in C#

Professor Utonium is restless because of the increasing crime in the world. The number of villains and their activities has increased to a great extent. The current trio of Powerpuff Girls is not well to fight the evils of the whole world. Professor has decided to create the maximum number of Powerpuff Girls with the ingredients he has.

There are N ingredients required in a certain quantity to create a Powerpuff Girl. Professor has all the N ingredients in his laboratory and knows the quantity of each available ingredient. He also knows the quantity of a particular ingredient required to create a Powerpuff Girl. Professor is busy with the preparations and wants to start asap.

The villains, on the other hand, want to destroy the laboratory and stop Professor Utonium from creating more Powerpuff girls. Mojo Jojo is coming prepared with ammunition and Him is leading other villains like Princess, Amoeba Boys, Sedusa, Gangreen Gang etc.

Professor does not have much time as villains will reach the laboratory soon. He is starting the process but does not know the number of Powerpuff Girls which will be created. He needs your help in determining the maximum number of Powerpuff Girls which will be created with the current quantity of ingredients.

Example:
Professor Utonium requires 3 ingredients to make Powerpuff Girls. The 3 ingredients are present in the laboratory in the given quantity:


To make a Powerpuff Girl, Professor Utonium requires:
3 units of Ingredient A
6 units of Ingredient B
10 units of Ingredient C
The maximum number of Powerpuff Girls that can be created is 3 as, after 3, Professor will run out of Ingredient C.


Can you determine the maximum number?

Input Format
The first line of input consists of the number of ingredients, N
The second line of input consists of the N space-separated integers representing the quantity of each ingredient required to create a Powerpuff Girl.
The third line of input consists of the N space-separated integers representing the quantity of each ingredient present in the laboratory.

Constraints:
1<= N <=10000000 (1e7)
0<= Quantity_of_ingredient <= LLONG_MAX 
Output Format
Print the required output in a separate line.

Sample TestCase
Input
4
2 5 6 3 
20 40 90 50 
Output
8

Solution :

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

namespace ConsoleApplication1
{
    public class PowerpuffGirls
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("PowerpuffGirls Problem");
            bool flag = false;
            long count = 0;
            long N = Convert.ToInt64(Console.ReadLine());
            string StrReqQuantity = Console.ReadLine();
            StrReqQuantity = StrReqQuantity.Trim();
            string StrAvailableQuantity = Console.ReadLine();
            StrAvailableQuantity = StrAvailableQuantity.Trim();

            string[] StrReqQuantityArray = StrReqQuantity.Split(' ');
            string[] StrAvailableQuantityArray = StrAvailableQuantity.Split(' ');

            long[] RequiredQuantityArray = Array.ConvertAll(StrReqQuantityArray, q => long.Parse(q));
            long[] AvailableQuantityArray = Array.ConvertAll(StrAvailableQuantityArray, a => long.Parse(a));

            long[] ReverseAvailableQuantityArray = new long[N];
            ReverseAvailableQuantityArray = Array.ConvertAll(StrAvailableQuantityArray, a => long.Parse(a));
            Array.Sort(ReverseAvailableQuantityArray);
            long LargestElment = ReverseAvailableQuantityArray[N - 1];


            for (long i = 0; i < LargestElment; i++)
            {
                for (long j = 0; j < N; j++)
                {
                    if (RequiredQuantityArray[j] <= AvailableQuantityArray[j])
                    {
                        flag = true;
                        AvailableQuantityArray[j] = AvailableQuantityArray[j] - RequiredQuantityArray[j];
                    }
                    else
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag == true)
                {
                    count = count + 1;
                }
                if (flag == false)
                {
                    break;
                }
            }
            Console.WriteLine(count);
            Console.Read();

        }
    }
}

Saturday, December 5, 2020

Right Angled Alphabet Triangle in C#

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

namespace ConsoleApplication3
{
    class PrintABCDE
    {
        static void Main()
        {
            string Stroutput = string.Empty;
            Console.WriteLine("Enter any number between 1 and 26");
            int UserNumber = int.Parse(Console.ReadLine());
            if (UserNumber >= 1 && UserNumber <= 26)
            {
                char[] alpha = Enumerable.Range('A', UserNumber).Select(i => (Char)i).ToArray();
                for (int i = 0; i < UserNumber; i++)
                {
                    for (int y = 0; y <= i; y++)
                    {
                        Stroutput = string.Format(Stroutput + alpha[y] + " ");
                    }
                    Stroutput = Stroutput.Remove(Stroutput.Length - 1);
                    if (i == UserNumber - 1)
                    {
                        Console.Write(Stroutput);
                        Stroutput = string.Empty;
                    }
                    else
                    {
                        Console.WriteLine(Stroutput);
                        Stroutput = string.Empty;
                    }
                }
            }
            else
            {
                Console.WriteLine("Number entered is not in Given Range");
            }
            Console.Read();
        }
    }
}

Output 1 :

Output 2 :
     


Saturday, September 5, 2020

Comparison of Sum of Matrix Row Elements in C#

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

namespace ConsoleApplication1
{
    class MatrixRowAddition
    {
        static int Total = 0;
        static int RowNumber = 0;
        static int equals = 0;
        static bool IsEqual = true;
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter Row and Column Size for Matrix Separated By Single Space");
            int[] RowsCols= Array.ConvertAll(Console.ReadLine().Trim().Split(' '), a => int.Parse(a));
            Console.WriteLine("Enter Matrix Elements in Matrix Form separated by single Space");
            for (int Rows = 0; Rows < RowsCols[0]; Rows++)
            {
                int[] Elements = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), b => int.Parse(b));
                if (Rows == 0)
                {
                    equals = Elements.Sum();
                }
                if (Rows > 0)
                {
                    if (equals == Elements.Sum())
                    {
                    }
                    else
                    {
                        IsEqual = false;
                    }
                }
                if (Elements.Sum() > Total)
                {
                    Total = Elements.Sum();
                    RowNumber = Rows;
                }
            }
            if (IsEqual == false)
            {
                Console.WriteLine("Sum of Row {0} Elements are greater", RowNumber + 1);
            }
            else
            {
                Console.WriteLine("All Rows are Equal");
            }
            Console.Read();
        }
    }
}

Output 1 :











Output 2 :

Sunday, August 2, 2020

Comparison of Sum of Matrix Diagonal Elements in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication
{
  public class MatrixDiagonal
    { 
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter Row and Column Size for Matrix Separated By Single Space");
            int[] RowsCols = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), a => int.Parse(a));
            int Rows = RowsCols [0];
            int columns = RowsCols [1];
            int[,] UserMatrix = new int[Rows, columns];
            int j = 0;

            Console.WriteLine("Enter Matrix Elements in Matrix Form separated by single Space");
            for (int i = 0; i < Rows; i++)
            {
               int[] Elements= Array.ConvertAll(Console.ReadLine().Trim().Split(' '), a => int.Parse(a));

                foreach (var e in Elements)
                {
                    UserMatrix[i, j] = e;
                    j++;
                }
                j = 0;
            }

            int LeftDiagonal = 0;
            int RightDiagonal = 0;
            int y = columns - 1;

            for (int r = 0; r < Rows; r++)
            {
                LeftDiagonal = LeftDiagonal + UserMatrix[r, r];
                RightDiagonal = RightDiagonal + UserMatrix[r, y];
                y--;
            }

            Console.WriteLine("Sum of Left Diagonal Elements : {0}", LeftDiagonal);
            Console.WriteLine("Sum of Right Diagonal Elements : {0}", RightDiagonal);

            if (LeftDiagonal == RightDiagonal)
                Console.WriteLine("Both Diagonals are Equal");
            else if (LeftDiagonal > RightDiagonal)
                Console.WriteLine("LeftDiagonal is greater than RightDiagonal");
            else
                Console.WriteLine("LeftDiagonal is Less than RightDiagonal");

            Console.ReadLine();
        }
    }
}

Output 1 :


Output 2 :

Output 3 :

Monday, April 27, 2020

Addition of two User Defined Square Matrix in C#

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

namespace ConsoleApplication
{
    class MatrixAdd
    {
        public static void Main()
        {
            Console.WriteLine("Enter Row and Columns for First Matrix separated by single space");
            int rows, cols;
            string strRowsCols = Console.ReadLine();
            rows = int.Parse(strRowsCols.Substring(0, 1));
            cols = int.Parse(strRowsCols.Substring(2, 1));
            int j = 0;

            int[,] MatrixLength = new int[rows, cols];

            Console.WriteLine("Enter First Matrix Elements in Matrix Form");
            for (int i = 0; i < rows; i++)
            {
            int[] intArrays = Array.ConvertAll(Console.ReadLine().Split(' '), a => int.Parse(a));

                foreach (var k in intArrays)
                {
                    MatrixLength[i, j] = k;
                    j = j + 1;
                }
                j = 0;
            }

        Console.WriteLine("Enter Row and Columns for Second Matrix separated by single space");
            strRowsCols = Console.ReadLine().Trim();
            rows = int.Parse(strRowsCols.Substring(0, 1));
            cols = int.Parse(strRowsCols.Substring(2, 1));


            int[,] MatrixLength1 = new int[rows, cols];
            Console.WriteLine("Enter Second Matrix Elements in Matrix Form");
            j = 0;
            for (int i = 0; i < rows; i++)
            {
            int[] intArrays = Array.ConvertAll(Console.ReadLine().Split(' '), a => int.Parse(a));

                foreach (var k in intArrays)
                {
                    MatrixLength1[i, j] = k;
                    j = j + 1;
                }
                j = 0;
            }

           Console.WriteLine("Addition of above two Matrix");
            for (int k = 0; k < rows; k++)
            {
                for (int l = 0; l < cols; l++)
                {
                    if (l < cols - 1)
                        Console.Write("{0} ", MatrixLength[k, l] + MatrixLength1[k, l]);
                    else
                        Console.Write("{0}", MatrixLength[k, l] + MatrixLength1[k, l]);
                }

                if (k < rows - 1)
                    Console.WriteLine();
            }

            Console.Read();
        }
    }
}

First Output :



Second Output :



Thursday, April 16, 2020

Binary To Decimal Conversion in C#

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

namespace ConsoleApplication
{
    class Program1
    {
        static void Main(string[] args)
        {
            double sum = 0;
            Console.WriteLine("Enter the Binary Number : ");
            string strBinary = Console.ReadLine();
            int Len = strBinary.Length;
            int slen = 0;
            int DecimalPosition = 0;
            int counter = 1;
            bool flag = false;
            if (strBinary.Contains("."))
            {
                flag = true;

                DecimalPosition = strBinary.IndexOf(".");
                slen = strBinary.Substring(DecimalPosition+1,Len-DecimalPosition-1).Length;
                foreach (var u in strBinary.Substring(DecimalPosition + 1, Len - DecimalPosition - 1))
                {

                    if (u.ToString() == "1")
                    {

                        sum = sum + Math.Pow(2, ((counter) * -1));

                    }
                    counter++;
                }
            }


            if (flag == true)
            {
                slen = strBinary.Substring(0, DecimalPosition).Length;
                foreach (var s in strBinary.Substring(0, DecimalPosition))
                {
                    if (s.ToString() == "1")
                    {
                        sum = sum + Math.Pow(2, slen - 1);
                    }
                    slen--;
                }
            }

            if (flag == false)
            {
                foreach (var s in strBinary)
                {
                    if (s.ToString() == "1")
                    {
                        sum = sum + Math.Pow(2, Len - 1);
                    }
                    Len--;
                }
            }

            Console.WriteLine("Decimal Number :");
            Console.Write(sum);
            Console.Read();
        }
    }
}

First Output :



Second Output :

Tuesday, April 14, 2020

Transpose of User Defined Matrix in C#

using System;


namespace ConsoleApplication

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Enter Row and Column Size for Matrix Separated By Single Space");

            int rows, cols;

            string strRowsCols = Console.ReadLine();

            rows = int.Parse(strRowsCols.Substring(0, 1));

            cols = int.Parse(strRowsCols.Substring(2, 1));

            int j = 0;

            int[,] UserMatrix = new int[rows, cols];



            Console.WriteLine("Enter each Matrix Elements in Matrix Form");

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

            {

                int[] intArrays = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), a => int.Parse(a));

                foreach (var k in intArrays)

                {

                    UserMatrix[i, j] = k;

                    j = j + 1;

                }

                j = 0;

            }


            Console.WriteLine("Transpose Matrix");

            int r = 0;

            int c = 0;

            int temp;

            temp = rows;

            rows = cols;

            cols = temp;

            int[,] TransposeMatrix = new int[rows, cols];



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

            {

                for (int m = 0; m < cols; m++)

                {

                    TransposeMatrix[k, m] = UserMatrix[r, c];

                    r = r + 1;

                }

                r = 0;

                c = c + 1;

            }



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

            {

                for (int l = 0; l < cols; l++)

                {

                    if (l < cols - 1)

                        Console.Write("{0} ", TransposeMatrix[k, l]);

                    else

                        Console.Write("{0}", TransposeMatrix[k, l]);

                }

                if (k < rows - 1)

                    Console.WriteLine();

            }

            Console.Read();

        }

   }

}

Test Case Output 1 :

Test Case Output 2 :