Ask Question
11 March, 13:05

Given an array of intergers, find the greatest product of 3 integers within that array and return that product

+1
Answers (1)
  1. 11 March, 14:08
    0
    Answer & Explanation:

    //written in java

    import java. util. Arrays;

    class Main {

    //this function find the greatest product of 3

    //integers within any array and return that product

    private static int greatestProduct (int[] arr, int length) {

    / / if the size of array is less that 3

    //the method return a value of 0

    if (length < 3) {

    return 0;

    }

    / / the code below sort the array in ascending order

    //multiply the last three, which is the product of the 3

    //greatest value in the array

    Arrays. sort (arr);

    return arr[length - 1] * arr[length - 2] * arr[length - 3];

    }

    / / the main method

    public static void main (String[] args) {

    int[] arr = {5, 3, 4, 2, 8, 10, 1};

    int n = arr. length;

    int product = greatestProduct (arr, n);

    //if the size of array is less than 3

    //greatestProduct (arr, n) return zero

    //and the program print out

    //"Size of array is less than three so product of 3 integers within that array does not exist"

    //else it print the product of the three largest integer in the array

    if (product = = 0)

    System. out. println ("Size of array is less than three/nso product of 3 integers within that array does not exist");

    else

    System. out. println ("Maximum product is " + product);

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Given an array of intergers, find the greatest product of 3 integers within that array and return that product ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers