Question

Use Java or Javascript to slve

1. Given an array of numbers, write a function to return an array of numbers where productsli] is the Input: [1, 2, 3,4, 5] Output: [(2*3*4*5), (1 3*4*5), (1*2*4*5), (1*2*3*5), (1*2*3*4)1 [120, 60, 40, 30, 24] You should do this in O(N) without using division.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java code :

int[] product(int a[], int n)  

{

int prod[] = new int[n]; // making a product array ( n is the number of elements in the array)

for(int i=0;i<n;i++)

prod[i]=1; // Initialize this array elements with 1

int flag=1; // For left side product

for (int i=0;i<n;i++)  

{

prod[i] = flag; // flag will have productof all elements on left hand sisde excluding a[i]

flag = flag * a[i];

}

int temp = 1; // For right side product

for (int j=n-1;j>=0;j--)  

{

prod[j] = prod[j] * temp; // temp will have product of all elements on left hand sisde excluding a[j]

temp = temp * a[j];

}

return prod;

}

Add a comment
Know the answer?
Add Answer to:
Use Java or Javascript to slve 1. Given an array of numbers, write a function to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java question Given an array of integer numbers, write a linear running time complexity program in...

    Java question Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A itf ATO] + A[1] + +A[iI-1] Ali+1]+ Ali+2] +... + A[n-1]; where 0 <i< n-1 Similarly, 0 is an stability index if (A[1] A[2]A[n-1]) 0 and n-1 is an stability index if (A[0] A[1]+... A[n-21) 0 Example:...

  • Given an array of integer numbers, write a linear running time complexity program in Java to...

    Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A if A[0] + A[1] + ... + A[i-1] = A[i+1] + A[i+2] + ... + A[n-1]; where 0 < i < n-1. Similarly, 0 is an stability index if (A[1] + A[2] + ... + A[n-1]) = 0 and...

  • o Write a function that outputs even numbers less than the input number. • Function name...

    o Write a function that outputs even numbers less than the input number. • Function name should be 'evenprint'. • A number is given as input to the function. . The function must return a list of even numbers def evenprint(num): #write statement >[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74,...

  • In Javascript Define 2 functions in JSBin: - A function which receives an array of numbers...

    In Javascript Define 2 functions in JSBin: - A function which receives an array of numbers as an argument, and returns the product of all the numbers in that array.   For example, multiply([2,3,4]) should return 24. - A function which receives a string as an argument, and returns the reversal of that string.   For example, reverse('Hello there!') should return "!ereht olleH"

  • Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a...

    Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a one-dimensional array with the first 30 Fibonacci numbers using a calculation to generate the numbers. Note: The first two Fibonacci numbers 0 and 1 should be generated explicitly as in -long[] series = new long[limit]; //create first 2 series elements series[0] = 0; series[1] = 1; -But, it is not permissible to fill the array explicitly with the Fibonacci series’ after the first two...

  • Write a program in Java that stores an array nxm  in less space than nxm  given...

    Write a program in Java that stores an array nxm  in less space than nxm  given that the numbers in the area are repeated a lot     - Input:      Array n x m    - Output:         - Area is stored in less than n x m space         - When called the program is return the original array          Example:   Given 3x6 array A           A: [2,3,5,5,5,5                   1,3,5,6,5,6                   0,5,5,5,6,3] Program stored it  in less that   When...

  • Write a function called addNeighbors that takes an array of numbers and adds each number in...

    Write a function called addNeighbors that takes an array of numbers and adds each number in the array to the number next to it. The array will always have an even number of numbers in it. In the end you should return an array of neighbors added up. Please use JavaScript Instructions from your teacher. Medium Question #4 Write a function called addNeighbors that takes an array of numbers and adds each number in the array to the number next...

  • Write a function that takes an array of integers as an argument and returns a value...

    Write a function that takes an array of integers as an argument and returns a value based on the sums of the even and odd numbers in the array. Let X = the sum of the odd numbers in the array and let Y = the sum of the even numbers. The function should return X – Y The signature of the function is: int f(int[ ] a) Examples if input array is return {1} 1 {1, 2} -1 {1,...

  • Write a JAVA program that declares and initializes an array with the following numbers: 10, 89,...

    Write a JAVA program that declares and initializes an array with the following numbers: 10, 89, 50, 24, 60, 1, 15. Then ask the user for a number and check if the array contains that number. If it does, return a message to the user (print to screen) that says

  • use javascript please Complete the function below using recursion. Assume the parameter is a positive integer....

    use javascript please Complete the function below using recursion. Assume the parameter is a positive integer. The function will return an array that contains all of the numbers from 1 up to the parameter in consecutive order. For example, if the parameter is the value 5 then the return value should be the array [1, 2, 3,4 function makeSequence(value) t\ please use recursion thank von

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT