Looking for code review, suggestions for improvement, best practices etc.
Given N leaves numbered from 1 to N . A caterpillar at leaf 1, jumps from leaf to leaf in multiples of Aj (Aj, 2Aj, 3Aj). j is specific to the caterpillar. Whenever a caterpillar reaches a leaf, it eats it a little bit.. You have to find out how many leaves, from 1 to N, are left uneaten after all K caterpillars have reached the end. Each caterpillar has its own jump factor denoted by Aj, and each caterpillar starts at leaf number 1.
Input: The first line consists of a integer T denoting the number of testcases. T test cases follow. Each test case consists of two lines of input. The first line consists of two integers: N, which denotes the number of leaves; and K, which denotes the number of caterpillars. Second line of each test case consists of K space seperated integers denoting the jumping factor of caterpillars.
Example:
Input:
1
10 3
2 3 5
Output: 2
Explanation: Testcase1: The leaves eaten by the first caterpillar are (2, 4, 6, 8, 10). The leaves eaten by the second caterpilllar are (3, 6, 9) The leaves eaten by the third caterpilllar are (5, 10) Ultimately, the uneaten leaves are 1, 7 and their number is 2
class CatterpillarDetails{
int leaves;
public int getLeaves() {
return leaves;
}
ArrayList<Integer> caterpillarOrder;
public void setCaterpillarOrder(int order) {
caterpillarOrder.add(order);
}
public ArrayList<Integer> getcaterpillarOrder() {
return caterpillarOrder;
}
int noOfCatterpillar;
public int getNoOfCatterpillar() {
return noOfCatterpillar;
}
int result;
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
CatterpillarDetails(int leaves, int noOfCaterpillar)
{
this.leaves = leaves;
this.noOfCatterpillar = noOfCaterpillar;
this.caterpillarOrder = new ArrayList<Integer>(noOfCaterpillar);
}
}
public class Catterpillar {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcase = sc.nextInt();
CatterpillarDetails[] detailsObj = new CatterpillarDetails[testcase];
for(int i = 0 ; i < testcase ; i++)
{
int leaves = sc.nextInt();
int noOfCatterpillar = sc.nextInt();
detailsObj[i] = new CatterpillarDetails(leaves,noOfCatterpillar);
boolean[] eaten = new boolean[leaves];
for(int j = 0 ; j < noOfCatterpillar ; j++) {
int catterpillarOrder = sc.nextInt();
detailsObj[i].setCaterpillarOrder(catterpillarOrder);
for(int k = 1 ; k <= leaves ; k++)
{
if(catterpillarOrder*k > leaves)
break;
else
{
eaten[(catterpillarOrder*k)-1] = true;
}
}
}
detailsObj[i].setResult(calculateNotEaten(eaten));
}
for(int i = 0 ; i < testcase ; i++)
{
System.out.println(detailsObj[i].getResult());
}
}
private static int calculateNotEaten(boolean[] eaten) {
int count = 0;
for(Boolean i : eaten)
{
if(!i)
{
count++;
}
}
return count;
}
}