Python Program to Get Sum of cubes of alternate even numbers in an array
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
# 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 ])) |
Output
224
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
# 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 )) |
Output
224
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
# 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 ])) |
Output
224
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using filter() and math.pow() methods
Python3
# 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 ])) |
Output
224
Time Complexity : O(N)
Auxiliary Space : O(N)
Please Login to comment...