titan2400

Product of Array Except Self - LeetCode

Oct 27th, 2025 (edited)
707
0
Never
4
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 KB | Source Code | 0 0
  1. // Product of Array Except Self - https://leetcode.com/problems/product-of-array-except-self/description/
  2.  
  3. class Solution {
  4.     // Brute Force
  5.     // Time Complexity: O(n^2)
  6.     // Space Complexity: O(1)
  7.     // Below solution leads to Time Limit Exceeded on LeetCode
  8.     // public int[] productExceptSelf(int[] nums) {
  9.     //     int[] answer = new int[nums.length];
  10.     //     for(int i = 0; i < nums.length; i++) {
  11.     //         int product = 1;
  12.     //         for (int j = 0; j < nums.length; j++) {
  13.     //             if (i != j) {
  14.     //                 product *= nums[j];
  15.     //             }
  16.     //         }
  17.  
  18.     //         answer[i] = product;
  19.     //     }
  20.  
  21.     //     return answer;
  22.     // }
  23.  
  24.     // Ignoring the Division constraint
  25.     // Time Complexity: O(n)
  26.     // Space Complexity: O(1)
  27.     // Below solution doesn't work when input array has 0
  28.     // public int[] productExceptSelf(int[] nums) {
  29.     //     int[] answer = new int[nums.length];
  30.     //     int arrayProduct = 1;
  31.     //     for(int i = 0; i < nums.length; i++) {
  32.     //         arrayProduct *= nums[i];
  33.     //     }
  34.  
  35.     //     for(int i = 0; i < nums.length; i++) {
  36.     //         if (nums[i] != 0) {
  37.     //             answer[i] = arrayProduct / nums[i];
  38.     //         }
  39.            
  40.     //     }
  41.  
  42.     //     return answer;
  43.     // }
  44.  
  45.     // Ignoring the Division constraint
  46.     // Time Complexity: O(n)
  47.     // Space Complexity: O(1)
  48.     // public int[] productExceptSelf(int[] nums) {
  49.     //     int[] answer = new int[nums.length];
  50.     //     int arrayProduct = 1, zCount = 0;
  51.     //     for(int i = 0; i < nums.length; i++) {
  52.     //         if (nums[i] != 0) {
  53.     //             arrayProduct *= nums[i];
  54.     //         } else {
  55.     //             zCount++;
  56.     //         }
  57.  
  58.     //     }
  59.  
  60.     //     if(zCount > 1) {
  61.     //         return answer;
  62.     //     }
  63.  
  64.     //     for(int i = 0; i < nums.length; i++) {
  65.  
  66.     //         if(zCount > 0) {
  67.     //             answer[i] = (nums[i] == 0)? arrayProduct: 0;
  68.     //         }
  69.     //         else {
  70.     //             answer[i] = arrayProduct / nums[i];
  71.     //         }
  72.     //     }
  73.  
  74.     //     return answer;
  75.     // }
  76.  
  77.     // Using Prefix & Suffix products arrays
  78.     // Time Complexity: O(n)
  79.     // Space Complexity: O(n)
  80.     // public int[] productExceptSelf(int[] nums) {
  81.     //     int[] answer = new int[nums.length];
  82.  
  83.     //     int[] prefix = new int[nums.length];
  84.     //     int[] suffix = new int[nums.length];
  85.  
  86.  
  87.     //     // prefixes
  88.     //     prefix[0] = 1;
  89.     //     for(int i = 1; i < nums.length; i++) {
  90.     //         prefix[i] = prefix[i - 1] * nums[i - 1];
  91.     //     }
  92.  
  93.     //     // suffixes
  94.     //     suffix[nums.length - 1] = 1;
  95.     //     for(int i = nums.length - 2; i >= 0; i--) {
  96.     //         suffix[i] = suffix[i + 1] * nums[i + 1];
  97.     //     }
  98.  
  99.     //     for(int i = 0; i < nums.length; i++) {
  100.     //         answer[i] = prefix[i] * suffix[i];
  101.     //     }
  102.  
  103.     //     return answer;
  104.     // }
  105.  
  106.     // Using Prefix & Suffix products optimised for space
  107.     // Time Complexity: O(n)
  108.     // Space Complexity: O(1)
  109.     public int[] productExceptSelf(int[] nums) {
  110.         int[] answer = new int[nums.length];
  111.  
  112.         // prefixes
  113.         answer[0] = 1;
  114.         for(int i = 1; i < nums.length; i++) {
  115.             answer[i] = answer[i - 1] * nums[i - 1];
  116.         }
  117.  
  118.         // suffixes
  119.         int suffix = 1;
  120.         for(int i = nums.length - 1; i >= 0; i--) {
  121.             answer[i] *= suffix;
  122.             suffix *= nums[i];
  123.         }
  124.  
  125.         return answer;
  126.     }
  127. }
  128.  
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment