My code is working fine on my cpu compiler but on codechef it shows wrong answers

You can check the editorial it will help

then what if the input string is “(no space)not(no space)”
again the output will be wrong

besides can you help me how to start learning for CP
as i just started coding in C++ and straight away came to codechef as its really challenging
So what all topics should i start to learn first ??

Well if you have just started then you should get your basics fixed first…try to solve easy problems first then move to codechef contests . Like you can go to codeforces and start solving problems in descending order of submission it will help you to boost your confidence and for C++ you must learn STL so that you can use concepts of map stack set and all in you programme .don’t just rush because you won’t be a great programmer in a day .it will take time and if you keep working hard then results will follow so for now I will suggest you two things

  1. Do codeforces from descending order of submission
    2.learn from geek for geeks
    And keep trying in the live challenges no matter how your rating is as it will increase eventually if you are working hard constantly…hope this helps
1 Like

I am about to post a collection of link’s I have found out for preparing for ACM-ICPC …it might help you a lot … I am just working to fix it’s alignment asap

thnx for support
BTW i found the problem myself teh problem is that once i take an input from the user what my compiler does is it stores a all characters before a whitespace and then the next word is automatically stored into another string for instance
suppose my custom input console ;looks like :
not working
what is this
the output i wil get will be
Real FAncy // as it sees not as one string
regularly fancy // sees working as the 2nd string 2nd word of the first string
regularly fancy // sees hey as the third string

can anyone help me get all the words entered once into a single string variable ???


with getline i got problem too

code is here : (using getline)

#include <iostream>
#include <string.h>
 using namespace std;
 int main () {
     string str2 (" not "); 
     string str3 ("not "); 
     string str4 ("not"); 
     int T;
     for (int k=0;k<T;k++){
     string str ;

     int len = str.size();
    //condition for only not as a sentence
             if ((str.find(str4) != string::npos) && len ==3) {
cout<<"Real Fancy";

// condition to check for not word in middle of a sentence eg. this is not good
else if ((str.find(str2) != string::npos) ) {
cout<<"Real Fancy";

// condition if the statement ends with the word not 
else if (str[len-1]=='t' && str[len-2]== 'o' && str[len-3]== 'n' && str[len-4]== ' '){
    cout<<"Real Fancy";

// code to check for if statement starts with word not
else if ((str.find(str3) != string::npos) ) {
cout<<"Real Fancy";
else {
    cout<<"regularly fancy";
     return 0;

bu the output i got :

i got his problem here is that once i enter value for test cases like here 3
the compiler treats it as the first string and prints regularly fancy for this How to fix this new problem whic i encounter when i use getline ??

Reading T won’t read the rest of the line after T, so the next call to getline will read the remaining characters after T until the end of the line.

You need to skip the rest of the line after reading T:

    int T;

    // Skip to the next line.
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

After that, you’ll need to fix the potential out-of-bounds access on the line:

        else if (str[len-1]=='t' && str[len-2]== 'o' && str[len-3]== 'n' && str[len-4]== ' '){

what did you mean by this i have no clue ?

Consider the testcase:


i ran with this and the
output :
regularly fancy
isn’t that whats supposed to be he output ??

since i m not much familiar with getline() funnction i wanna kno i did a slight adjustment in the code earlier and got an error message
the small change i made was
int T;
getline(cin,T); // instead of cin>>T;

but i got an error while compiling which says :
main.cpp:12:18: error: no matching function for call to ‘getline(std::istream&, int&)’

can you explain why this happened and why this dint happen when i used getline to read a string ?

i got the answer its basically because :
std::getline() function is not for integer type. and i was using it to read an integer in the variable T

1 Like

in this basically i took two sets and stored n different integers(ranging from 1 to 10^9) into each of the two multiset data structures. and since multisets store values already sorted so i used a for loop and tried to tranverse in both sets from the beginning till the end and did a small job. But it exceeds time limit of 5 seconds on online judge.

Note : i used multiset as it sorts a data in O(logn) time.
So is the problem occuring because i m dealing with very big number of the order upto 10^9 ???

my piece of code :

#include <stdio.h>
#include <bits/stdc++.h> 
#include <cstdio>
using namespace std;

int main()
    int T;

    for(int k=0;k<T;k++)  {
    int a,n;
    int sum = 0;
   // cin>>n;   // n ranges from 1 to 10^9

    // multiset declare 
    multiset<int> s;
    for (int i =0;i<n;i++){
       // cin>>a;

    //multiset 2 declare
    multiset<int> s1;
    for (int i =0;i<n;i++){
     //   cin>>a;


    // Iterator declared to traverse 
    // set elements 
    multiset<int>::iterator it, it1; 

// now i want where iterator is look for values i the two sets and compare them and store the shorter of the two values in an int variable sum. 

    for (it = s.begin(), it1 = s1.begin(); it != s.end(); it++,it1++)  {
        (*it <= *it1 ) ? (sum=sum + (*it)) : (sum = sum + (*it1));
    return 0;

can you help why time limit is exceeding even when i m using multisets

I would say try using a regular array and then sort it before iterating through it.

but why isn’t multiset a good choice for this purpose ?