Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. package Arrays;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ProductOfSelf {
  6.  
  7. public static void main(String[] args) {
  8. Scanner s = new Scanner(System.in);
  9. int n = s.nextInt();
  10. int[] input = new int[n];
  11. for (int i = 0; i < n; i++) {
  12. input[i] = s.nextInt();
  13. }
  14. s.close();
  15. int[] output = productExceptSelf(input);
  16. // Printing output
  17. for (int i = 0; i < n; i++) {
  18. System.out.print(output[i] + " ");
  19. }
  20. }
  21.  
  22. public static int[] productExceptSelf(int[] input) {
  23. int[] output = new int[input.length];
  24. int[] left = new int[input.length];
  25. int[] right = new int[input.length];
  26.  
  27. // Filling the left
  28. left[0] = 1;
  29. for (int i = 1; i < left.length; i++) {
  30. left[i] = left[i - 1] * input[i - 1];
  31. }
  32.  
  33. // Filling the Right
  34. right[input.length - 1] = 1;
  35. for (int i = input.length - 2; i >= 0; i--) {
  36. right[i] = right[i + 1] * input[i + 1];
  37. }
  38.  
  39. for (int i = 0; i < input.length; i++) {
  40. output[i] = left[i] * right[i];
  41. }
  42.  
  43. return output;
  44. }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement