Your solution seems correct - but it seems u have a couple of syntax errors if you're using python You also have to re-initialize sum1 & sum2 between test cases. Right now you get the total sum of all arrs rather than arr[x]-hopefully that makes sense. You also have to remove duplicate numbers from the sum. For instance, if you're getting the sum of all numbers up to 20 - you'll end up adding 15 twice as it is divisible by 3 and 5. So the inner for loops will add it twice to the sum. So you'll need to remove 15 to get rid of duplicates.

for x in range(len(arr)): #by default range starts at o. therefore, range(len(arr)) = range(0, len(arr)) 
    sum1 = 0 # you forgot to initialize sum1
    sum2 = 0 # you forgot to initialize sum2
    duplicates = 0 #you have to remove duplicates from the answer
    for b5 in range (0, arr[x], 5): #you have to add the colons here
        sum1 = sum1 + b5
    for b3 in range (0, arr[x], 3): #you have to add the colons here
        sum2 = sum2 + b3
    for dup in range(0, arr[x], 3*5): # removes duplicates from the final sum
        duplicates = duplicates + dup
    sum = sum1 + sum2 - duplicates 
    print(sum)

This is an O(n^2) solution - you can drop it down to O(n) using a little bit of math.

You'll notice that the inner for loops can be represented using the formula sum(n)=Σd*i=d*Σi-where the summation starts at i = 0, end at ⌊(n-1)/d⌋ and d is the divisor (in the case of your question d=3 or 5).

for b5 in range (0, arr[x], 5):
    sum1 = sum1 + b5

