# Algorithm

Like how new recruits in the army are required to do a few pull-ups before lunch, I make myself complete at least 2 Codility challenges a week. If nothing, it helps to keep the mind thinking algorithmically - which I believe is a good thing.

This challenge requires one to cycle through a series of numbers in an array for N times. For example, given the array A = [3,8,9,7,6], to cycle this function once, it becomes A = [6,3,8,9,7]. Notice that the last element becomes the first, and then every other element moves 1 place up.

To cycle it twice, it becomes A = [7,6,3,8,9].

To cycle it thrice, it becomes A = [9,7,6,3,8].

In essence, you are removing the last element, and then moving every element up by 1 place, and then putting the last element back into the first place.

To achieve this, I used 2 loops, one to iterate through every element in the array and moving them up, and another one to do it n times.

The 2nd Codility lesson is to search for the odd number of elements in an array that cannot be paired with any other element. Codility provided this example.

A[0] = 9, A[1] = 3, A[2] = 9

A[3] = 3, A[4] = 9, A[5] = 7

A[6] = 9

Apart from A[5], every other element has a match. The first thought is to iterate through the array and look for the pair and remove both elements once it is found. When an element cannot find its pair, break from the loop and return the element. Simple enough, so I coded.

It works for a small number of elements but Codility needs this to run in less than 1 second when tested in big random sets with 999,999 elements. My solution runs in time for only tests with 2,001 elements or less.

And this become a revision for bitwise operators after reading the comments in Codility. This was the final answer, adapted from what another programmer shared.

Codility is where programmers receive programming tests as interview questions. This is also where programmers could learn to code. I am at lesson 1 on Iterations, and here's my answer. It finds the binary gap of a number. A binary gap is the longest sequence of 0 of a number converted to binary. For example, the number 5 is 101 is binary, and it contains 1 zero, so it's binary gap is 1. The number 1041 is 10000010001 in binary. It contains 2 sequences of 0, the first of which is 5 zeros long and the second is 0 zeros long. So its binary gap is 5, since the first sequence is longer than the first.

I got 100% and did it in 15 minutes and....2 tries. During my first try, I got excited and forgot to initialize my numZero variable back to 0 after it found each sequence of zeros. Careless me!