Codechef : Problem name "FIND A and B" solution in C#

 ===========================Solution==========================

using System;


public class Test

{

public static void Main()

{

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

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

    string[] array=Console.ReadLine().Split(' ');

    int X=Convert.ToInt32(array[0]);

    int Y=Convert.ToInt32(array[1]);

    int Z=Convert.ToInt32(array[2]);

    bool flag=false;

    int A=0;

    int B=0;

    int[] arrayInt={X,Y,Z};

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

    {

        

        B= arrayInt[i];

        if(i==0)

        A= arrayInt[i+1]*arrayInt[i+2];

        else if(i==1)

        A=arrayInt[i-1]*arrayInt[i+1];

        else if(i==2)

        A=arrayInt[i-1]*arrayInt[i-2];

        

        if(A%B==0)

        {

            flag=true;

            Console.WriteLine(A+" "+B);

            break;

        }

        else{

            flag=false;

        }

    }

    if(flag==false)

    Console.WriteLine(-1);

}

}

}

=====================Explanation=====================
First we created an array with the numbers we receive
Given that B is any of the 3 numbers hence inside loop one by one we will make a number defined as B. So , A will be product of remaining two numbers from array. Then we are checking if A is divisible by B or not , if divisible then make the flag true. And if flag is true break out from code and print A and B

using System; public class Test { public static void Main() { int t=Convert.ToInt32(Console.ReadLine()); for(int j=0;j<t;j++){ string[] array=Console.ReadLine().Split(' '); int X=Convert.ToInt32(array[0]); int Y=Convert.ToInt32(array[1]); int Z=Convert.ToInt32(array[2]); bool flag=false; int A=0; int B=0; int[] arrayInt={X,Y,Z}; for(int i=0;i<3;i++) { B= arrayInt[i]; if(i==0) A= arrayInt[i+1]*arrayInt[i+2]; else if(i==1) A=arrayInt[i-1]*arrayInt[i+1]; else if(i==2) A=arrayInt[i-1]*arrayInt[i-2]; if(A%B==0) { flag=true; Console.WriteLine(A+" "+B); break; } else{ flag=false; } } if(flag==false) Console.WriteLine(-1); } } }

using System; public class Test { public static void Main() { int t=Convert.ToInt32(Console.ReadLine()); for(int j=0;j<t;j++){ string[] array=Console.ReadLine().Split(' '); int X=Convert.ToInt32(array[0]); int Y=Convert.ToInt32(array[1]); int Z=Convert.ToInt32(array[2]); bool flag=false; int A=0; int B=0; int[] arrayInt={X,Y,Z}; for(int i=0;i<3;i++) { B= arrayInt[i]; if(i==0) A= arrayInt[i+1]*arrayInt[i+2]; else if(i==1) A=arrayInt[i-1]*arrayInt[i+1]; else if(i==2) A=arrayInt[i-1]*arrayInt[i-2]; if(A%B==0) { flag=true; Console.WriteLine(A+" "+B); break; } else{ flag=false; } } if(flag==false) Console.WriteLine(-1); } } }

Comments