Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 581841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:37:24+00:00 2026-05-13T14:37:24+00:00

here’s a problem I’ve solved from a programming problem website(codechef.com in case anyone doesn’t

  • 0

here’s a problem I’ve solved from a programming problem website(codechef.com in case anyone doesn’t want to see this solution before trying themselves). This solved the problem in about 5.43 seconds with the test data, others have solved this same problem with the same test data in 0.14 seconds but with much more complex code. Can anyone point out specific areas of my code where I am losing performance? I’m still learning C++ so I know there are a million ways I could solve this problem, but I’d like to know if I can improve my own solution with some subtle changes rather than rewrite the whole thing. Or if there are any relatively simple solutions which are comparable in length but would perform better than mine I’d be interested to see them also.

Please keep in mind I’m learning C++ so my goal here is to improve the code I understand, not just to be given a perfect solution.

Thanks

Problem:

The purpose of this problem is to verify whether the method you are using to read input data is sufficiently fast to handle problems branded with the enormous Input/Output warning. You are expected to be able to process at least 2.5MB of input data per second at runtime. Time limit to process the test data is 8 seconds.

The input begins with two positive integers n k (n, k<=10^7). The next n lines of input contain one positive integer ti, not greater than 10^9, each.
Output

Write a single integer to output, denoting how many integers ti are divisible by k.
Example

Input:

7 3
1
51
966369
7
9
999996
11

Output:

4

Solution:

#include <iostream>
#include <stdio.h>
using namespace std;

int main(){
  //n is number of integers to perform calculation on
  //k is the divisor
  //inputnum is the number to be divided by k
  //total is the total number of inputnums divisible by k

  int n,k,inputnum,total;

  //initialize total to zero
  total=0;

  //read in n and k from stdin
  scanf("%i%i",&n,&k);

  //loop n times and if k divides into n, increment total
  for (n; n>0; n--)
  {
    scanf("%i",&inputnum);
    if(inputnum % k==0) total += 1;
  }

 //output value of total
 printf("%i",total);
 return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:37:24+00:00Added an answer on May 13, 2026 at 2:37 pm

    I tested the following on 28311552 lines of input. It’s 10 times faster than your code. What it does is read a large block at once, then finishes up to the next newline. The goal here is to reduce I/O costs, since scanf() is reading a character at a time. Even with stdio, the buffer is likely too small.

    Once the block is ready, I parse the numbers directly in memory.

    This isn’t the most elegant of codes, and I might have some edge cases a bit off, but it’s enough to get you going with a faster approach.

    Here are the timings (without the optimizer my solution is only about 6-7 times faster than your original reference)

    [xavier:~/tmp] dalke% g++ -O3 my_solution.cpp
    [xavier:~/tmp] dalke% time ./a.out < c.dat
    15728647
    0.284u 0.057s 0:00.39 84.6% 0+0k 0+1io 0pf+0w
    [xavier:~/tmp] dalke% g++ -O3 your_solution.cpp
    [xavier:~/tmp] dalke% time ./a.out < c.dat
    15728647
    3.585u 0.087s 0:03.72 98.3% 0+0k 0+0io 0pf+0w
    

    Here’s the code.

    #include <iostream>
    #include <stdio.h>
    using namespace std;
    
    const int BUFFER_SIZE=400000;
    const int EXTRA=30;  // well over the size of an integer 
    
    void read_to_newline(char *buffer) {
      int c;
      while (1) {
        c = getc_unlocked(stdin);
        if (c == '\n' || c == EOF) {
          *buffer = '\0';
          return;
        }
        *buffer++ = c;
      }
    } 
    
    int main() {
      char buffer[BUFFER_SIZE+EXTRA];
      char *end_buffer;
      char *startptr, *endptr;
    
      //n is number of integers to perform calculation on
      //k is the divisor
      //inputnum is the number to be divided by k
      //total is the total number of inputnums divisible by k
    
      int n,k,inputnum,total,nbytes;
    
      //initialize total to zero
      total=0;
    
      //read in n and k from stdin
      read_to_newline(buffer);
      sscanf(buffer, "%i%i",&n,&k);
    
      while (1) {
        // Read a large block of values
        // There should be one integer per line, with nothing else.
        // This might truncate an integer!
        nbytes = fread(buffer, 1, BUFFER_SIZE, stdin);
        if (nbytes == 0) {
          cerr << "Reached end of file too early" << endl;
          break;
        }
        // Make sure I read to the next newline.
        read_to_newline(buffer+nbytes);
    
        startptr = buffer;
        while (n>0) {
          inputnum = 0;
          // I had used strtol but that was too slow
          //   inputnum = strtol(startptr, &endptr, 10);
          // Instead, parse the integers myself.
          endptr = startptr;
          while (*endptr >= '0') {
            inputnum = inputnum * 10 + *endptr - '0';
            endptr++;
          }
          // *endptr might be a '\n' or '\0'
    
          // Might occur with the last field
          if (startptr == endptr) {
            break;
          }
          // skip the newline; go to the
          // first digit of the next number.
          if (*endptr == '\n') {
            endptr++;
          }
          // Test if this is a factor
          if (inputnum % k==0) total += 1;
    
          // Advance to the next number
          startptr = endptr;
    
          // Reduce the count by one
          n--;
        }
        // Either we are done, or we need new data
        if (n==0) {
          break;
        }
      }
    
     // output value of total
     printf("%i\n",total);
     return 0;
    }
    

    Oh, and it very much assumes the input data is in the right format.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 384k
  • Answers 384k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you mean syntactic nesting, then the answer is it… May 14, 2026 at 11:10 pm
  • Editorial Team
    Editorial Team added an answer Yes, there seems to be a bug in some older… May 14, 2026 at 11:10 pm
  • Editorial Team
    Editorial Team added an answer The answer was to add the tag within the tag.… May 14, 2026 at 11:10 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.