From 01d7895fc504a266e65a43edb5c9ae4777021190 Mon Sep 17 00:00:00 2001 From: Swaroop Phatak Date: Wed, 29 Oct 2025 23:37:41 +0530 Subject: [PATCH] Add productExceptSelf function in Solution class Implement productExceptSelf function to calculate the product of array elements except self. --- productExceptSelf_swaroop-phatak.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 productExceptSelf_swaroop-phatak.cpp diff --git a/productExceptSelf_swaroop-phatak.cpp b/productExceptSelf_swaroop-phatak.cpp new file mode 100644 index 0000000..9a7a4fb --- /dev/null +++ b/productExceptSelf_swaroop-phatak.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + vector productExceptSelf(vector& nums) { + int n = nums.size(); + vector res(n, 1); + int left = 1, right = 1; + for (int i = 0; i < n; i++) { + res[i] *= left; + left *= nums[i]; + res[n - i - 1] *= right; + right *= nums[n - i - 1]; + } + return res; + } +};