Problem with variable size

How i can store 10^9 elements in an array. if i declare int arr[1000000000], will it work in codechef?

Probably not.

CodeChef allows 64MB of storage to be used by your code as it runs:

Assuming those ints are 32-bit ints, that array would take up (32/8)*(10^9) bytes, which is 4^9 bytes. One megabyte is 2^20. Divide the 4^9 bytes by 2^20 bytes per megabyte, and you get about 3.81 x 10^3 MB. Way too much for CodeChef.

In this situation, you usually have to do the converse of what you’ve always been told to do. You’ve probably been told to trade space for time; well, you’ve used too much space. You have to trade time for space.

To relate to the question title, there is a way to make a variable length array. In C++, it’s called a vector. In Java, it’s called an ArrayList. You can google for implementations of those, but both of those are variable-length arrays in the respective languages.

If you choose not to use those variable-length arrays, you have to optimize your space.

@kullalok Dynamic memory allocation will work or not???

Sorry, I forgot to mention that. I was in a rush.
Dynamic memory takes up more space than static, even if both are holding the same number and type of elements. So, @dhiraj74, you’re going to need to optimize for space.