Python Program to Get Sum of cubes of alternate even numbers in an array
Last Updated :
30 Apr, 2023
Given an array, write a program to find the sum of cubes of alternative even numbers in an array.
Examples:
Input : arr = {1, 2, 3, 4, 5, 6}
Output : Even elements in given array are
2,4,6
Sum of cube of alternate even numbers are 2**3+6**3 = 224
Input : arr = {1,3,5,8,10,9,11,12,1,14}
Output : Even elements in given array are
8,10,12,14
Sum of cube of alternate even numbers are 8**3+12**3=2240
Method 1: Using Iterative method
- Start traversing the array from left to right.
- Maintain a result variable.
- Maintain a Boolean variable to check whether the current element should be added to the result or not.
- If the current element is even and it is an alternate element then find the cube of that element and add to the result.
- Finally, print the result.
Below is the implementation of the above approach
Python3
def sumOfCubeofAlt(arr):
n = len (arr)
isAlt = True
result = 0
for i in range (n):
if arr[i] % 2 = = 0 :
if isAlt:
result + = int (arr[i] * * 3 )
isAlt = False
else :
isAlt = True
return result
print (sumOfCubeofAlt([ 1 , 2 , 3 , 4 , 5 , 6 ]))
|
Complexity Analysis:
Time complexity: O(n)
Auxiliary Space: O(1)
Method 2: Using Recursive method
- We can implement the above approach using recursion by passing 4 parameters to the recursive function. The array itself, the index variable( to know where the array is traversed), a Boolean variable to check whether the current element should be added to the result or not, and the result variable to store the final result ( Cube of alternate even numbers).
- The base case is to check whether the index is reached at the end of an array or not.
- If the index is reached to end then stop calling the function and return the result.
Below is the implementation of the above approach
Python3
def sumOfCubeofAlt(arr, index, isAlt, ans):
if index > = len (arr):
return ans
if arr[index] % 2 = = 0 :
if isAlt:
ans + = int (arr[index] * * 3 )
isAlt = False
else :
isAlt = True
return sumOfCubeofAlt(arr, index + 1 , isAlt, ans)
print (sumOfCubeofAlt([ 1 , 2 , 3 , 4 , 5 , 6 ], 0 , True , 0 ))
|
Complexity Analysis:
Time complexity: O(n)
Auxiliary Space: O(n) for recursion call stack.
Method 3:Using range() function
We first get all the even numbers of the list. Then we find sum of cubes of alternate numbers of the above even numbers list
Python3
def sumOfCubeofAlt(arr):
result = 0
evenList = []
for i in arr:
if (i % 2 = = 0 ):
evenList.append(i)
n = len (evenList)
for i in range ( 0 , n, 2 ):
result + = int (evenList[i] * * 3 )
return result
print (sumOfCubeofAlt([ 1 , 2 , 3 , 4 , 5 , 6 ]))
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using filter() and math.pow() methods
Python3
def sumOfCubeofAlt(arr):
x = list ( filter ( lambda x: x % 2 = = 0 , arr))
res = 0
for i in range ( 0 , len (x)):
if i % 2 = = 0 :
import math
res + = math. pow (x[i], 3 )
return int (res)
print (sumOfCubeofAlt([ 1 , 2 , 3 , 4 , 5 , 6 ]))
|
Time Complexity : O(N)
Auxiliary Space : O(N)
Using list comprehension in python:
Approach:
In this approach, we will use list comprehension to get a list of even elements and a list of even elements with even indices. We will then calculate the sum of cubes of the even elements with even indices using another list comprehension.
Define a function named sum_of_cubes that takes an array arr as an argument.
Use list comprehension to create a list called even_nums that contains all even numbers in the array arr.
Use list comprehension to create another list called even_nums_even_index that contains all even numbers from the list even_nums that have an even index.
Calculate the sum of cubes of the numbers in the list even_nums_even_index using another list comprehension.
Return the sum of cubes from step 4.
Python3
def sum_of_cubes(arr):
even_nums = [x for x in arr if x % 2 = = 0 ]
even_nums_even_index = [even_nums[i] for i in range ( len (even_nums)) if i % 2 = = 0 ]
return sum ([x * * 3 for x in even_nums_even_index])
arr1 = [ 1 , 2 , 3 , 4 , 5 , 6 ]
arr2 = [ 1 , 3 , 5 , 8 , 10 , 9 , 11 , 12 , 1 , 14 ]
print ( "Even elements in the first array are:" , [x for x in arr1 if x % 2 = = 0 ])
print ( "Sum of cube of alternate even numbers in the first array is:" , sum_of_cubes(arr1))
print ( "Even elements in the second array are:" , [x for x in arr2 if x % 2 = = 0 ])
print ( "Sum of cube of alternate even numbers in the second array is:" , sum_of_cubes(arr2))
|
Output
Even elements in the first array are: [2, 4, 6]
Sum of cube of alternate even numbers in the first array is: 224
Even elements in the second array are: [8, 10, 12, 14]
Sum of cube of alternate even numbers in the second array is: 2240
Time complexity: O(n)
Space complexity: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...