

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find a list of product of all elements except the current index in Python
Suppose we have a list of numbers called nums, we have to find a new list such that each element at index i of the newly generated list is the product of all the numbers in the original list except the one at index i. Here we have to solve it without using division.
So, if the input is like nums = [2, 3, 4, 5, 6], then the output will be [360, 240, 180, 144, 120]
To solve this, we will follow these steps −
- if size of nums < 1, then
- return nums
- l := size of nums
- left := a list of size l and initially all values are null
- right := a list of size l and initially all values are null
- temp := 1
- for i in range 0 to size of nums, do
- if i is same as 0, then
- left[i] := temp
- otherwise,
- temp := temp * nums[i - 1]
- left[i] := temp
- if i is same as 0, then
- temp := 1
- for i in range size of nums - 1 to 0, decrease by 1, do
- if i is same as size of nums - 1, then
- right[i] := temp
- otherwise,
- temp := temp * nums[i + 1]
- right[i] := temp
- if i is same as size of nums - 1, then
- for i in range 0 to size of nums, do
- left[i] := left[i] * right[i]
- return left
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums): if len(nums) < 1: return nums l = len(nums) left = [None] * l right = [None] * l temp = 1 for i in range(len(nums)): if i == 0: left[i] = temp else: temp = temp * nums[i - 1] left[i] = temp temp = 1 for i in range(len(nums) - 1, -1, -1): if i == len(nums) - 1: right[i] = temp else: temp = temp * nums[i + 1] right[i] = temp for i in range(len(nums)): left[i] = left[i] * right[i] return left ob = Solution() nums = [2, 3, 4, 5, 6] print(ob.solve(nums))
Input
[2, 3, 4, 5, 6]
Output
[360, 240, 180, 144, 120]
- Related Questions & Answers
- Python program to replace all Characters of a List except the given character
- Program to find the product of three elements when all are unique in Python
- Python Program to Find Number of Occurrences of All Elements in a Linked List
- Product of all Subsequences of size K except the minimum and maximum Elements in C++
- Program to find the largest product of two distinct elements in Python
- Product of Array Except Self in Python
- Program to find sum of all elements of a tree in Python
- Find sum of all elements in a matrix except the elements in row and-or column of given cell in Python
- Find the average of all elements of array except the largest and smallest - JavaScript
- Python program to find sum of elements in list
- Python – Find Product of Index Value and find the Summation
- Python Program that print elements common at specified index of list elements
- Program to find highest common factor of a list of elements in Python
- Find sum of elements in list in Python program
- C# program to find the index of an element in a List
Advertisements