Codechef : Problem name " The Block Game " solution in C#

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

using System;


public class Test

{

public static void Main()

{

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

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

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

    string X=Console.ReadLine();

    bool isPallindrome=false;

    int k,j;

    for(k=0,j=X.Length-1 ; k<=(X.Length-1)/2 && j>=(X.Length-1)/2 ; k++,j--)

    {

        if(X[k]=='0')

        {

            isPallindrome=false;

        }         

        if(X[k]==X[j])

        {

            isPallindrome=true;

        }

        else if(X[k] != X[j])

        {

            isPallindrome=false;

            break;

        }         

    }     

    if(isPallindrome)

    Console.WriteLine("wins");

    else if(isPallindrome==false)

    Console.WriteLine("loses");

}

}

}

===========================Explanation=============================
1.We have defined an bool variable which we will use to signal if given string is palindrome or not 
2.We have defined loop variables
3.We are running the loop on the given input , one from forward direction means index k=0 and one from backward direction means index j = length of the input
4.We need to run the loop to the point where we can check items from beginning and items from end are matching means we need to run the loop till half of the length of the input
5.As per calculation we are setting the value of the boolean value and as per value we are printing required output

Comments