fork download
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Main
  5. {
  6. public static void main(String[] args) {
  7. Scanner sc = new Scanner(System.in);
  8. int n=sc.nextInt();
  9. int[] nums = new int[n];
  10. for(int i=0;i<n;i++){
  11. nums[i]=sc.nextInt();
  12. }
  13. //mountain
  14.  
  15. int[] pref= new int[n];
  16. int[] suf = new int[n];
  17.  
  18. pref[0]=1;
  19. for(int j=1;j<n;j++){
  20. if(nums[j-1] < nums[j]){
  21. pref[j]=pref[j-1]+1;
  22. }
  23. else{
  24. pref[j]=1;
  25. }
  26. }
  27.  
  28. suf[n-1]=1;
  29. for(int j=n-2;j>=0;j--){
  30. if(nums[j]>nums[j+1]){
  31. suf[j]=suf[j+1]+1;
  32. }
  33. else{
  34. suf[j]=1;
  35. }
  36. }
  37. int count=0;
  38. for(int j=1;j<n-1;j++){
  39. count+=((pref[j]-1)*(suf[j]-1));
  40. }
  41. System.out.print("Total number of mountains : " + count);
  42. int res=Integer.MIN_VALUE;
  43. for(int j=1;j<n-1;j++){
  44. int len = pref[j]+suf[j]-1;
  45. res=Math.max(res,len);
  46. }
  47. System.out.print("Longest length of the mountain: " + res);
  48. }
  49. }
Runtime error #stdin #stdout #stderr 0.1s 56652KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.util.NoSuchElementException
	at java.base/java.util.Scanner.throwFor(Scanner.java:937)
	at java.base/java.util.Scanner.next(Scanner.java:1594)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at Main.main(Main.java:8)