Soham_K

CSE102

Mar 12th, 2020 (edited)
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     int x, max=-1,sum=0,pos=0,s,i=0,j,t,flg=0;
  6.  
  7.     char c;
  8.     while(scanf(" %c", &c)!=0)
  9.     {
  10.         if(c == 'q')
  11.             break;
  12.         if(flg==1)
  13.         {
  14.             flg = 0;
  15.             x = c - '0';
  16.             x = -1*x;
  17.         }
  18.         else if(c == '-')
  19.         {
  20.             flg=1;
  21.         }
  22.         else
  23.         {
  24.             x = c - '0';
  25.         }
  26.  
  27.         if(c>='0' && c<='9')
  28.         {
  29.             if(x > 0)
  30.             {
  31.                 t=1;
  32.                 pos++;
  33.                 sum=sum+x;
  34.                 i++;
  35.  
  36.             }
  37.             else if(x<0)
  38.             {
  39.                 t=0;
  40.                 if(max < pos)
  41.                 {
  42.                     max = pos;
  43.                     s = sum;
  44.                     j=i-max;
  45.                     sum=0;
  46.                     pos=0;
  47.  
  48.                 }
  49.                 i++;
  50.                 pos=0;
  51.                 sum=0;
  52.  
  53.             }
  54.         }
  55.  
  56.  
  57.     }
  58.  
  59.  
  60.     if(t==1)
  61.     {
  62.         max = pos;
  63.         s = sum;
  64.         j=i-max;
  65.     }
  66.     printf("Length of LPS:%d\nSum of LPS:%d\nStarting Position of LPS:%d(Assuming that position starts from 0)",max,s,j);
  67.  
  68.  
  69.  
  70.     return 0;
  71. }
Add Comment
Please, Sign In to add comment