Promi_38

prefix RMQ

Jan 9th, 2021
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. /* In this problem I will give you an array A of length n. You have to print maximum value for every prefix of that array.
  2.  
  3. Input Format
  4.  
  5. First line will contain a number n which indicate the length of the array.
  6.  
  7. Second line will contain n number Ai.
  8.  
  9. Output Format
  10.  
  11. In a single line print n integer. ith of which is the maximum value of ith prefix.
  12.  
  13. Sample Input 0
  14.  
  15. 5
  16. 5 4 10 2 1
  17. Sample Output 0
  18.  
  19. 5 5 10 10 10
  20. Explanation 0
  21.  
  22. Here,  prefix means [5] and here maximum value is 5.
  23.  
  24. 1st prefix means [5,4] and maximum value is 5.
  25.  
  26. 2nd prefix means [5,4,10] and maximum value is 10.
  27.  
  28. So our total answer will be: 5 5 10 10 10 */
  29.  
  30. #include<stdio.h>
  31.  
  32. int main()
  33. {
  34.     int n;
  35.     scanf("%d", &n);
  36.    
  37.     int a[n], i;
  38.     for(i = 0; i < n; i++) scanf("%d", &a[i]);
  39.    
  40.     int max = a[0];
  41.     for(i = 0; i < n; i++)
  42.     {
  43.         if(max < a[i]) max = a[i];
  44.         printf("%d ", max);
  45.     }
  46.     printf("\n");
  47. }
Advertisement
Add Comment
Please, Sign In to add comment