0

The constraints given in a question is as follows:

Constraints: 1<=T<=10^3, 1<=N<=10^7 0 <= Ai <= 10^18

Here T is the number of test cases, N is the number of elements in an array and Ai is the ith element in the array.

I want to declare something like:

int ar[100000000]

to satisfy the constraints but, it is not possible. Please help me.

sparrow
  • 27
  • 4

1 Answers1

0

int ar[100000000] as a local var probably explodes your stack, you can use a global var or malloc it to have it in the heap

The elements can reach 10^18 needed 60b, so use a type on 64b, probably long rather than int

Edit : as r3mus n0x says in a remark you need int ar[10000000] (length 10 times less)

If you are under Linux/Unix the stack size is given by ulimit -s

bruno
  • 32,421
  • 7
  • 25
  • 37
  • 1st: it has too many zeroes for 10^7, 2nd: you can definitely declare a global variable of that size, no allocation needed. – r3mus n0x Feb 23 '19 at 10:41
  • @r3musn0x oh yes 10^7 is only 10000000 not 100000000 as OP said, I didn't count well. Yes I know not alloc mandatory, this is why I also proposed a global var – bruno Feb 23 '19 at 10:44
  • Oh, sorry I misread your answer, also you have a typo there "var **of** malloc". – r3mus n0x Feb 23 '19 at 10:46
  • Thanks a lot. Using the dynamic memory instead of the static solved the problem. But can an I get an explanation of how did it work and if I was really to use and integer N, like 9546728(for instance), that would be 9546728*4 Bytes 9546728*4/1024 = 37291.90 mB. That would be out of scope of my memory. How would that problem be solved? – sparrow Feb 23 '19 at 11:07
  • @sparrow when you start a process/thread you have a default maximum stack size, and that size if less than the full memory size, stack does not have a dynamic size. What is your OS ? – bruno Feb 23 '19 at 11:12
  • @sparrow and `9546728*4/1024 = 37291.90 mB` you mean _kB_ or 37mB. Typicaly under Linux/Unix the stack size is 8 or 10 Mb – bruno Feb 23 '19 at 11:18
  • The OS is Windows 10. My question really is not forcefully using int as the data type, but can I use data types like char to implement the solution? To give you a context, the question is to sort an array, maximum size N(10^7) and the element size Ai(10^18). – sparrow Feb 23 '19 at 11:21
  • @sparrow all depends how you get the size of the array, you can use a fixe sized global array, or use _malloc_ if the size is known at running time. I do not understand `element size Ai(10^18)` you mean max element value ? – bruno Feb 23 '19 at 11:24
  • @bruno My bad in calculating the size. I am now getting what you're trying to say. – sparrow Feb 23 '19 at 11:24
  • @bruno yes. The maximum element value. – sparrow Feb 23 '19 at 11:25
  • @sparrow 10^18 need 60 bits, probably your _long_ are on 64b so use an array of _long_ – bruno Feb 23 '19 at 11:26
  • @bruno Thanks man for helping me out on the concepts and my mistakes. – sparrow Feb 23 '19 at 11:27
  • @sparrow sorry I don't see max element need probably _long_ before, I edited my answer for that – bruno Feb 23 '19 at 11:28