Chocolate distribution

There are N people in a row with some count number of chocolate in there hands. You have to select range of people to distribute those chocolate equally among them.

Write a program to determine Maximum number chocolate that can be placed in Box.
T - Test cases
N - Number of chocolate
M - Box

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MaxChocolateDistribution {

public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub

BufferedReader b=new BufferedReader(new InputStreamReader(System.in));

int T=Integer.parseInt(b.readLine());

for(int i=0;i<T;i++){
String line1[]=b.readLine().split(" ");


int M=Integer.parseInt(line1[1]);

String line2[]=b.readLine().split(" ");
int[] sum = new int[line2.length];
int j = Integer.parseInt(line2[0]);
sum[0] = j;
for (int i1 = 1; i1 < line2.length; i1++) {
sum[i1] = sum[i1 - 1] + Integer.parseInt(line2[i1]);
}
int max = 0;
for (int i1 = sum.length - 1; i1 >= 0; i1--) {
for (int p = i1 - 1; p >= 0; p--) {
if ((sum[i1] - sum[p]) % M == 0 && (sum[i1] - sum[p]) > max) {
max = (sum[i1] - sum[p]);
}
}
}
System.out.println(max / M);
}
}

}



Input
-------
3
5 3
1 2 3 4 5
5 4
1 2 3 4 5
5 8
1 2 3 4 5

Output
---------
5
3
0



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation