Frage im Vorstellungsgespräch bei Capgemini

Java Basic Questions Program to write a product of array except itself

Antworten zu Vorstellungsgespräch

Anonym

26. Mai 2022

import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class GFG { // Function to remove the element public static int[] removeTheElement(int[] arr, int index) { // If the array is empty // or the index is not in array range // return the original array if (arr == null || index = arr.length) { return arr; } // Create ArrayList from the array List arrayList = IntStream.of(arr) .boxed() .collect(Collectors.toList()); // Remove the specified element arrayList.remove(index); // return the resultant array return arrayList.stream() .mapToInt(Integer::intValue) .toArray(); }

Anonym

15. Juli 2022

public class productExceptSelf { public static void main(String[] args) { int a[] = {4, 6, 1, 2}; prodArray(a, a.length); } public static void prodArray(int a[], int n) { int p[] = new int[n]; int prod = 1; //Find product of all elements of a[] for (int i = 0; i < n; i++) { prod = prod * a[i]; } //Create array p[] to store //product except self for (int i = 0; i < n; i++) { p[i] = prod / a[i]; } for (int i = 0; i < n; i++) { System.out.print(p[i] + " "); } } }