Largest Gold Ingot TCS CodeVita 9 [SOLVED].
Ramesh is a goldsmith, who brought a large number of gold ingot each of
different length(L) but equal breadth(B) and height(H). He wanted to
wield the ingots of same length with each other.
Problem Description
Ramesh is a goldsmith, who brought a large number of
gold ingot each of different length(L) but equal breadth(B) and
height(H). He wants to weld the ingots of same length with each other.
He tasks his new employee, Akash, to weld the ingots of same length with
each other. But Akash forgot that he had to weld the ingots of same
length, instead he welded the ingots in a random manner.
Later Ramesh found out what he had done. He then ordered Akash to cut
the welded ingot such that a cuboid with the largest volume from the
welded gold ingot is obtained.
Find the volume of summation of gold ingots minus volume of the largest cuboid.
Constraints
0 < G < 10^5
First Line contains one integer G, denoting number of gold ingots
Second line contains two space separated integers B and H, where B
denotes the breadth and H denotes the height of individual ingot
Third line contains G space separated integers, denoting the length
of the individual gold ingots that are welded together in adjacent
manner
Output
An integer corresponding to the volume of summation of gold ingots minus volume of the largest cuboid, mod 10^9+7.
Time Limit
1
Examples
Example 1
Input
7
1 1
6 7 3 4 5 1 3
Output
14
Explanation
Total volume of shaded region is 15 and the total volume is 29. So the
volume of summation of gold ingots minus largest cuboid obtained is 14,
since the height is 1 and breadth is 1.
Example 2
Input
7
1 2
1 2 6 4 5 3 4
Output
20
Explanation
The volume of summation of gold ingots minus largest cuboid obtained is 20, since the height is 2 and breadth is 1.
Solution in Java:-
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class LargestGoldIngot
{
public static void main(String[] args) throws IOException {
int n,i,sum=0, B,H,m,res=0,ans=0,peek;
int arr[];
Scanner sc=new Scanner(System.in);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
n=sc.nextInt();
m=(int)Math.pow(10,9)+7;
B=sc.nextInt();
H=sc.nextInt();
arr=new int[n];
for( i=0;i<n;i++)
{
arr[i]=sc.nextInt();
sum=sum+arr[i];
}
Stack<Integer> stack = new Stack<Integer>();
for(i=0;i<n;i++){
while(stack.empty()==false && arr[i]<=arr[stack.peek()]){
peek=stack.peek();
stack.pop();
res= arr[peek]* (stack.empty()?i:(i-stack.peek()-1));
ans=Math.max(ans%m,res);
}
stack.push(i);
}
while(stack.empty()==false){
peek=stack.peek();
stack.pop();
res=arr[peek]*(stack.empty()? n:(n-stack.peek()-1));
ans=Math.max(ans%m,res);
}
System.out.println(((sum%m-ans%m)%m*B%m*H%m)%m);
}//end of main
}