Coding Challenges

HackerRank

Sales by Match

https://www.hackerrank.com/challenges/sock-merchant/problem 

There is a large pile of socks that must be paired by color. Given an array of integers representing the colour of each sock, determine how many pairs of socks with matching colours there are.

Example

n=7

ar = 1 2 1 2 1 3 2

There is one pair of color 1 and one of color 2. There are three odd socks left, one of each color. The number of pairs is 2.

Sample Input

9

10 20 20 10 10 30 50 10 20

Sample Output

3

Solutions

{sum floor {x%2}count each group x}


{sum floor 0.5*1+sum each 1_'(where differ asc x) cut not differ asc x}

Counting Valleys

https://www.hackerrank.com/challenges/counting-valleys/problem 

An avid hiker keeps meticulous records of their hikes. During the last hike that took exactly  steps steps, for every step it was noted if it was an uphill, U, or a downhill, D step. Hikes always start and end at sea level, and each step up or down represents a 1 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 the sequence of up and down steps during a hike, find and print the number of valleys walked through.

Example

steps = 8 path = D D U U U U D D

 The hiker first enters a valley 2 units deep. Then they climb out and up onto a mountain 2 units high. Finally, the hiker returns to sea level and ends the hike.

Solutions

{count  where 0<abs min each neg[1],(where 0 = sums map x) cut (sums map x)}