0% found this document useful (0 votes)
5 views4 pages

Largest Product of Two Elements of An Unsorted Array

Uploaded by

shuklanikita568
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Largest Product of Two Elements of An Unsorted Array

Uploaded by

shuklanikita568
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

//Largest Product Of Two Elements Of An Unsorted Array

import java.util.*;

class Product

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("ENTER THE NUMBER OF ELEMENTS OF ARRAY");

int n=sc.nextInt();

int nums[]=new int[n];

//Entering elements of array

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

System.out.println("ENTER THE ELEMENTS OF ARRAY");

nums[K]=sc.nextInt();

int largest=0;

int secondLargest=0;

// Iterate through the array to find the two largest elements.

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

if (nums[i] > largest)

{
// If the current element is greater than the largest, update both .

secondLargest = largest;

largest = nums[i];

else if (nums[i] > secondLargest)

// If the current element is greater than the second largest, update only
secondLargest.

secondLargest = nums[i];

// Iterate through the array to find the two smallest elements.

int smallest=0;

int secondSmallest=0;

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

if (nums[j] < smallest)

//If the current element is smaller than the smallest, update both.

secondSmallest = smallest;

smallest = nums[j];

else if(nums[j] < secondSmallest)

{
//If the current elementis smaller than tghe second smallest,update only secondSmallest.

secondSmallest = nums[j];

//To check if two positive elements or two negative elements which have highest product.

if ((largest * secondLargest)>(smallest * secondSmallest))

System.out.println (largest * secondLargest);

else if((largest * secondLargest)<(smallest * secondSmallest))

System.out.println (smallest * secondSmallest);

else

System.out.println (largest * secondLargest);

//OUTPUT

ENTER THE NUMBER OF ELEMENTS OF ARRAY

ENTER THE ELEMENTS OF ARRAY

39

ENTER THE ELEMENTS OF ARRAY

-88

ENTER THE ELEMENTS OF ARRAY

65

ENTER THE ELEMENTS OF ARRAY


72

ENTER THE ELEMENTS OF ARRAY

21

ENTER THE ELEMENTS OF ARRAY

-90

ENTER THE ELEMENTS OF ARRAY

101

7920

You might also like