From aa5b0ef9b24a31e3c8fc2148dc01e243bc3f1c33 Mon Sep 17 00:00:00 2001 From: dhruvil15 <76967484+dhruvil15@users.noreply.github.com> Date: Wed, 29 Apr 2026 20:21:08 -0400 Subject: [PATCH] Complete Array-1 --- Sample.java | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/Sample.java b/Sample.java index f10cd8d1..f895824c 100644 --- a/Sample.java +++ b/Sample.java @@ -1,6 +1,33 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Three line explanation of solution in plain english +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Three line explanation of solution in plain english: +// We maintain a running product +// Frist do the left to right pass where we multiple numbers +// Then do a right to left pass -// Your code here along with comments explaining your approach \ No newline at end of file +// Your code here along with comments explaining your approach + +class Solution { + public int[] productExceptSelf(int[] nums) { + int n = nums.length; + int[] result = new int[n]; + + int rp = 1; + result[0] = 1; + + for (int i = 1; i < n; i++) { + rp = rp * nums[i-1]; + result[i] = rp; + } + + rp = 1; + + for (int i = n-2; i >=0; i--) { + rp = rp * nums[i+1]; + result[i] = result[i] * rp; + } + + return result; + } +} \ No newline at end of file