Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FindLargestProduct {
- public static int adjacentElementsProduct(final int[] inputArray) {
- if (inputArray == null) {
- throw new IllegalArgumentException("Input array must not be null.");
- }
- if (inputArray.length < 2) {
- throw new IllegalArgumentException("Minimum array size is two elements.");
- }
- int largestProduct = Integer.MIN_VALUE;
- for (int index = 0; index < inputArray.length - 1; ++index) { // (one less than the end, due to "index + 1" reference below]
- final int thisProduct = inputArray[index] * inputArray[index + 1];
- if (thisProduct >= largestProduct) {
- largestProduct = thisProduct;
- }
- }
- return largestProduct;
- }
- public static void main(final String[] args) {
- final int[] inputArray = {3, 6, -2, -5, 7, 3};
- final int largestProduct = FindLargestProduct.adjacentElementsProduct(inputArray);
- System.out.println(largestProduct);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement