In the problem to the counting of service time, the queue of an arbitrary customer at N Cass turn is given an array of integers int[] customers, where every element of a customers[i] is the service time of the ith customer.

In the process of solving it was necessary to sum the array elements in the case where we have N=1:

However, the decision required to find the sum of elements from two arrays - nCustomers and restOfCustomers.

I create a method that allows to sum the elements of an arbitrary array:

I now syntactically correct to call it to all three arrays in the code - customers nCustomers and restOfCustomers?

In the process of solving it was necessary to sum the array elements in the case where we have N=1:

```
.........
} else if (n == 1) {
for (int i = 0; i < customers.length; i++ ) {
sum += customers[i];
}
}
.........
return sum;
```

However, the decision required to find the sum of elements from two arrays - nCustomers and restOfCustomers.

I create a method that allows to sum the elements of an arbitrary array:

```
public int sumOfArray(int ary[]) {
int s = 0;
for (int i = 0; i < ary.length; i++) {
s += ary[i];
}
return s;
}
```

I now syntactically correct to call it to all three arrays in the code - customers nCustomers and restOfCustomers?

asked June 3rd 19 at 19:39

2 answers

answered on June 3rd 19 at 19:41

If I understand correctly and you want the sum of elements in three arrays, then:

Or you can create a method with varargs, which will count the sum of an arbitrary number of arrays:

Or swipe through the stream:

`int sumOfAll = sumOfArray(arr1) + sumOfArray(arr2) + sumOfArray(arr3)`

Or you can create a method with varargs, which will count the sum of an arbitrary number of arrays:

```
public int sumOfArray(int[]... ary) {
/// your code
}
```

Or swipe through the stream:

`int sumOfAll = IntStream.of(arr1).sum() + IntStream.of(arr2).sum() + IntStream.of(arr3).sum();`

answered on June 3rd 19 at 19:43

I now do that

Instead of list - use your collection of objects.

And inside the map(....) take the desired value of the field.

If SIU with objects that can

```
List<string> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("4");
list.add("1");
int summ = list.stream()
.map(s -> Integer.parseInt(s)) // converting from String to int
.reduce( (s1,s2) -> s1 + s2 ) // amount
.orElse(0); // default value
System.out.println(summ);</string>
```

Instead of list - use your collection of objects.

And inside the map(....) take the desired value of the field.

If SIU with objects that can

```
public class Reduce {
public static class Offer {
int price;
public Offer (int price) {
this.price = price;
}
public int getPrice() {
return price;
}
}
public static void main(String args[]) {
List< Offer > list = new ArrayList<>();
list.add(new Offer(1));
list.add(new Offer(2));
list.add(new Offer(4));
list.add(new Offer(1));
int summ = list.stream()
.map(Offer::getPrice) // take the price
.reduce( (s1,s2) -> s1 + s2 ) // amount
.orElse(0); // default value
System.out.println(summ);
}
}
```

1) "If the element is one, then why cycle?" - When running one office - N=1 - then the time required to service a queue set, for example, an array [2, 3, 1, 5, 7] - it is simply the sum of array elements: 2+3+1+5+7=18.

2) In the course of solving the problem when the number of people in the queue does not exceed the number of working desks, I just find the maximum element of the array customers:

3) In the case when the number of people in the queue is greater than the number of working banks (the customers.length > n && n > 1), I need to start the counter and to decrease the number by one all the elements of the array nCustomers (it's coming up to n banks n customers in the queue), while, as soon as some of its elements set to zero for the next iteration I in his place, put the first element of the array balance queue restOfCustomers[0], and the array restOfCustomers cut for this element. And all this occurs while the sum of elements in nCustomers will not be zero, and the array restOfCustomers is empty - zero length. - nikita.Stracke commented on June 3rd 19 at 19:44

It would seem that you need to do this:

But I have an error:

- nikita.Stracke commented on June 3rd 19 at 19:50