# Cannot understand the error in my program

the question is:

Chef is playing a video game, and is getting close to the end. He decides to finish the rest of the game in a single session.There are X levels remaining in the game, and each level takes Chef Y minutes to complete. To protect against eye strain, Chef also decides that every time he completes 3 levels, he will take a Z minute break from playing. Note that there is no need to take this break if the game has been completed.

How much time (in minutes) will it take Chef to complete the game?

input:
4
2 12 10
3 12 10
7 20 8
24 45 15
output:
24
36
156
1185

my solution:

``````// Update the code below to solve this problem
import java.util.Scanner;

// Update the code below to solve this problem
import java.util.Scanner;

class Codechef
{
public static void main (String[] args)
{

for(int i=0; i<t; i++)
{
int a=x/3;
int b=x%3;
if (b==0){
int total=a*(3*y)+z*(a-1);
System.out.println(total);
}
else if (b==1){
int total=a*(3*y)+z*(a)+y;
System.out.println(total);
}
else{
int total=x*y+a*y;
System.out.println(total);
}
}
}
}
``````

Why are you not counting Z minutes in the else case?

My logic is like this:
when we have the remainder as 1…lets say for a case where x=7
then the group will be divided as 3+3+2…z mins represents the mins taken extra after every 3 levels…as we can see here that there will be 2 such cases where we will have to add the z mins to get the proper output…

What about when the remainder is 2?

Your logic is wrong man !!!
First understood the problem correctly. According to the question if levels remaining(x here) is less than 3 then directly multiply xy , which becomes your result. But, if levels remaining is ,ore than three then you have to multiply xy and additionally we have to check whether x is completely divisible by x or not
//if x is completely divisible then we process as (x/3)-1 since after completion of all the levels coder was stopped the game ex in 4th case 24 45 15 here
//if x isn’t completely divisible by 3 then we process as x/3 and the remaining part is same ex in 3rd case 7 20 8 here
for more details you can check my solution

code in c++

// Update the code below to solve this problem
import java.util.Scanner;

class Codechef
{
public static void main (String[] args)
{
int[] Array; int i;
Array = new int[t];
for( i=0; i<t; i++)
{

``````		int a=x/3;
int b=x%3;
if (b==0){
int total=a*(3*y)+z*(a-1);
Array[i]= total;
}
else if (b==1){
int total=a*(3*y)+z*(a)+y;
Array[i]= total;
}
else{
int total=x*y+a*y;
Array[i]= total;
}
}
for(i=0;i<t;i++){
System.out.println(Array[i]);

}

}
``````

}

int main() {
int X, Y, Z;
std::cout << "Enter the number of levels remaining: ";
std::cin >> X;
std::cout << "Enter the time taken to complete each level (in minutes): ";
std::cin >> Y;
std::cout << "Enter the break time after every 3 levels (in minutes): ";
std::cin >> Z;

``````int breaks = (X - 1) / 3;
int break_time = breaks * Z;
int level_time = X * Y;
int total_time = break_time + level_time;

std::cout << "Total time taken to complete the game: " << total_time << " minutes" << std::endl;

return 0;
``````

}
In this code, the user is prompted to enter the number of levels remaining (X), the time taken to complete each level (Y), and the break time after every 3 levels (Z). The code then calculates the total time taken and displays the result.