Advertisement
SuitNdtie

Max Sequence PROG 1005

Mar 9th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. //Source #643521:
  2. #include<stdio.h>
  3.  
  4. #define IM -1e9
  5. int main()
  6. {
  7.     int n;
  8.     scanf("%d",&n);
  9.     int arr[n];
  10.     int temparr[n];
  11.     int cot = 0;
  12.     int temp = IM;
  13.     int ans = IM;
  14.     int ansarr[n];
  15.     int coa = 0;
  16.     for(int i=0;i<n;i++){
  17.         scanf("%d",&arr[i]);
  18.         if(temp+i >= i){ //continue
  19.             temp = temp+arr[i];
  20.            
  21.             temparr[cot] = arr[i];
  22.             cot++;
  23.         }else{ //new
  24.             temp = arr[i];
  25.            
  26.             cot = 0;
  27.             temparr[cot] = arr[i];
  28.             cot++;
  29.         }
  30.         if(temp>ans){
  31.             ans = temp;
  32.             coa = cot;
  33.             for(int i=0;i<cot;i++){
  34.                 ansarr[i] = temparr[i];
  35.             }
  36.         }
  37.     }
  38.     if(coa == 0 || ans <= 0){
  39.         printf("Empty sequence");
  40.     }
  41.     else{
  42.         for(int i=0;i<coa;i++){
  43.             printf("%d ",ansarr[i]);
  44.         }
  45.         printf("\n%d",ans);
  46.     }
  47.    
  48. }
  49. /*
  50. 12
  51. 4 -6 3 -2 6 -4 -6 6 -6 4 -2 5
  52.  
  53. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement