## Oracle Interview Question

Developer Program Engineers**Country:**India

**Interview Type:**Written Test

import java.io.InputStreamReader;

import java.util.Arrays;

import java.util.Scanner;

public class Metals {

public static void main(String[] args) {

Scanner scanner=new Scanner(new InputStreamReader(System.in));

int i=1;

int costpercut=scanner.nextInt();

int price=scanner.nextInt();

int n=scanner.nextInt();

int arr[] =new int[n];

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

arr[j]=scanner.nextInt();

}

Arrays.sort(arr);

int maxItem=arr[arr.length-1];

int k=0;

int max=1;

while (i<=maxItem) {

int val=0;

for (int j = k; j < arr.length; j++) {

val=(price*i*(arr[j]/i))-(costpercut*(arr[j]/i))+val;

}

i++;

for (int j = 0; j < arr.length; j++) {

if (i>arr[j]) {

k=j;

}

}

if (val>max) {

max=val;

}

}

System.out.println(max);

}

}

public static void main(String[] args) {

Scanner scanner=new Scanner(new InputStreamReader(System.in));

int costpercut=scanner.nextInt();

int price=scanner.nextInt();

int n=scanner.nextInt();

int arr[] =new int[n];

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

arr[j]=scanner.nextInt();

}

int max = arr[0];

int maxProfit = 0;

for(int i = 0 ; i < arr.length; i++)

if(max < arr[i])

max = arr[i];

for(int size = 1 ; size <= max; size++) {

int profit = 0;

for(int i = 0 ; i < arr.length; i++) {

if(size > arr[i])

continue;

int currPrice = (arr[i] / size) * price * size; // current Rod price after cutting it.

int cuts = arr[i] % size == 0 ? (arr[i] / size) - 1 : (arr[i] / size); // Number of cuts depend on the length of rod.

int currProfit = currPrice - costpercut * cuts;

if(currProfit > 0)

profit += currProfit;

}

if(profit > maxProfit)

maxProfit = profit;

}

System.out.println(maxProfit);

}

```
public static void main(String[] args) {
Scanner scanner=new Scanner(new InputStreamReader(System.in));
int costpercut=scanner.nextInt();
int price=scanner.nextInt();
int n=scanner.nextInt();
int arr[] =new int[n];
for (int j = 0; j < n; j++) {
arr[j]=scanner.nextInt();
}
int max = arr[0];
int maxProfit = 0;
for(int i = 0 ; i < arr.length; i++)
if(max < arr[i])
max = arr[i];
for(int size = 1 ; size <= max; size++) {
int profit = 0;
for(int i = 0 ; i < arr.length; i++) {
if(size > arr[i])
continue;
int currPrice = (arr[i] / size) * price * size; // current Rod price after cutting it.
int cuts = arr[i] % size == 0 ? (arr[i] / size) - 1 : (arr[i] / size); // Number of cuts depend on the length of rod.
int currProfit = currPrice - costpercut * cuts;
if(currProfit > 0)
profit += currProfit;
}
if(profit > maxProfit)
maxProfit = profit;
}
System.out.println(maxProfit);
}
```

- prasenjit June 04, 2015