large data in C++

I am not an awesome coder and not really an algo guy.

But love coding small stuff…I have been trying out hands on competitive coding and been facing problems with handling large data. My codes solve problems for example test cases.

Please guide me a lil…

thnx :slight_smile:

1 Like

Try to use variables which can handle large data and optimize your code so as to obtain the answer in the stipulated time. Avoid using too many nested loops and function calls, if at all functions are needed declare them to be inline. Use fast read/write to read/write large data. Try obtaining the solution in least no. of steps. And go through some of the solutions to problems in practice section.

1 Like

The most important things to note while solving a problem are:

  1. Make sure that the algorithm that you are using for solving the problem has the time complexity according to the time limit specified in the problem. This step is very important.

Here is a discussion about time limits and the maximum looping in a program to avoid time limit exceed: Click here

  1. Make sure that the data types being used are according to the maximum possible values( given by the constraints of the problem), that particular variable can take during execution.

If the constraints on n are 1<=n<=10^18 and you are declaring n as int, it will lead to wrong answer, as the int does not have the space to store such a large value. So, you need to use long long int.

Here are a few things about handling large numbers in C++:

a) In c++ the data types long long int and unsigned long long int are enough for handling the upper bounds of most of the problems.

b) If still there is a need for storing more larger number you need to do it using an array.

For these kinds of problems,there will an array based algorithm for solving them.<Br>
For example,

The problem : small factorials uses an array for storing the factorial of numbers.

**The limit of long long int is -2^63+1 to +2^63-1, so by looking at the constraints, try and deduce, that the values should be stored in long long int or you will need an array for storage of value.**

Thnx a lot guys…I ll try out these suggestions…although i had been using long int, but that doesnt seem to really work…thanx again :slight_smile: :slight_smile:

Yeah everyone who starts competitive programming faces these difficulties with large data.

Remember these rules of thumb in c++ with data->

  • signed char: -127 to 127 (note, not
    -128 to 127; this accommodates 1’s-complement platforms)
  • unsigned char: 0 to 255
  • char: -127 to 127 or 0 to 255
    (depends on default char signedness)
  • short: 10^5
  • unsigned short: 10^5
  • int: 10^5
  • unsigned int: 0 to 10^5
  • long: 10^10
  • unsigned long: 0 to 10^10
  • long long: 10^18
  • unsigned long long: 0 to 10^19

for time complexity try to reduce no of loops or the no of times a loop runs.
To learn the best way of coding do practice problem and see the codes other programmers

Happy coding :slight_smile:

Please split it in different lines.this is looking messy and not readable.

tech_boy thank u for pointing it out .

thanks for efforts