arrayspython-3.xdata-structures# Having trouble finding Pivot index

I am trying to get the pivot index of the given array using python in leetcode. for the Given an array of integers nums, calculate the pivot index of this array.

This is the code i've used

```
class Solution:
def pivotIndex(self, nums: List[int]) -> int:
if 1 <= len(nums) <= 104:
counter1 = 0
for i in range(len(nums)):
if -1000 <= nums[i] <= 1000:
counter1 += nums[i]
counter2 = 0
for j in range(i,len(nums)):
counter2+=nums[j]
if counter1 == counter2:
return i
else:
return -1
return -1
```

It works with most usecases but i don't understand why it is not working in this specific usecase. this is my submission ( the usecase is present there too) can anyone explain me what i did wrong?

Solution

You're checking for the length of the input list being between `1`

and `104`

, but the problem specifies the maximum length as 10 to the power of 4, or 10,000. You are thus returning `-1`

when the length is greater than 104 without attempting to solve the test case at all.

So, it should be

```
if 1 <= len(nums) <= 10000:
```

The input for these problems are guaranteed to be valid and conform to the problem specification, so you can also remove the superfluous checks.

Note that this problem can be solved much faster (in linear time), by precomputing prefix and suffix sums.

- Matlab: Problem with logical indexing (combining two vectors with different sample rate)
- Rotate an YUV byte array on Android
- Symfony2 - Trouble using usort inside a controller
- Check if a string exists in an array case insensitively
- why does this code prints " " instead of "." however it prints the other elements correctly?
- Loop through an array of strings in Bash?
- How to explode only on the last occurring delimiter?
- Does usort() behave differently in php 8.2.0 vs php 7.4.33?
- Associative-array in PHP from CSV / TXT file
- Add a new element to an array without specifying the index in Bash
- Parse two arrays to make one PHP
- Merge two indexed arrays of associative arrays and group by column value to create an associative array of indexed arrays
- How to combine two different tables into one in codeigniter?
- Add a prefix to all values in an array which don't already have it
- Iterating over array of polymorphic objects
- How to use an ARRAYFORMULA in Google Sheets that references cells in the same column
- Append associatve elements to each row while looping a mysqli query result
- Count occasions when a column value in a 2d array starts with a specified letter
- Text parsing nightname in MS Excel
- Find min / max value in Swift Array
- How to generate a repeating sequence of numbers in Google Sheet?
- SwiftUI. Trying to pass variables between views with @Binding but keep getting errors
- Dynamic Multidimensional Array in C++
- JavaScript Array Every method passing argument showing undefine for arrow lambda callback function
- How can I remove the first element of an array and return the rest?
- NumPy: matrix multiplication when coordinates are given separately, is there a shortcut?
- Ho do I list values of an array in a column based on values from other arrays - Excel VBA
- MySQL SELECT WHERE IN LIST and NOT IN LIST in the same SQL
- Update key value in jsonb array of objects
- How to reproduce `kneighbors_graph(include_self=True)` using `KNeighborsTransformer` in sklearn?