Memory problem

how i create an array of 10^9 and store elements on each index

use long long instead of int .

yes i used long long int
but in case of
for(i to 10^9)
a[i]=i

it gives run time error

You cannot store it in an array. Use a vector instead. Its max capacity is 4611686018427387903.

should it depends on machine like 32 bit or 64

in case of long long definitely u can use but there are other reasons for run time error like Invalid memory access during run-time

I wonder why no one mentioned abt maps
whenever u feel like u need large arrays then the ans is map

2 Likes

can you provide some content for abt maps from where i learn it

@ganesh92 yes map is best option .


there is lot of resource on internet just google it.

1 Like

geeksforgeeks
cppreference
these are my fav sites for c++

remember u cannot traverse all 10^9 elements coz then u will easily cross ur user id i.e time limit :smile:

traversing 10^8 elements takes 1 sec
then 10^9 elements will require 10sec
hence even if u could store 10^9 elements in an array, u won’t be able to traverse it

1 Like

ohh thanks a lot it help me a lot thanks all of you specifically ganesh92
time limit is best name i think​:joy::joy:

also thanks knakul853 for providing that link

this works with 10^8 not with 10^9 why??

#include <iostream>
#define SIZE 100000000
typedef unsigned long long int ulli;
ulli a[SIZE];
int main() {
  for (ulli i = 0; i < SIZE; ++i) {
    a[i] = i;
  }

  std::cout << a[99999998] << "\n";
  std::cout << a[99999999] << "\n";
}

see @ganesh92 comment

1 Like