The e-commerce company “BuySellGoods” collected the sales value of N most popular selling
products on its website. From these values the company decides to calculate the total sales value of
the two highest selling products.
Write an algorithm to help the company calculate the total sales value of the two highest selling
products on its website.
N
Input
The first line of input consists of an integer- num Products representing the total number of
products (N).
The next line consists of N space-separated integers - saleValue[1], saleValue[2.... saleValue[N],
representing the sales value of the N selected products.
Output
Print an integer representing the total sales value of the two highest selling products on the
website.
Constraints
2 s num Products s 100
Answers
Answer:
oxdgcoh hfxfjxgjcxhivo
ihhrtidyifihvoh
The first character in the input line is an integer number..The total number of goods that make up the total number of products (N). The two most popular products have a total worth of $71.
Explanation:
- The first line contains an integer.
- The following line contains a seperated N space integer.
- There are a lot of products with the same values.
- Take, for example, value, 15, 26, 13, 7, and 5,
import java.util.Scanner;
public class BuySellGoods
{
public static void main(String[] args)
{
int res;
Scanner scan = new Scanner(System.in);
Read n
int numProducts = scan.nextInt();
Initialiaze array of n integers representing the sales values
int[] saleValue = new int[numProducts];
Read the sales values from input
for(int i=0;i<numProducts;i++)
{
saleValue[i] = scan.nextInt();
}
sort the array to obtain the highest two values of sales
int temp;
for(int i=0;i<numProducts-1;i++) {
for(int j=i;j<numProducts;j++) {
if(saleValue[i]<saleValue[j]) {
temp = saleValue[i];
saleValue[i] = saleValue[j];
saleValue[j] = temp;
}
}
}
res = saleValue[0] + saleValue[1];
Add the first two elements of the array i.e the highest two sales
System.out.println(res);
Print output
}
}
Final answer
The second and fifth products are the ones with the highest sales value. As a result, the total sales value is 71 (26+45) dollars.
#SPJ3