Python Program to Get Sum of cubes of alternate even numbers in an array
Last Updated :
05 Sep, 2024
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
Python
# Python program to find out
# Sum of cubes of alternate
# even numbers in an array
# Function to find result
def sumOfCubeofAlt(arr):
n = len(arr)
# Maintain a Boolean variable to check whether current
# element should be added to result or not.
isAlt = True
result = 0
for i in range(n):
if arr[i] % 2 == 0:
# If the current element is
# even and it is alternate
# element then find the cube of
# that element and add to the result.
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
Python
# Python program to find out
# Sum of cubes of alternate
# even numbers in an array
# Recursive Function to find result
def sumOfCubeofAlt(arr, index, isAlt, ans):
# Base case, when index reached the
# end of array then stop calling function.
if index >= len(arr):
return ans
if arr[index] % 2 == 0:
# If the current element is even and it is alternate
# element then find the cube of that element and add to the result.
if isAlt:
ans += int(arr[index]**3)
isAlt = False
else:
isAlt = True
return sumOfCubeofAlt(arr, index+1, isAlt, ans)
# isAlt a Boolean variable to check whether current
# element should be added to result or not.
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
Python
# Python program to find out
# Sum of cubes of alternate
# even numbers in an array
# Function to find result
def sumOfCubeofAlt(arr):
result = 0
evenList = []
# Getting even numbers from the array
for i in arr:
if(i % 2 == 0):
evenList.append(i)
n = len(evenList)
# Getting the cubes of alternate even numbers
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
Python
# Python program to find out
# Sum of cubes of alternate
# even numbers in an array
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.
Python
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))
OutputEven 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)
Similar Reads
Python program to find the sum of all even and odd digits of an integer list The following article shows how given an integer list, we can produce the sum of all its odd and even digits. Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.Input : test_list = [345, 893]
5 min read
Python Program to Count Even and Odd Numbers in a List In Python working with lists is a common task and one of the frequent operations is counting how many even and odd numbers are present in a given list. The collections.Counter method is the most efficient for large datasets, followed by the filter() and lambda approach for clean and compact code. Us
4 min read
Python Program for Find sum of even factors of a number Given a number n, the task is to find the even factor sum of a number. Examples: Input : 30 Output : 48 Even dividers sum 2 + 6 + 10 + 30 = 48 Input : 18 Output : 26 Even dividers sum 2 + 6 + 18 = 26 Let p1, p2, ⦠pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respect
4 min read
Python program to count Even and Odd numbers in a Dictionary Given a python dictionary, the task is to count even and odd numbers present in the dictionary. Examples: Input : {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e' : 5}Output : Even = 2, odd = 3Input : {'x': 4, 'y':9, 'z':16}Output : Even = 2, odd = 1 Approach using values() Function: Traverse the dictionary and
3 min read
Python3 Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples: Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6,
2 min read