Feedback Needed | Infosys | OA | Alternating Array
Anonymous User
256

I tried to solve two questions from Infosys OA for DSE/SE on 24 April 2022.
I was able to solve Alternating Array Question but got TLE for 3 test cases out off 15.

I need feedback and efficient solution, if any.

Here is the question:

Alternating Array

The alternating array of an array A having N integers can be created as follows:

  1. Create an array B of length N.
  2. Add the first element of A at the last position of B.
  3. Add the second element of A at the first position of B.
  4. Add the third element of A at the second last position of B.
  5. Add the fourth element of A at the second position of B.
  6. Continue the above process till you have added all elements of A to 8.

You are given an alternating array X having M elements. Let the original array of X be
called O.

Output the hash value (0[1]*10^1+0[2]*10^2 +...0[M]*10^M) modulo (10^9)+7.

Function description

Complete the solve function in the editor below. It has the following parameter(s).

NameTypeDescription
MINTEGERThe number of elements in the alternating array.
XINTEGER ARRAYThe alternating array.

Return: The function must return an INTEGER denoting the
(0[1]*10^1+0[2]*10^2+...0[M]*10^M) modulo 10^9+7

Constraints

  • 1 ≤ M ≤ 10^5
  • 1 ≤ X[i] ≤ 10^9

Input format for debugging

  • The first line contains an integer, M, denoting the number of elements in X.
  • Each line i of the M subsequent lines (where O ≤ i M) contains an integer
    describing X[i].

Sample Testcases
image

Sample Code:


import sys

def solve(M, X):
    #write your code here

def main():
    M = int(sys.stdin.readline().strip())
    X = []
    for _ in range(M):
        X.append(int(sys.stdin.readline().strip()))

    result = solve(M, X)
    print(result)

if __name__ == "__main__":
    main()

My Approach:

I found a pattern.
If M is even number. Ex. M=4 and Input is 2, 2,1, 6 then output is
middle number, middle+1, middle-1, ....., 0
21260
If M is odd number. Ex. M=5 and Input is 1, 2, 3, 4, 5 then output is
middle number, middle-1, middle+1, middle-2, middle+2...., 0
324150

By I got TLE for 3 test cases.

Comments (1)