Solve it without division operator and in O(n).
Example:
Input : {2, 4,1, 4, 2, 2} Output : {64,32,128,32,64,64}
1. Declare an output Array
with size same as an input array.
2. Initialize all the elements of an output Array
to 1
3. Declare and initialize two variables
left
and right
to 1
.
4. Loop through the elements of an input Array
.
5. Within the loop, set outputArray[i] *= left
6. Set outputArray[inputArray.length - 1 - i] *= right
7. Set left *= inputArray[i]
8. Set right *= inputArray[inputArray.length - 1 - i]
9. Return outputArray
.
public static int[] arrayMultiplication(int [] inputArray) { }
C
Java
Python