I'm attempting to solve a problem where I need to find the maximum index difference in an array while adhering to certain constraints. I wrote the code in java and tried to replicate it in Python. However, when implementing my solution in Python, I encountered a "SyntaxError: invalid syntax" related to the logical AND operator.
I'm expecting the code to iterate through the array elements and update the maximum index difference according to the given constraints.
class Solution:
def maxIndexDiff(self, a, n):
right = []
right[n-1] = a[n-1]
for i in range(n-2, 0, -1):
right[i] = max(a[i], right[i+1])
i, j = 0, 0
ans = 0
while(i < n && j < n): # SyntaxError occurs here
if(a[i] <= right[j]):
ans = max(ans, j, -1)
j += 1
else:
i += 1
return ans