Help me in solving CHEFEREN problem

My issue

Can anyone pls help me why my code fails testcases

My code

t=int(input())
for i in range(t):
    n,a,b=map(int,input().split())
    if(n%2==0):
        print((n//2)*(a+b))
    
    else:
        print(((n+1//2)*(a+b))-a)

Problem Link: CHEFEREN Problem - CodeChef

here we are looking that we have divided the episode as it could be even or odd thus we make two variables and give them half of their values , and now we find whether the total number of episodes are even or not , thus if there are odd number of total episodes then we would get an extra odd episode thus increase the no of odd episode by one , thus now we have got the total number of distributed episode thus we multiply them to their respective time duration and we have the total watch time as the output.

// solution in C++14
#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <iomanip>
#include <numeric>
using namespace std;

int main() {
	// your code goes here
    int t;
    cin>>t;
    while(t--)
    {
      int n,m,k;
        cin>>n>>m>>k;

        int a = n/2;
        int b = n/2;
        if(n%2) b++;
        cout<< (a*m) + (b*k)<<endl;
          
      
    }
	return 0;
}

1 Like

The logic you’re using appears to be faulty.

My logic is to run a for loop in the range of 1 to n+1 and check if j is even or odd and if it is then add it to the variable that stores the watch time.

This is my solution for this question.

1 Like

in your case the loop is executing , thus time complexity will be O(n)
but here the complexity is O(1) as it not executing any loop . Also in the loop the condition is checked again and again , thus taking more time in your case .

Sure but since the question does not have limits of any sort, I preferred on giving him a simple solution and logic for it.

1 Like