Statistics

Problem Statement for "MountainRanges"

Problem Statement

Tom is in charge of a tourist agency. He has a lovely picture of the local mountain range. He would like to sell it to the tourists but first he needs to know how many peaks are visible in the picture.

The mountain range in the picture can be seen as a sequence of heights. You are given these heights as a int[] height. An element of height is called a peak if its value is strictly greater than each of the values of adjacent elements. Compute and return the number of peaks in the given mountain range.

Definition

Class:
MountainRanges
Method:
countPeaks
Parameters:
int[]
Returns:
int
Method signature:
int countPeaks(int[] heights)
(be sure your method is public)

Constraints

  • heights will contain between 1 and 50 elements, inclusive.
  • Each element of heights will be between 1 and 100, inclusive.

Examples

  1. {5, 6, 2, 4}

    Returns: 2

    Element 1 (0-based index) is a peak. Its height is 6, which is strictly greater than each of its neighbors' heights (5 and 2). Element 3 is also a peak since its height is 4 and that is strictly greater than its neighbor's height (which is 2).

  2. {1, 1, 1, 1, 1, 1, 1}

    Returns: 0

    This is a very flat mountain with no peaks.

  3. {2, 1}

    Returns: 1

    Element 0 is a peak.

  4. {2,5,3,7,2,8,1,3,1}

    Returns: 4

    The peaks here are the elements with 0-based indices 1, 3, 5, and 7. Their heights are 5, 7, 8, and 3, respectively.

  5. {1}

    Returns: 1

    Element 0 is a peak. Even though it has no neighbors, the condition from the problem statement is still satisfied.

  6. {1,2,3,4,4,3,2,1}

    Returns: 0

    According to our definition there is no peak in this mountain range.

  7. {63,97,2,40,38,22,1,68,85,59,48,53,23,23,72,86,97,11,93,76}

    Returns: 6

  8. {52,76,51,40}

    Returns: 1

  9. {63,89,39,87,72,92,18,24,99,32,20,55,99}

    Returns: 5

  10. {39,64,74,51,49,55,13,90,48,77,39,9,96,32,2,3,92,53,73,7,14,6,11,82,93,84,78,74,20,63}

    Returns: 10

  11. {29,99,98,74,31,11,67,1,87,79,42,48,35,77,63,82,63,45,2,82,37,44,67,84,98,30,65,69,45,51,58,26,78,69,64,77,48,37,41,98,74,33,62,23,56,17,24,52,43,44}

    Returns: 17

  12. {82,57,45,29,91,52,48,33,20,95,100,51,5,92,28,51,44,27,58,11,9,94,4,9,8,99,23,76,80,70}

    Returns: 10

  13. {84,94,72,24,46,86,39,47,10,91,60,20}

    Returns: 4

  14. {75,6,7,11,20,84,53,46,57,10,63,52,83,41}

    Returns: 5

  15. {52,89,60,82,97,26,44,49,53,61,16,55,67,66,95,47,47,35,95,81,62,100,70,23}

    Returns: 7

  16. {80,4,7,73,95,56,18,71,16,26,99,97,59,54,28,91,58,16,84,52,95,37,38,70,7,89,85}

    Returns: 9

  17. {23,38,83,19,58,92,34,94,11,93}

    Returns: 4

  18. {73,94,99,54,5,49,76,17,50,4,78,20,33,54,95,38,8,32,12,60,50,97,36,92,54,56,76,47,48,91,72,19,85,52,87,90,25,64,86,60,39,73,6,34,72}

    Returns: 16

  19. {58,70,100,85,48,21,48,42,23,92,87,4,10,72,53,89,9,75}

    Returns: 6

  20. {45,47,94,44}

    Returns: 1

  21. {89,43,78,74,56,23,25,67,32,53,14,18,30,53,40,60,19,86,63,91,1,15}

    Returns: 9

  22. {86,68,61,33,14,93,3,39,77,82,83,23,57,42,70,63}

    Returns: 5

  23. {4,40,72,28,56,28,19,30,63,30,81,90,28,32,41,34,32,72,76,61,17}

    Returns: 6

  24. {26,15,26,33,4,53,68,30,19,91,29,85,100,49,28,48,89}

    Returns: 6

  25. {16,59,49,4,87,33,34,38,2,69,43,54,47,42,22,99}

    Returns: 6

  26. {20,2,36,90,48,63,68,38,34,6,67,17,16,53,87,70,33,66,46,65,5,45,54,25,85,18,60,20}

    Returns: 10

  27. {40}

    Returns: 1

  28. {10,5,36,3,98,90,98,65,80,39,79,65}

    Returns: 6

  29. {27,13,31,100,6,28,31,33,61,2,20,42,53,78,96,89,9,2,18,87,61,89,93,27,11,82,51}

    Returns: 7

  30. {2,18,91,44,92,97,11,23,27,64,2,69,18,56,4,58,59,28,11,45,22,84,49,27,92,49,17,86,39,79,38,24,36,77,47,5,75,18,2,100}

    Returns: 14

  31. {90,79,6,94,50,80,48,19,67,90,77,8,78,62,13,61,79,22,22,91,27,65,80,62,46,8,36,68,48,56,46}

    Returns: 10

  32. {91,31,100,38,96,33,76,83,78,85,40,29,88,33,4,49,14}

    Returns: 7

  33. {98,6,42,18,25,18,51,67,4,80,27,70,79,5,29,77,26,73,89,85,49,65,36,69,65,47,45,54,94,45,77,76,9,13,34,66,10,4}

    Returns: 13

  34. {95,59,61,86,82,86,67,76,9,3,77,45,44,85,94,51,14,84,58,47,39,85,51,27,93,8,14,60,27,48,47,28,43,77,38,7,92,15,66,69}

    Returns: 14

  35. {36,81,53,83,20,90,30,98,81,43,28,60,99,15,26,25,71,99}

    Returns: 7

  36. {55,27,91,86,50,21,95,98,7,93,46,95,63,89,93,71,41,25,75,64,36,72,56,47,3,7,97,34,83,75,83,25,56,41,13,26,92,34,55,99,12,71,24,94,91,83,51}

    Returns: 16

  37. {51,62,93,45,31,4,24,6,49,82,48,33,7,12}

    Returns: 4

  38. {80,25,59,64,41,45,52,8,10,63,93,87,95,31,14,94,60,88,53,67,99,56,88,83,92,87,84,19,65,98,20,32,75,13}

    Returns: 12

  39. {34,86,81,45}

    Returns: 1

  40. {45,12,84,13,74,64,1,66,76,49,80,78,54,59,45,32,41,41,61,100,4,81,41,42,80}

    Returns: 9

  41. {83,45,57,82,81,52,12,48}

    Returns: 3

  42. {16,29,7,55,40,38,55,14,65,29,92,87,14,32,32,83,76,54,92,6,88,25,83,91,61,95,83,18,58,16,70,33,23,96,13,79}

    Returns: 14

  43. {73,75,58,57,40,67,20,25,5,60,94,12,63,83,45,48,19,65,2,38,7,43,15,97,88,80,53,66,75,98,87,47,6}

    Returns: 11

  44. {27,22,49,34,69,43,97,12,97,64,74,68,17,79,96,73,47,79,20,30,77,35,40,5,25,6,89,85,50,43,26,15,42,51,18,87,33,84,13,93}

    Returns: 16

  45. {11,88,74,72,23,91,67,22,34,57,45,16,36,43,45,69,82,69,80,67,91,64,11,58,56,3,94,61}

    Returns: 8

  46. {16,86,30,16,63,35,33,49,37,73,27,11,35,61,43,60,31,38,69,78,56,69,62,57,8,99,13,99,66,50,53,12,44,35,25,12,32,97,5,13,86,21,30}

    Returns: 15

  47. {93,93,82,83,32,59,47,26}

    Returns: 2

  48. {45,4,11,39,72,1,49,11,60,92,84,51,51,74,8,50,59,9,50}

    Returns: 7

  49. {6,51,22,44,84,9,38,18,40,53,21,98,51,57,27,85,53,70,54,52,39,90,39,75,48,48,24,59,30,42}

    Returns: 12

  50. {19,83,76,58,49,31,20,83,88,15,84,75,13,60,45,9,15,5,52,51,97,48,73,8,25,97,82,56,87,16,56,52,64,77,91,22,17,21,17,100}

    Returns: 14

  51. {88,27,86,44,66,53,41,41,51,6,90,65,43,76,25,5,12,31,52,65,35,73,3,56,24,47,28,34,100,29,9,48,35,26,32}

    Returns: 13

  52. {15,46,58,61,30,94,87,20,58,76,57,48,57,51,28,74,47,15,46,66,34,45,67,11,66,42,14,24,17,76,98,79,49,17,54,11,71,83,4}

    Returns: 12

  53. {9,41,62,87,78,8,99,10,19,62,83,45,81,46,98,61,84,76,70,86}

    Returns: 7

  54. {43,20,79,80,56,1,58,95,67,53,98,24,10,67,45}

    Returns: 5

  55. {1,39,87,8,37,43,25,15,15,66,2,34,64,79,21,71,2,58,73,92,48,99,5,43,16,21,86,96,92,80,52}

    Returns: 9

  56. {25,68,46,78,18,44,41,6,72,39,77}

    Returns: 5

  57. {2,2}

    Returns: 0

  58. {3,2,1}

    Returns: 1

  59. {100,99,100}

    Returns: 2

  60. {99,100,99}

    Returns: 1

  61. {2,3,2,3,1,2,2,3,3}

    Returns: 2

  62. {3,2,2}

    Returns: 1

  63. {1,2,2,2,3,2,3,3,2,2,3,3,3,1,1,2,3,1,1,2,2,3,3,2,2,1,1,1,3,3,1}

    Returns: 2

  64. {1,2,3,2,3,1,1,2,3,3,1,3,2,3,1,2,1,1,2,3,2,2,3,2,3,3,3,2,2,3,1,2,1,3,1,1,1,2,3,2,1,3}

    Returns: 12

  65. {3,1,3,3,1,1,1,1,3,2,1,1,1,3,1,1,2,2,2}

    Returns: 3

  66. {3,2,1,1,1,2,1,1,3,2,2,1,3,1,3,3,1,3,3,2,3,3,1,1}

    Returns: 4

  67. {2,1,2,1,3,1,1,3,2,1,3,2,2,2,3,3,2,3,2,3,3,2,2,2,2,1,3,3,2,1,1,1}

    Returns: 6

  68. {2,2,3,1,3,3,3,3,3,3,3,3,3}

    Returns: 1

  69. {1,2,3,2,2,1,1,2,3,2,1,1,3,2,2,2,1,3,2,2,1,1,3,3,3,3,3,3,2,3,1,2,2,1,3,3,3,2}

    Returns: 5

  70. {2,3,3,2,3,1,3,3,1,1,3,2,2,3,1,2,3,1,3,1,3,2,2}

    Returns: 6

  71. {1,3,3,1,3,3,2,3,2,2,3,2,3,3,2,2,3,3,2,1,1}

    Returns: 2

  72. {1,1,1,1}

    Returns: 0

  73. {1,1,3,3,1,1}

    Returns: 0

  74. {3,3,2,3,1,1,2,1,2,3,2,2,1,1,3,3,1,2,1,2,3,3,1,2,2,1}

    Returns: 4

  75. {1, 2, 3, 1, 3, 4, 5, 1, 6, 8, 1, 9, 15, 6, 18 }

    Returns: 5

  76. {1 }

    Returns: 1

  77. {4 }

    Returns: 1

  78. {1, 2, 3, 4, 4, 3, 2, 1 }

    Returns: 0

  79. {5 }

    Returns: 1

  80. {2, 2 }

    Returns: 0

  81. {2, 1, 1, 1, 2 }

    Returns: 2

  82. {2, 1, 2 }

    Returns: 2

  83. {5, 5 }

    Returns: 0

  84. {2, 1 }

    Returns: 1

  85. {1, 1 }

    Returns: 0

  86. {99 }

    Returns: 1

  87. {11, 1, 11, 1, 11, 1, 11, 1 }

    Returns: 4

  88. {2, 1, 1 }

    Returns: 1

  89. {42 }

    Returns: 1

  90. {10, 9, 8, 7, 6, 4, 3, 2, 1 }

    Returns: 1

  91. {3, 2, 3 }

    Returns: 2

  92. {5, 6, 7, 8, 7, 6, 7, 6, 7, 6, 6 }

    Returns: 3


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: