- 3

I've tried the following code but it gives a wrong output .....can somebody help me? Test case will be 3001213 n output is 300

#include <iostream> using namespace std; int main() { int num; cin>>num; int len= to_string(num).length(); int arr[10],lnum=0,max=0; for (int i =len-1;i>=0;i--) { arr[i] = num%10; num =num/10; //to convert int into array } for (int i=0;i<len-2;i++) { if (arr[i] >arr[i+1]) lnum = arr[i]*100+arr[i+1]*10+arr[i]; if (lnum>max) max= lnum; } cout<<max; return 0; }

6th Jul 2021, 5:53 AM
Disha Jain
3 ответов
0
Complete task Description?
6th Jul 2021, 6:03 AM
Ipang
0
Find largest 3 digit number within a numerical string Input 3001213 Output 300 Input 6478434 Output 843
6th Jul 2021, 6:06 AM
Disha Jain
0
you have two mistakes in your logic: 1) no need to check if (arr[i]>arr[i+1]) 2) using twice arr[i] in lnum computation (instead/lacks of arr[i+2]): lnum = arr[i]*100+arr[i+1]*10+arr[i+2]; https://code.sololearn.com/c7jqaoUJt141/?ref=app anyway, are you sure you must take input as number rather as string? (what if input overflow int capacity?)... as task say "numerical string" ^^
6th Jul 2021, 6:26 AM
visph
visph - avatar