(https://en.wikipedia.org/wiki/Summation)

There is a very common summation formula that is commonly used to convert summations into a closed-form expression (something with finite steps - which is O(1))

Σi=n*(n+1)/2

In the case of the inner loop - it would be sum(n) = d*(⌊(n-1)/d⌋)*(⌊(n-1)/d⌋+1)/2.

let,

f(n,d) = (⌊(n-1)/d⌋+1)/2

Therefore, the solution to your problem would be f(n,3)+f(n,5)-f(n,3*5)

Which would convert the inner for loops from O(n) to O(1). Which, means your entire solution would be O(n).

I'll let you figure out the code on your own. However, theoretically, there is a better solution; such that as arr grows indefinitely the work scales linearly rather than quadratically.

Answer from Hasin Zaman on Stack Overflow
Top answer
1 of 4
3

Your solution seems correct - but it seems u have a couple of syntax errors if you're using python You also have to re-initialize sum1 & sum2 between test cases. Right now you get the total sum of all arrs rather than arr[x]-hopefully that makes sense. You also have to remove duplicate numbers from the sum. For instance, if you're getting the sum of all numbers up to 20 - you'll end up adding 15 twice as it is divisible by 3 and 5. So the inner for loops will add it twice to the sum. So you'll need to remove 15 to get rid of duplicates.

for x in range(len(arr)): #by default range starts at o. therefore, range(len(arr)) = range(0, len(arr)) 
    sum1 = 0 # you forgot to initialize sum1
    sum2 = 0 # you forgot to initialize sum2
    duplicates = 0 #you have to remove duplicates from the answer
    for b5 in range (0, arr[x], 5): #you have to add the colons here
        sum1 = sum1 + b5
    for b3 in range (0, arr[x], 3): #you have to add the colons here
        sum2 = sum2 + b3
    for dup in range(0, arr[x], 3*5): # removes duplicates from the final sum
        duplicates = duplicates + dup
    sum = sum1 + sum2 - duplicates 
    print(sum)

This is an O(n^2) solution - you can drop it down to O(n) using a little bit of math.

You'll notice that the inner for loops can be represented using the formula sum(n)=Σd*i=d*Σi-where the summation starts at i = 0, end at ⌊(n-1)/d⌋ and d is the divisor (in the case of your question d=3 or 5).

for b5 in range (0, arr[x], 5):
    sum1 = sum1 + b5

(https://en.wikipedia.org/wiki/Summation)

There is a very common summation formula that is commonly used to convert summations into a closed-form expression (something with finite steps - which is O(1))

Σi=n*(n+1)/2

In the case of the inner loop - it would be sum(n) = d*(⌊(n-1)/d⌋)*(⌊(n-1)/d⌋+1)/2.

let,

f(n,d) = (⌊(n-1)/d⌋+1)/2

Therefore, the solution to your problem would be f(n,3)+f(n,5)-f(n,3*5)

Which would convert the inner for loops from O(n) to O(1). Which, means your entire solution would be O(n).

I'll let you figure out the code on your own. However, theoretically, there is a better solution; such that as arr grows indefinitely the work scales linearly rather than quadratically.

2 of 4
0
  1. Create Array with input number (Eg. arr = [12, 15, 4] )
  2. Iterate over the created Array
  3. In for loop check if number are divisible by 3 and 5

Sample code:

    arr = [12, 15, 4]
    total = 0
    
    for num in arr:
        if num % 3 == 0 and num % 5 == 0:
            total = total + num
    
    print(total) # 15
Discussions

How do I loop though the length of an array inside of another array in Python? - Stack Overflow
I need to loop through the keys array, which is a group of arrays within one array, I use two for loops. One to access the different arrays, and then one to loop through the contents of each array.... More on stackoverflow.com
🌐 stackoverflow.com
January 18, 2020
Loop over array elements without knowing length
Either your array is static or it's dynamic. If the array is static you know its length even if you don't explicity set it to a known length. You can do this int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; const int ARRAYLEN = (sizeof(array) / sizeof(array[0])); Use the ARRAYLEN value in your code. If the array is dynamic (malloc(), etc) you need to keep track of the array size, there's no other way. More on reddit.com
🌐 r/arduino
22
10
September 6, 2024
Why iterate over an array using the index?
You do it if there is something in your loop that will use an index rather than the elements of the array. For instance, sometimes you want to sample something from another array that is not your loop array A cleaner way to do it is by using enumerate function for i, item in enumerate(array): The index gets assigned to i and whatever element of the array gets assigned to item. This may also be a bad habit from Matlab, people that come from Matlab are more used to looping over indexes. More on reddit.com
🌐 r/learnpython
57
46
October 3, 2022
What's the python version of (i=0;i<array.length;i++)? for x in list doesn't work as expected.

I think this is an XY problem. Explain exactly what you're trying to accomplish and why and how it's not working. for x in y: is the way it ought to be expressed in 99.9% of cases.

More on reddit.com
🌐 r/learnprogramming
27
0
April 25, 2012
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to get array length in python
How to Get Array Length in Python - Spark By {Examples}
May 31, 2024 - First, create an array using array() function and set the length to '0'. Then, apply for loop over an array and for each iteration, increment the loop by 1 and increase the length value.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-array-length
Python Array length - GeeksforGeeks
July 23, 2025 - You can get the length by capturing the last index it provides. You must start from 1 to match the actual count. ... Explanation: enumerate(a, 1) loop through the list a with a counter starting at 1. Although the loop does nothing (pass), the ...
🌐
W3Schools
w3schools.com › python › gloss_python_array_loop.asp
Python Loop Through an Array
You can use the for in loop to loop through all the elements of an array. ... Python Array Tutorial Array What is an Array Access Arrays Array Length Add Array Element Remove Array Element Array Methods
🌐
Tutorialspoint
tutorialspoint.com › python › python_loop_arrays.htm
Python - Loop Arrays
This variable often represents an index for accessing elements in the array. Inside the while loop, iterate over the array elements and manually update the loop variable. The following example shows how you can loop through an array using a while loop − · import array as arr # creating array a = arr.array('i', [96, 26, 56, 76, 46]) # checking the length l = len(a) # loop variable idx = 0 # while loop while idx < l: print (a[idx]) # incrementing the while loop idx+=1
🌐
CodeGym
codegym.cc › java blog › learning python › length of an array in python
Length of an Array in Python
November 7, 2024 - Alternatively, you can write your own function using a for loop to count the elements manually. The simplest and most common method is, of course, using len(). It's efficient, readable, and works with different types of arrays.
Find elsewhere
🌐
iO Flood
ioflood.com › blog › get-length-of-array-in-python-guide-and-examples
Get Length of Array in Python: Guide and Examples
March 12, 2024 - As such, it’s generally recommended to use len() instead. An alternative method to calculate the length of an array involves using a for loop to iterate over the array and count the number of elements.
🌐
Codecademy
codecademy.com › forum_questions › 529bc86980ff33fe45011652
Array and Loops 13/14 | Codecademy
For a for loop to iterate through the array and stop at the end, you need to use the array’s length property, array.length. This will let JavaScript know how many pockets the array has, because i will guide JavaScript through the searching process. When i is equal to the same number, JavaScript has reached the end of ...
🌐
IONOS
ionos.com › digital guide › websites › web development › python list length
How to find the length of a Python list - IONOS
July 18, 2023 - If you want to avoid using functions, ... Python loops. The most popular way to find out a Python list length is to use the Python len function. This is defined for sequences, such as lists, and col­lec­tion types.
🌐
Trey Hunner
treyhunner.com › 2016 › 04 › how-to-loop-with-indexes-in-python
How to loop with indexes in Python
In this article I’ll compare Python’s for loops to those of other languages and discuss the usual ways we solve common problems with for loops in Python. Before we look at Python’s loops, let’s take a look at a for loop in JavaScript: This JavaScript loop looks nearly identical in C/C++ and Java. In this loop we: Set a counter variable i to 0 · Check if the counter is less than the array length ·
🌐
GeeksforGeeks
geeksforgeeks.org › iterate-over-a-list-in-python
Iterate over a list in Python - GeeksforGeeks
... We can use the range() method with for loop to traverse the list. This method allow us to access elements by their index, which is useful if we need to know the position of an element or modify the list in place.
Published   January 2, 2025
🌐
W3Schools
w3schools.com › python › gloss_python_array_length.asp
Python Array Length
Python Examples Python Compiler ... Q&A Python Bootcamp Python Certificate Python Training ... Use the len() method to return the length of an array (the number of elements in an array)....
🌐
TutorialsArena
tutorialsarena.com › programming › python › python-arrays-loop
Looping Through Arrays in Python: Using For and While Loops
August 10, 2024 - Use the built-in len() function to find the length of an array. Create a range object with the length to get the indices, then access the elements in a for loop.
🌐
W3Schools
w3schools.com › python › python_arrays.asp
Python Arrays
An array can hold many values under a single name, and you can access the values by referring to an index number. You refer to an array element by referring to the index number. ... Use the len() method to return the length of an array (the number ...
🌐
DataCamp
datacamp.com › tutorial › python-list-size
Python List Size: 8 Different Methods for Finding the Length of a List in Python | DataCamp
February 7, 2024 - # Use len() to find the size of the list length = len(my_list) print(length) # Output: 4 · Referred to as the naive method, this approach involves initializing a counter and incrementing it for each element encountered in a for loop through the list. You can learn more about for loops and other types of loops in this tutorial. # Naive method using a for loop to count the list's size counter = 0 for item in my_list: counter += 1 print(counter) # Output: 4 · List comprehensions in Python offer a compact syntax for performing operations on a list.
🌐
Flexiple
flexiple.com › python › length-of-array-python
Python Array Length - How to Calculate the Length of an Array in Python - Flexiple - Flexiple
April 11, 2022 - Calculating the length of an array in Python is a straightforward process. The built-in function len() is specifically designed for this task. It returns the total number of elements in an array or list.
🌐
CodeRivers
coderivers.org › blog › lenght-of-array-python
Understanding the Length of Arrays in Python - CodeRivers
April 11, 2025 - One common use of knowing the array length is to iterate over the elements. You can use a for loop with the range() function to iterate a specific number of times based on the length of the array.