-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestockingTheWarehouse.java
More file actions
41 lines (31 loc) · 926 Bytes
/
RestockingTheWarehouse.java
File metadata and controls
41 lines (31 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class RestockingTheWarehouse
{
public static void main( String[] args )
{
Scanner s = new Scanner( System.in );
int n = s.nextInt();
List<Integer> nums = new ArrayList<Integer>();
for( int i = 0; i < n; i++ )
{
nums.add( s.nextInt() );
}
int target = s.nextInt();
System.out.println( restock( nums, target ) );
}
public static int restock( List<Integer> itemCount, int target )
{
BigDecimal sum = new BigDecimal( 0 );
for( int i : itemCount )
{
sum = sum.add( new BigDecimal( i ) );
if(sum.longValue()>=(long)target)
break;
}
long sumL = sum.longValue();
return Math.abs( (int)( sumL - (long)target ) );
}
}