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 Sum of Array Given an array of integers, find the sum of its elements. Examples: Input : arr[] = {1, 2, 3}Output : 6Explanation: 1 + 2 + 3 = 6This Python program calculates the sum of an array by iterating through each element and adding it to a running total. The sum is then returned. An example usage is provid
4 min read
Python Program to Find the Number Occurring Odd Number of Times Write a Python program for a given array of positive integers. All numbers occur an even number of times except one number which occurs an odd number of times. Find the number in O(n) time & constant space. Examples : Input: arr = {1, 2, 3, 2, 3, 1, 3}Output : 3 Input: arr = {5, 7, 2, 7, 5, 2, 5
3 min read
Difference between Sums of Odd and Even Digits in Python Write a python program for a given long integer, we need to find if the difference between sum of odd digits and sum of even digits is 0 or not. The indexes start from zero (0 index is for the leftmost digit). Examples: Input: 1212112Output: YesExplanation:the odd position element is 2+2+1=5the even
2 min read
Create three lists of numbers, their squares and cubes using Python In this article, we are going to create a list of the numbers in a particular range provided in the input, and the other two lists will contain the square and the cube of the list in the given range using Python.Input: Start = 1, End = 10Output:Numbers_list = [1,2,3,4,5,6,7,8,9,10]Squares_list= [1,
4 min read
Sort Even-Placed in Increasing and Odd-placed Elements in Decreasing Order - Python We need to sort the elements at even indices in increasing order and the elements at odd indices in decreasing order. For example, given a list [10, 3, 5, 8, 2, 7, 6, 1], the result should be [2, 8, 5, 7, 6, 3, 10, 1]. Let us explore different ways to achieve this in Python.Using List Comprehension
3 min read
Calculating the sum of all columns of a 2D NumPy array Let us see how to calculate the sum of all the columns of a 2 dimensional NumPy array. Example : Input : [[1, 2, 3, 4, 5], [5, 6, 7, 8, 9], [2, 1, 5, 7, 8], [2, 9, 3, 1, 0]] Output : [10, 18, 18, 20, 22] Input : [[5, 4, 1, 7], [0, 9, 3, 5], [3, 2, 8, 6]] Output : [8, 15, 12, 18] Approach 1 : We will
2 min read