streing to int array

which is the fastest method to convert string to integer array?
in a question iam given integers as input without spaces… i need to access each one of them… iam storing them into string and then converting it into int array by subtracting each element of the string from ‘0’… but iam getting tle… what is the faster approach?

1 Like

ur doing the fastest approach which takes an O(string length) time which is not going to affect running time of ur program .try to improve other operation…BUT if u want to improve little bit u can use
getchar_unlocked() to read each character one by one and store the c-‘0’(c is the character input) in integer array.

I doubt tle is because of input.

Either way a better way to do what you’re trying to do is by taking char input into a temporary char and storing it directly.

Int ar[100];
For(i=0;i<100;i++)
{
char c;
cin>>c;
ar[i]=c-'0';
}

This won’t be any faster but it will save memory. You could use a fast input method instead of cin>>c; to make it go faster. However most tle’s are because of inefficient algorithms.if it’s a tle because of input/output, scanf printf will fix that

thats really so simple …:stuck_out_tongue:

thanks :slight_smile:
but can u please provide me the code as i havent used getchar unlocked before and the code which i found on google gives me several errors which i am unable to resolve…

i googled before asking it in the forum… and i tried to implement several codes which i found on google in my code… but it gives me errors which iam not able to resolve… so i thought of discussing it here

ok. :slight_smile: i will try to optimize my algorithm then :slight_smile: