0

I have done many submissions for this problem but getting TLE for all. The same code is accepted in Java. This is bit manipulation question with in which we are given two arrays with odd number of elements let's say A and B, we have to check and arrange array B such that A1^B1 = A2^B2 =...= An^Bn. To solve this first we have to xor all the elements of A and B to find the the number that can be the common xor value for all elements (e.g A1 ^ A2). After calculating the xor we have to XOR this value with all the elements of array A to find another array C. If array B contains all the elements of array C. Then array C is our answer, this is the array containing all elements of B whose XOR with corresponding item of array A return the same value for all elements. I just need to find out why I am getting TLE for my solution in C#.

My submissions link - https://www.codechef.com/status/XORGM,himanshu0786?sort_by=All&sorting_order=asc&language=All&status=13&Submit=GO

using System;
using System.Linq;
public class Test
{
    public static void Main()
    {
        int N, noOfTestCases = int.Parse(Console.ReadLine());
        string[] temp1, temp2;
        while (noOfTestCases-- != 0)
        {
            N = int.Parse(Console.ReadLine());
            temp1 = Console.ReadLine().Trim().Split(); //.Select(int.Parse).Take(N).ToArray();
            temp2 = Console.ReadLine().Trim().Split(); //.Select(int.Parse).Take(N).ToArray();

            int xor = 0;
            int[] A = new int[N], B = new int[N], C = new int[N];
            for (int i = 0; i < N; ++i)
            {
                A[i] = int.Parse(temp1[i]);
                B[i] = int.Parse(temp2[i]);
                xor ^= A[i] ^ B[i];
            }
            for (int i = 0; i < N; ++i)
            {
                C[i] = A[i] ^ xor;
            }
            if (B.Except(C).Any())
            {
                Console.WriteLine(-1);
            }
            else
            {
                foreach (var num in C)
                {
                    Console.Write($"{num} ");
                }
                Console.WriteLine();
            }
        }
    }
}
  • Aaaaah, TLE, is short for Time Limit Exceeded.... What is the Time Limit, that you seem to exceed ? – Luuk Aug 28 '21 at 14:56
  • I believe its 4 second. – HIMANSHU GARG Aug 28 '21 at 15:20
  • I believe it is 2 seconds, it is mentioned here: https://www.codechef.com/problems/XORGM. But you are not the only person who is suffering from TLE's, see; https://discuss.codechef.com/search?q=TLE – Luuk Aug 28 '21 at 15:24
  • I strongly recommend you to use C/C++ to solve algorithm problems, I've seen your first version code which uses double-loop to check if B and C are the same, the time complexity of the first version is O(n^2), which for 1≤N≤10^5 is very hard to finish in 4s. So I use the key-value map to optimize the complexity into O(n) yet still get a TLE. I once met this kind of situation when using Java, same method in C successfully passed but Java just can't pass. Repeat, I strongly recommend you to use C/C++(It's very fast) to solve algorithm problems. – Drinter Aug 28 '21 at 15:36
  • I wrote almost the same code in C and it passed with 0.21ms costs. As for why is the same method passes the problem in C/C++ but not C#, the article as follows might be an answer.https://stackoverflow.com/questions/5326269/is-c-sharp-really-slower-than-say-c – Drinter Aug 28 '21 at 16:08
  • There is some relaxation for programming languages like Java, C# and Python. Because I see solutions with Java and Python languages taking more than 3 seconds with successful submission. The relaxation must not be enough for C#. – HIMANSHU GARG Aug 28 '21 at 16:09

0 Answers0