# Product of array except self

Given an array/list [] of integers , Construct a product array Of same size Such That prod[i] is equal to The Product of all the elements of Arr[] except Arr[i] .

# Notes

• Array/list size is at least 2 .
• Array/list's numbers Will be only Positives
• Repetition of numbers in the array/list could occur.
``````fn product_array(arr: &[u64]) -> Vec<u64> {
let  len = arr.len();
let mut res: Vec<u64> = Vec::new(len).unwrap();
//     let mut res = vec![len];
let mut temp = 1;

for  i in 0..len{
res[i] = temp;
temp *= arr[i];
}
for i in (0..len).rev().step_by(1) {

res[i] *= temp;
temp *= arr[i]
}

return res;

//       todo!();

}
``````

Sounds like an algorithmic question for a practice problem? Assuming so...

If you have some sequence `a, a, ..., a` (for example), the product of all `a[i]` except `i=n` for a specific `n` would be...

`n` product
0: `a * a * a * a`
1: `a * a * a * a`
2: `a * a * a * a`
3: `a * a * a * a`
4: `a * a * a * a`

Perhaps take a moment to look for patterns and come up with an approach on your own. But if you're just looking for some direction to try...

Note that the products between subsequent rows are very similar. Between 1 and 2 for example, we have removed `a` from the product and inserted `a` in its place.

So one approach would be to calculate the product of all input elements except `a` -- that will be the first element in the solution, `s` -- and then calculate subsequent elements of the solution (`s[i]`) based on the previous element (`s[i-1]`).

3 Likes

Well i actually did something similar am having an issue with implementating the vector cause u can not change the array length once specified
It is given me usize instead of u36

It's really unclear what you are asking. Please rephrase your problem in a comprehensible way. You may want to ask for help to improve the English of your posts at least to the point where it becomes apparent what you are actually struggling with. From what I can tell, none of what you wrote should affect the solution of this task. It doesn't matter what specific type the array elements are (you can multiply and divide all types of integers Rust supports). It doesn't matter what type is used for storing the length of the array, either, as it's never to be found in the calculation (you only operate on its elements).

I already included my codes to the request pls check it

The immediate compile error in your code comes from the fact that `Vec::new()` is a 0-argument function that creates an empty vector, and your other attempt, `vec![len]` creates a 1-element vector from the length. You probably want a vector of `len` elements, all initialized to some value, which can be done like this: `vec![initial_value; len]`.

However, the rest of your function has little to do with the task. I don't exactly get what it does, but it doesn't compute the product of every element except one. For getting started with the algorithm proposed above, you could use this template, which currently compiles at least:

``````fn product_array(arr: &[u64]) -> Vec<u64> {
let len = arr.len();
let mut res = vec![1; len];

res
}
``````

I will try it

So i tested it am not getting the expected return values

Returned

assertion failed: `(left == right)`
left: `[4800, 2880]`,
right: `[20, 12]`

Codes i used

``````fn product_array(arr: &[u64]) -> Vec<u64> {
let len = arr.len();
let mut res = vec![1; len];
//     let mut res = vec![len];
let mut temp = 1;

for  i in 0..len{
res[i] = temp;
temp *= arr[i];
}
for i in (0..len).rev().step_by(1) {

res[i] *= temp;
temp *= arr[i]
}

return res;

//       todo!();

}
``````

Yes, that's exactly what I'm saying -- your current algorithm is incorrect, it has nothing to do with the task basically. Try implementing the solution suggested by @quinedot in my template.

That is what i used as the standard to write this algorithm

If it is no trouble can you write it

(Assuming this is not official homework –) Something like:

``````fn products(xs: &[u64]) -> Vec<u64> {
let product_all: u64 = xs.iter().product();
xs.iter().map(|&x| product_all / x).collect()
}
``````
2 Likes

No it is not just working on rust problem solving

@H2CO3 's answer solves the problem you describe above.

If that's not what you need, it would be better to provide some clear examples via inputs and expected outputs, instead of just a failed assertion.

Thanks i will

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.