Skip to main content

Posts

Solved Counting Valleys Hacker Rank

Challenge: Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike he took exactly   steps. For every step he took, he noted if it was an  uphill ,  , or a  downhill ,   step. Gary's hikes start and end at sea level and each step up or down represents a   unit change in altitude. We define the following terms: A  mountain  is a sequence of consecutive steps  above  sea level, starting with a step  up  from sea level and ending with a step  down  to sea level. A  valley  is a sequence of consecutive steps  below  sea level, starting with a step  down  from sea level and ending with a step  up  to sea level. Given Gary's sequence of  up  and  down  steps during his last hike, find and print the number of  valleys  he walked through. For exampl...

Solved Sock Merchant Hacker Rank

Challenge: John works at a clothing store. He has a large pile of socks that he must pair by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. For example, there are   socks with colors  . There is one pair of color   and one of color  . There are three odd socks left, one of each color. The number of pairs is  . Function Description Complete the  sockMerchant  function in the editor below. It must return an integer representing the number of matching pairs of socks that are available. sockMerchant has the following parameter(s): n : the number of socks in the pile ar : the colors of each sock Input Format The first line contains an integer  , the number of socks represented in  . The second line contains   space-separated integers describing the colors   of the socks in the pile. ...