Leetcode 238 Product of Array Except Self (C,C++,C#,JAVA,JAVASCRIPT,RUBY,TYPESCRIPT)

Input:  [1,2,3,4]
Output: [24,12,8,6]
//C
int* productExceptSelf(int* nums, int numsSize, int* returnSize){
int left = 1;
int right = 1;

int *product=(int*)malloc(numsSize*sizeof(int));
*returnSize=numsSize;

for(int i = 0; i < numsSize; i++)
{
product[i] = left;
left *= nums[i];
}

for(int i = numsSize -1; i > -1; i--)
{
product[i] = right*product[i]; // left * right
right *= nums[i];
}
return product;
}
//C++
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int left = 1;
int right = 1;
vector<int> product;

for(int i = 0; i < nums.size(); i++)
{
product.push_back(left);
left *= nums[i];
}

for(int i = nums.size() -1; i > -1; i--)
{
product[i] = (right*product[i]); // left * right
right *= nums[i];
}
return product;
}
};
//C#
public class Solution {
public int[] ProductExceptSelf(int[] nums) {
int left = 1;
int right = 1;
int[] product = new int[nums.Length];

for(int i = 0; i < nums.Length; i++)
{
product[i] = left;
left *= nums[i];
}

for(int i = nums.Length -1; i > -1; i--)
{
product[i] = right*product[i]; // left * right
right *= nums[i];
}
return product;
}
}
//JAVA
class Solution {
public int[] productExceptSelf(int[] nums) {
int left = 1;
int right = 1;
int[] product = new int[nums.length];

for(int i = 0; i < nums.length; i++)
{
product[i] = left;
left *= nums[i];
}

for(int i = nums.length -1; i > -1; i--)
{
product[i] = right*product[i]; // left * right
right *= nums[i];
}
return product;
}
}
//JAVASCRIPT
var productExceptSelf = function(nums) {
let left = 1;
let right = 1;
let product = [];

for(let i = 0; i < nums.length; i++)
{
product[i] = left;
left *= nums[i];
}

for(let i = nums.length -1; i > -1; i--)
{
product[i] = right*product[i]; // left * right
right *= nums[i];
}
return product;
};
//RUBY
def product_except_self(nums)
left = 1;
right = 1;
product = [];

for i in (0..nums.length - 1)
product[i] = left;
left = left*nums[i];
end
for i in ((nums.length - 1).downto(0))

product[i] = right * product[i] # left * right
right = right*nums[i];
end
product;
end
//TYPECRIPT
function productExceptSelf(nums: number[]): number[] {
let left: number = 1;
let right: number = 1;
let product: number[] = [];

for(let i: number = 0; i < nums.length; i++)
{
product[i] = left;
left *= nums[i];
}

for(let i: number = nums.length -1; i > -1; i--)
{
product[i] = right*product[i]; // left * right
right *= nums[i];
}
return product;
};

--

--

--

student at Flatiron School

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Flask: Web Framework

Download YouTube videos — Dart

Linux desktop environment for VM shootout

Stains

My learning path — 5 years of a tester life

Man climbing mountains

Windows Authentication with .NET Core API and Angular on IIS

@Transactional-Propagations in short

INSTRUCTION: CHALLENGE MODE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Chan Park

Chan Park

student at Flatiron School

More from Medium

The flipside of the PM interview: The questions you should ask for your own sake

An Interview with Feka Parchibell

Peak: Secrets From the New Science of Expertise: Summary and Notes

My Interview experience at FORE School of Management New Delhi 2022