Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
778 views
in Online Assessments by Expert (46,090 points) | 778 views

2 Answers

0 like 0 dislike
Best answer

Questions in OA.
image
image
image
image

by Expert (46,090 points)
0 like 0 dislike
  public int MinDegreeOfExtendedPalindrome(string s)
    {
        int degree = 1;
        int i = 0;
        int j = s.Length - 1;
        
        //regular palindrome
        if (CheckPalindrome(s, ref i, ref j, degree))
            return degree;

        var primes = GetPrimes(s.Length);
        int k = 0;
        while (i < j && k < primes.Length)
        {
            //check if the prime number is a divisor of s.Length
            if (s.Length % primes[k] == 0)
            {
                degree = primes[k];
                if (CheckPalindrome(s, ref i, ref j, degree))
                    return degree;
            }
            k++;
        }

        return -1;
    }

    public bool CheckPalindrome(string s, ref int i, ref int j, int degree)
    {
        while(i < j)
        {
            string a = s.Substring(i, degree);
            string b = s.Substring(j - degree + 1, degree);
            if (a.Equals(b))
            {
                i += degree;
                j -= degree;
            }
            else
                return false;
        }

        return true;
    }

    public int[] GetPrimes(int n)
    {
        var primes = new List<int>();
        primes.Add(2);
        int next = 3;
        
        while (next <= n)
        {
            int sqrt = (int)Math.Sqrt(next);
            bool isPrime = true;
            for(int i = 0; primes[i] <= sqrt; i++)
            {
                if(next % primes[i] == 0)
                {
                    isPrime = false;
                    break;
                }
            }

            if (isPrime)
                primes.Add(next);

            next += 2;
        }

        return primes.ToArray();
    }
by Expert (46,090 points)