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:
The alternating array of an array A having N integers can be created as follows:
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).
| Name | Type | Description |
|---|---|---|
| M | INTEGER | The number of elements in the alternating array. |
| X | INTEGER ARRAY | The 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
Input format for debugging
Sample Testcases

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.