diff --git a/README.md b/README.md index e38f5a8..22ca3b5 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Blogs on Self-Hosting, Homelab and DevOps Technologies. - Finding Elements in an Array that Sum Up to a Target Value - Group Anagrams in an Array - Finding Most Frequent Elements in an Array + - Building a Product Array without the Element Itself ## Technologies Used - [Hugo Static Site Generator](https://gohugo.io/) diff --git a/content/contents.md b/content/contents.md index 488a837..4aea70c 100644 --- a/content/contents.md +++ b/content/contents.md @@ -34,3 +34,4 @@ summary: "Index of all content" - Finding Elements in an Array that Sum Up to a Target Value - Group Anagrams in an Array - Finding Most Frequent Elements in an Array + - Building a Product Array without the Element Itself diff --git a/content/posts/dsa/building-a-product-array-without-the-element-itself/index.md b/content/posts/dsa/building-a-product-array-without-the-element-itself/index.md new file mode 100644 index 0000000..ed247f4 --- /dev/null +++ b/content/posts/dsa/building-a-product-array-without-the-element-itself/index.md @@ -0,0 +1,244 @@ +--- +author: "Avnish" +title: "Building a Product Array without the Element Itself" +date: "2023-10-28" +description: "Implementing a productExceptSelf function that returns an array of products of all the elements without the element at same index in input array" +tags: ["data-structures", "arrays", "go", "neetcode-150", "leetcode-medium"] +categories: ["Data Structures"] +series: ["Data Structures and Algorithms"] +aliases: + ["building-a-product-array-without-the-element-itself", "product-except-self"] +ShowToc: true +TocOpen: false +comments: false +cover: + image: "product-except-self-cover.png" + linkFullImages: true + alt: "The productExceptSelf function will returns an array of products of all the elements without the element at same index in input array" + caption: "" + relative: false + responsiveImages: false +math: true +--- + +# Problem Statement + +We have to implement a `productExceptSelf` function that takes an integer array `nums` as input and returns another array `answer` as output. The element `answer[i]` will be the product of all elements in `inputArray` except `inputArray[i]`. + +

Problem Statement for productExceptSelf

+ +The implementation should not contain a division (`/`) operation because the `answer` array could also be created by iterating over all elements in `inputArray` and dividing them from the product of the complete array. + +# Brute Force Solution + +The brute-force solution to this problem could be implemented using nested loops. Where the first loop will select an element from the array and the second loop will be used to calculate the product without the element from the first loop. + +## Psuedo-code for the Brute Force Solution + +```text +answer = [] +loop index on nums + product = 1 + loop index2 on nums + if index!=index2 + product *= nums[index2] + + answer.append(product) +return answer +``` + +## Best Case Scenario + +The brute-force solution will return the `answer` array in $O(n^2)$ time for the best-case input. Since both the loops will iterate completely over the `nums` array. + +## Worst Case Scenario + +The worst-case scenario will also take $O(n^2)$ to return the solution. + +## Code for Brute Force Solution + +```Go +package main + +import "fmt" + +func productExceptSelf(nums []int)([]int){ + answer := []int{} + for index:=0;indexCalculating prefix for nums array

+ +- `suffix[i]` is the product of all elements after `nums[i]` + +

Calculating suffix for nums array

+ +and multiply them to create the `answer` array. + +## Psuedo code for the Optimized Solution + +```text +prefix = [] +loop index on nums + prefixProduct = 1 + prefix.append(prefixProduct) + prefixProduct *= nums[index] + +suffix = [] +loop index from length(nums)-1 to 0 + suffixProduct = 1 + suffix.append_at_start(suffixProduct) + suffixProduct *= nums[index] + +answer = [] +loop index on prefix + answer.append(prefix[index]*suffix[index]) + + return answer +``` + +## Best Case Scenario + +The optimized solution will return the `answer` array in $O(n)$ time (generalized from $O(3n)$) since the time complexity of iterating over all three arrays (`prefix`, `suffix`, and `answer`) is $O(n)$. + +We can improve the space complexity further if we use a single array for storing products of `prefix` and `suffix` and return it as the `answer`. + +## Worst Case Scenario + +The time complexity of iterating over arrays in worst-case input is the same as in the best-case scenario. Thus, the total time complexity of the function remains the same i.e. $O(n)$. + +## Code for Optimized Solution + +```Go +package main + +import "fmt" + +func productExceptSelf(nums []int)([]int){ + prefix := []int{} + prefixProduct := 1 + for index:=0;index(-1);index--{ + // Append the element at the start + // rather than at the end + suffix = append([]int{suffixProduct}, suffix...) + suffixProduct *= nums[index] + } + + answer := []int{} + for index:=0;index(-1);index--{ + answer[index] *= suffixProduct + suffixProduct *= nums[index] + } + + return answer +} + +func main(){ + nums := []int{4, 2, 3, 7} + fmt.Println("Product Except Self:", productExceptSelf(nums)) + + nums = []int{45, 34, 67, 86, 23} + fmt.Println("Product Except Self:", productExceptSelf(nums)) +} + +// Output +// Product Except Self: [42 84 56 24] +// Product Except Self: [4505884 5963670 3026340 2357730 8815860] +``` + +
+ +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. + +# Resources + +238. Product of Array Except Self +Product of Array Except Self - Leetcode 238 - Python diff --git a/content/posts/dsa/building-a-product-array-without-the-element-itself/product-except-self-cover.png b/content/posts/dsa/building-a-product-array-without-the-element-itself/product-except-self-cover.png new file mode 100644 index 0000000..2addc77 Binary files /dev/null and b/content/posts/dsa/building-a-product-array-without-the-element-itself/product-except-self-cover.png differ diff --git a/content/posts/dsa/building-a-product-array-without-the-element-itself/product-except-self-prefix.png b/content/posts/dsa/building-a-product-array-without-the-element-itself/product-except-self-prefix.png new file mode 100644 index 0000000..8b12e60 Binary files /dev/null and b/content/posts/dsa/building-a-product-array-without-the-element-itself/product-except-self-prefix.png differ diff --git a/content/posts/dsa/building-a-product-array-without-the-element-itself/product-except-self-problem.png b/content/posts/dsa/building-a-product-array-without-the-element-itself/product-except-self-problem.png new file mode 100644 index 0000000..1194cae Binary files /dev/null and b/content/posts/dsa/building-a-product-array-without-the-element-itself/product-except-self-problem.png differ diff --git a/content/posts/dsa/building-a-product-array-without-the-element-itself/product-except-self-suffix.png b/content/posts/dsa/building-a-product-array-without-the-element-itself/product-except-self-suffix.png new file mode 100644 index 0000000..351e2bd Binary files /dev/null and b/content/posts/dsa/building-a-product-array-without-the-element-itself/product-except-self-suffix.png differ