## The Power Sum

Find the number of ways that a given integer, X, can be expressed as sum of the Nth powers of unique, natural numbers.

Input format:
The first line contains an integer X
The second line contains an integer N

Constraints:
1 <= X <= 1000
2 <= N <= 10

Output format:
Output a single integer, the answer to the problem explained above.

Sample Input 0

10
2


Sample Output 0

1


Explanation 0

If X = 10 and N = 2, we need to find the number of ways that 10 can be represented as the sum of squares of unique numbers.
10 = 1^2 + 3^2
This is the only way in which 10 can be expressed as the sum of unique square.

Sample Input 1

100
2

Sample Output 1

3

Explanation 1

100 = 10^2 = 6^2 + 8^2 = 1^2 + 3^2 + 4^2 + 5^2 + 7^2

Sample Input 2

100
3

Sample Output 2

1

Explanation 2

100 can be expressed as the sum of the cubes of 1,2,3,4.
(1 + 8 + 27 + 64) = 100. There is no other way to express 100 as the sum of cubes.

Approach 1:
With X = 10, and N = 2, this approach will generate sequences like:
[1]
[1, 4]
[1, 4, 9]
[1, 9]
[4]
[4, 9]
[9]
[9, 4]
and you can easily spot out the problem of this approach is it produces duplicated sequences. Here is the code:
def powerSum1(X, N):
max_base = int(math.sqrt(X))
sequence = list()
ways = set()
base = 1
_recursive_sum1(X, N, base, max_base, sequence, ways)
return len(ways)

def _recursive_sum1(X, N, base, max_base, sequence, ways):
if sum(sequence) == X:
# if set(sequence) not in ways:
return False
elif sum(sequence) > X:
return False
else:
for i in range(base, max_base+1):
tmp_val = i**N
if tmp_val not in sequence:
sequence.append(tmp_val)
print(sequence)
if _recursive_sum1(X, N, base+1, max_base, sequence, ways):
return True
sequence.pop()
return False

If you remove the comment from the if statement, indent 1 tab to the right for the line above, it will work perfectly, but only for all possible small-enough numbers. Because it generates on possible sequences, sequences that are subsequently increment.
Approach 2:
This recursion type generate non-repeated sequences that are subsequently increment.
1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 6]
[1, 2, 3, 4, 7]
[1, 2, 3, 4, 8]
[1, 2, 3, 5]
[1, 2, 3, 5, 6]
[1, 2, 3, 5, 7]
[1, 2, 3, 6]
[1, 2, 3, 6, 7]
[1, 2, 3, 7]
[1, 2, 3, 8]
[1, 2, 3, 9]
[1, 2, 4]
def powerSum(X, N):
sequence = list()
base = 1
look_up = [0]*(101)
for i in range(1, 101):
look_up[i] = i**N
cur_sum = 0
ways = _recursive_sum(X, N, base, look_up, cur_sum, sequence)
return ways

def _recursive_sum(X, N, base, look_up, cur_sum, seq):
ways = 0
tmp_val = look_up[base]

while cur_sum + tmp_val < X:
seq.append(base)
print(seq)
ways += _recursive_sum(X, N, base+1, look_up, cur_sum+tmp_val, seq)
seq.pop()
base += 1
tmp_val = look_up[base]

if cur_sum + tmp_val == X:
ways += 1
seq.append(base)
seq.pop()
return ways


## Problem: Connected Cells in a Grid (Python3)

Consider a matrix with n rows and m columns, where each cell contains either a 0 or a 1 and any cell containing a 1 is called a filled cell. Two cells are said to be connected if they are adjacent to each other horizontally, vertically, or diagonally; in other words, cell [i][j]  is connected to cells [i-1][j-1], [i-1][j], [i-1][j+1], [i][j+1], [i+1][j+1], [i+1][j], [i+1][j-1], and [i][i-1], provided that the location exists in the matrix for that [i][j].

If one or more filled cells are also connected, they form a region. Note that each cell in a region is connected to zero or more cells in the region but is not necessarily directly connected to all the other cells in the region.

Given an nxm matrix, find and print the number of cells in the largest region in the matrix. Note that there may be more than one region in the matrix.

Input Format

The first line contains an integer, n, denoting the number of rows in the matrix.
The second line contains an integer, m, denoting the number of columns in the matrix.
Each line i of the n subsequent lines contains m space-separated integers describing the respective values filling each row in the matrix.

Constraints

0 < n,m < 10

Output Format

Print the number of cells in the largest region in the given matrix.

Sample Input

4
4
1 1 0 0
0 1 1 0
0 0 1 0
1 0 0 0


Sample Output

5


Explanation

The diagram below depicts two regions of the matrix; for each region, the component cells forming the region are marked with an X:

X X 0 0     1 1 0 0
0 X X 0     0 1 1 0
0 0 X 0     0 0 1 0
1 0 0 0     X 0 0 0


The first region has five cells and the second region has one cell. Because we want to print the number of cells in the largest region of the matrix, we print 5.

Approach:
Since the constrains are small, we can use brute-force algorithm. The hole algorithm can be described in three lines.

With each cell in the grid:
—- if it is 1 and not visited:
——– get all of it neighbors into a group (if any)

We need to implement the hard part. We need a groups list to store a list of groups (this is optional, you can use a list of group’s size), a collection to store visited cell (this should be set because in Python, O(1) is time complexity requires to check if a value in a set). We explore all 8 directions of a given cell, recursively call that method until we cant explore anymore. The algorithm has time complexity O(nxm), since we will never revisit a cell that is added into a group. Each cell is ensured to be visited only one.

EIGHT_DIRECTIONS = [
(-1, -1),
(-1, 0),
(-1, 1),
(0, 1),
(1, 1),
(1, 0),
(1, -1),
(0, -1)
]

def explore(grid, x, y, visited, groups):
groups[-1].append((x, y))
for i, j in EIGHT_DIRECTIONS:
next_x = x + i
next_y = y + j
if in_bound(grid, next_x, next_y) and \
grid[next_x][next_y] == 1 and (next_x, next_y) not in visited:
explore(grid, next_x, next_y, visited, groups)

def in_bound(grid, i, j):
return i >= 0 and i < len(grid) and \
j >= 0 and j < len(grid[0])

def connected_cells(grid, visited, groups):
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 1 and (i,j) not in visited:
groups.append(list())
explore(grid, i, j, visited, groups)

def largest_region(groups):
return max(len(group) for group in groups)

if __name__ == '__main__':
n = int(input())
m = int(input())
grid = list()
for i in range(n):
line = input()
grid.append([int(x) for x in line.strip().split()])
groups = list()
visited = set()
connected_cells(grid, visited, groups)
# for group in groups:
#     print(group)
print(largest_region(groups))


## Eight queens problem, backtracking, Python3

• Problem:

Given an 8×8 board, your job is to place 8 queens on the board such that none of them can attack each other (If you are not familiar with chess, then google it first). The problem can be more general, given a n x n (n > 3) board, place n queens on the board such that there is no way a queen can attack the others. There are 92 solutions to this problem.

• Solutions

The way to solve this problem is already described in the title. Here’s let solve it step by step. First let’s try some thoughts on a small board to see if we can apply the approach which we are about to come up with on small board to a wider one.

To develop an algorithm for this problem, we can first study a smaller instance of the problem by using just four queens and a 4 * 4 board. How would you go about solving this smaller problem? You may attempt to randomly place the queens on the board until you find a solution that may work for this smaller case. But when attempting to solve the original eight-queens problem, this approach may lead to chaos.

Consider a more organized approach to solving this problem. Since no two queens can occupy the same column, we can proceed one column at a time and attempt to position a queen in each column. We start by placing a queen in the upper-left square or position (0, 0) using the 2-D array notation:

With this move, we now eliminate all squares horizontally, vertically, and diagonally from this position for the placement of additional queens since these positions are guarded by the queen we just placed.

With the first queen placed in the first column, we now move to the second column. The first open position in this column where a queen can be placed without being attacked by the first queen we placed is at position (2,1). We can place a queen in this position and mark those squares that this queen guards, removing yet more positions for the placement of additional queens.

We are now ready to position a queen in the third column. But you may notice there are no open positions in the third column. Thus, the placement of the first two queens will not result in a valid solution. At this point, you may be tempted to remove all of the existing queens and start over. But that would be a drastic move.

We first return to the second column and try alternate positions for that queen before possibly having to return all the way back to the first column.
The next step is to return to the second column and pick up the queen we placed at position (2,1) and remove the markers that were used to indicate the squares that queen was guarding.
We then place the queen at the next available square within the same column (3, 1) and mark the appropriate squares guarded from this position, as shown here:
Now we can return to the third column and try again. This time, we place a queen at position (1, 2), but this results in no open squares in the fourth column.
We could try other squares in the same column, but none are open. Thus, we must pick up the queen from the third column and again backtrack to try other combinations. We return to the second column and pick up the queen we placed earlier at position (3, 1) so we can try other squares within this column.

But there are no more open squares in the second column to try, so we must back up even further, returning to the first column. When returning to a column, the first step is always to pick up the queen previously placed in that column.

After picking up the queen in the first column, we place it in the next position (1, 0) within that column.

We can now repeat the process and attempt to find open positions in each of the remaining columns. These final steps, which are illustrated here, results in a solution to the four-queens problem.

Having found a solution for the four-queens problem, we can use the same approach to solve the eight-queens problem. The only difference between the two is that there is likely to be more backtracking required to find a solution. In addition, while the four-queens problem has two solutions, the eight-queens problem has 92 solutions.

The original problem definition only considered finding a solution for a normal 8 * 8 chessboard. A more general problem, however, is known as the n-queens problem, which allows for the placement of n queens on a board of size n*n where n > 3. The same backtracking technique described earlier can be used with the n-queens problem, although finding a solution to larger-sized boards can be quite time consuming.

The possible data structures for representing the actual board.

The most obvious choice is a 2-D array of size n * n. The elements of the array can contain boolean values with True indicating the placement of the queens. To determine if a given square is unguarded, loops can be used to iterate over all of the squares to which a queen can move from that position. If a queen is found in any of the squares searched during the loop iterations, then we know the square is currently guarded by at least one queen. The placement and removal of the queens is also quite easy to implement using the 2-D array structure.

As an alternative, we can actually use a 1-D array consisting of n elements. Each element of the 1-D array corresponds to one column on the board. The elements of the 1-D array will contain row indices indicating the positions of the queens on the board.

Since only one queen can be placed within a given column, we need only keep track of the row containing the queen in the column. When determining if a square is unguarded, we can iterate through the row and column indices for the preceding columns on the board from which the given square can be attacked by a queen. Instead of searching for a True value within the elements of a 2-D array, we need only determine if the elements of the 1-D array contain one of the row indices being examined.

When searching horizontally backward, we examine the elements of the 1-D array looking for an index equal to that of the current row. If one is found, then there is a queen already positioned on the current row as is the case in the above example. If a queen was not found on the current row, then we would have to search diagonally to the upper left and to the lower left. We only search in this two directions because we place the queen from left to right, column by column. In these two cases, we search the squares indicated by the arrows and examine the row indices of each and compare them to the entries in the 1-D array. If any of the indices match, then a queen is currently guarding the position and it is not a legal move.

Code using backtracking to solve n-queens problem.

from board import Board

def solve_n_queens(board, col, ways):
# check if n-queens has been placed on the board
# base case
if board.num_queens() == board.size():
ways.append([x for x in board._board])
return False
else:
for row in range(board.size()):
if board.unguarded(row, col):
# place a queen in that square
board.place_queen(row, col)
# continue placing queens in the following columns
if solve_n_queens(board, col+1, ways):
# we are finished if a solution was found
return True
else:
# no solution was found with the queen in this square
# so it has to be removed from the board
# and try the next row in this col
if board._board[0] == 1:
print(board._board)
board.remove_queen(row, col)

# If the loop terminates no queen can be placed within this column
return False

if __name__ == '__main__':

for col in range(0, 1):
ways = []
board = Board(8)
res = solve_n_queens(board, col, ways)
print(*ways, sep='\n')
print(len(ways))


The board

from array import Array

class Board:
def __init__(self, size):
self._size = size
self._board = Array(size)

def size(self):
return self._size

def num_queens(self):
return len([x for x in self._board if x is not None])

def place_queen(self, row, col):
self._board[col] = row

def remove_queen(self, row, col):
self._board[col] = None

def unguarded(self, row, col):
# first check if this row is guarded
if row in self._board:
return False
# check up left diagonal
up_left_row = row - 1
up_left_col = col - 1
while self._in_bound(up_left_row, up_left_col):
if self._board[up_left_col] is not None and \
self._board[up_left_col] == up_left_row:
return False
up_left_row -= 1
up_left_col -= 1
# check lower left diagonal
low_left_row = row + 1
low_left_col = col - 1
while self._in_bound(low_left_row, low_left_col):
if self._board[low_left_col] is not None and \
self._board[low_left_col] == low_left_row:
return False
low_left_row += 1
low_left_col -= 1

return True

def _in_bound(self, row, col):
return row >= 0 and row < self.size() and \
col >= 0 and col < self.size()


And the Array that’s used to construct Board

import ctypes

class Array:
def __init__(self, size):
assert size > 0, "Array size must be > 0"
self._size = size
PyArrayType = ctypes.py_object * size
self._elements = PyArrayType()
self.clear(None)

def __len__(self):
return self._size

def __getitem__(self, index):
assert index >= 0 and index < len(self), "Array subcript out of range"
return self._elements[index]

def __setitem__(self, index, value):
assert index >= 0 and index < len(self), "Array subcript out of range"
self._elements[index] = value

def clear(self, value):
for i in range(len(self)):
self._elements[i] = value

def __iter__(self):
return _ArrayIterator(self._elements)

def __str__(self):
str_template = ""
for item in self:
str_template += " %4s "
str_template = str_template.strip()
return "[" + str_template % tuple(self) + "]"

def __contains__(self, val):
for i in self:
if i is not None and i == val:
return True
return False


## Check if a number is palindromic, O(1) space complexity (Python3)

• Reverse digits

Since this is a simple problem so I don’t explain much here. You can either convert to string and then reverse that string or using mod 10. Here a chunk of code that uses the mod 10 approach:

def reverse(x):
result, x_remaining = 0, abs(x)
while x_remaining:
result = result * 10 + x_remaining % 10
x_remaining //= 10
return -result if x < 0 else result

• Check palindrome

We can easily solve this problem by converting the number into string and pairwise comparing digits starting from the least significant digit and the most significant digit, and working inwards, stopping if there is a mismatch. But time and space complexity of this approach is O(n).

We already know how to extract the least significant digit using mod 10, but how can we extract the most significant digit? The answer is using log base 10. To calculate the number of digits of a decimal integer, we have the formula:

num_digits = math.floor(math.log10(n)) +1

Everything works the same with the converting approach, which is pairwise comparing.

import math

def is_palindrome_number(x):
# since string representation of negative numbers
# is not palindromic
if x <= 0:
return x == 0

num_digits = math.floor(math.log10(x)) + 1
# msd : most significant digit
# msd is used to remove the msd from x
for i in range(num_digits // 2):
# check equality of msd and lsd
if x // msd_mask != x % 10:
return False
x %= msd_mask # get rid of the msd of x
x //= 10 # get rid of the lsd of x
msd_mask //= 100 # 100 because msd and lsd removal
# all pair of msd and lsd match
return True

# another solution could be reverse the digit and check if
# it equals with the original one (or they are the same)


## Compute x/y (quotient) using only the addition, subtraction and shifting operators (Python3)

Solutions:

• Brute-force:

A brute-force approach is to iteratively subtract y from x until what remains is less than y. The number of such subtractions is exactly the quotient, x/y, and the remainder is the term that’s less than y. The complexity of the brute-force approach is very high, when y=1 and x = K, it will take K iterations. K could be 1, 100, or even 10^20, etc.

• The grade-school division algorithm to binary numbers

A better approach is to try and get more work done in each iteration. We can compute the largest k such that pow(2,k)*y <= x, subtract pow(2k,)*y from x, and pow(2,k) to the quotient. For example, if x = 1011 (base 2), y = 10 (base 2), then k = 2. We subtract 1000 from 1011 to get 11, add pow(2,k) = 100 (base 2) to the quotient, and continue by updating x to 11.

The advantage of using pow(2,k)*y is that it can be compute very efficiently using shifting. If it takes n bits to represent x/y, there are O(n) iterations. If the largest k such that pow(2,k)*y <= x is computed by iterating through k, each iteration has time complexity O(n). This leads to an O(n^2) algorithm.

A better way to find the largest k in each iteration is to recognize that it keeps decreasing. Therefore, instead of testing in each iteration whether 2^0 * y, 2^1 * y, 2^2 * y, …. is less than or equal to x, after we initially find the largest k such that pow(2,k)*y <= x, in subsequent iterations we test 2^k-1 * y, 2^k-2 *y, 2^k-3 *y, …

For the example given earlier, after setting the quotient to 100 (binary), we continue with 11. Now the largest k such that pow(2, k)*y <= 11 is 0, so we add 2*0 to the quotient, which is now 101. We continue with 11 – 10 = 1. Since 1 < y, we are done, the quotient is 101, and the remainder is 1 (binary).

def divide(x, y):
result, power = 0, 32
# starting at 2^32 y
y_power = y << power
while x >= y:
# searching for biggest k such that
# 2^k * y <= x
while y_power > x:
y_power >>= 1
power -= 1

result += 1 << power
# subtract 2^k * y from x
x -= y_power
return result


With each iteration, we process an additional bit. Therefore, assuming individual shift and add operation take O(1), the time complexity is O(n).

## Compute x * y without arithmetic operator (Python3)

Problem:

Write a program that multiplies two nonnegative integers. The only operator you are allowed to use are:

• Assignment
• the bitwise operator >>, <<, |, &, ^ and
• equality check

Solution:

A brute-force approach would be to perform repeated addition. Initialize the result to 0 and then ad x to it y times. The time complexity is very high.

The algorithm taught in grade-school for decimal multiplication does not use repeated addition – it uses shift and add to achieve a much better time complexity. We can do the same with binary numbers – to multiply x and y we initialize the result to 0 and iterate through the bits of x, adding pow(2, k)*k to the result if kth bit of x is 1.

The value pow(2,k)*y can be computed by left-shifting y by k. Since we cannot use add, we have to implement it. We apply the grade-school algorithm for addition to the binary case, compute the sum of bit-by-bit, and ‘rippling’ the carry along.

Example: 13(1101) * 9(1001), using the algorithm described above.
In the first iteration, since the LSB of 13 is 1, we set the result to 1001. The second bit of 13(1101) is 0 so we move on to the third bit. This bit is 1, so we shift 1001 to the left by 2 to obtain 100100, which we add to 1001 to get 101101. The final bit of 1101 is 1, so we shift 1001 to the left by 3 (this is calculate pow(2, k)*y) to obtain 1001000, which we add to 101101 to get 1110101 = 117.

Each addition is itself performed bit-by-bit. For example when adding 101101 and 1001000, the LSB of the result is 1. The next bit is 1 (since the next bit of the operands are 0). The next bit is 1 (since exactly one of the next bits of the operands is 1). The next bit is 0 (since both the next bits of the operands are 1). We also ‘carry’ a 1 to the next position. The next bit is 1 (since the carry-in is 1 and both the next bits of the operands are 0). The remaining bits are assigned similarly.

def multiply(x, y):
# just note it somewhere and when you need to
# do bit-addition quickly, you know where to find it
running_sum, carryin, k, temp_a, temp_b = 0, 0, 1, a, b
while temp_a or temp_b:
ak, bk = a & k, b & k
carryout = (ak & bk) | (ak & carryin) | (bk & carryin)
running_sum |= ak ^ bk ^ carryin
carryin, k, temp_a, temp_b = (carryout << 1, k << 1, temp_a >> 1, temp_b >> 1)
return running_sum | carryin

# first initialize result to 0
running_sum = 0
while x: # examines each bit of x
if x & 1: # the current bit is 1
# first iteration will make result = y
# as in the example
# right shift x to get next LSB
# left shift y to get pow(2, k)
x, y = x >> 1, y << 1
return running_sum


Time complexity: O(n^2), where n is the number of bits.

## Find a closet integer with the same weight (Python3)

Theory:

The weight of a nonnegative integer n to be the number of bits that are set to 1 in its binary presentation. For example, 92 base 2 is 1011100, its weight is 4.

Problem:

Write a program which takes input as a nonnegative integer and returns a number m which is not equal to n, but has the same weight as and their difference |n-m| must be minimum. For example, if x = 6, you should return 5. You can assume the integer fits in 4 bits.

Solutions:

• Brute-force

A brute-force approach might be to try all integers n-1, n+1, n-2, n+2, … stopping as soon as we encounter one with the same weight as n. This is clearly unacceptable if we want to perform this operation repeatedly.

• Heuristic

If we swap the LSB and the right most bit that differ from it, we get the result. But this is only true for some cases. For example, if n in binary is 10, then the result is 01. But this is not true if we go back and apply this heuristic approach to the example in the Theory section.

• Analyze

Assume we not gonna deal with numbers that their binary presentation consist only 0s or 1s. The move that need to be done here is swap two bits, and after swapping the result must be a number such that its difference with the original is minimal. Lets say we need to swap (or flip if since they’re different) bit at position k1 and position k2 (k1 > k2), then the difference between the original and the new swapped number is pow(2, k1) – pow(2, k2). In order to minimize the difference, k1 must be as small as possible, and k2 is next to k1. We conclude that the final solution is to swap the two rightmost consecutive bits that differ.

Example 92 (1011100), if we apply heuristic approach, it yields 89 (1011001), the final solution which we come up with yields 90 (1011010).

def closet_same_weight(n):
NUM_UNSIGNED_BITS = 64
for i in range(NUM_UNSIGNED_BITS - 1):
# two right most consecutive bits differ
if (x >> i) & 1 != (x >> (i+1)) & 1:
# swap using 'XOR flip'
x ^= (1 << i) | (1 << (i+1))
return x

# raise error if all bits are either 0 or 1
raise ValueError('All bits are 0 or 1')


Time complexity: O(n), with n is the number of bits.

## Reverse bits (Python3)

Problem:

Write a program that takes a 64-bit unsigned integer and returns the 64-bit unsigned integer consisting of the bits of the input in reverse order. For example, input 110110, output should be 011011

Solutions:

• Simple brute force solution.

If we need to perform this operation just once, there is a simple brute-force algorithm iterate through the 32 least significant bits of the input, and swap each with the corresponding most significant bit. But this is not efficient if you want to perform this operation repeatedly.

• Using lookup table (cache)

As you may know, one of the way to speed up repeated operations is using lookup table. So the first step is somehow recognize how you can apply lookup table to this problem. If we divide 64-bit word into 4 16-bit words, then everything is crystal-clear.

64-bit word -> 4 16-bit word: y3 y2 y1 y0

y0 and y1 are 2 16-bit word consisting 32 least significant bits, y2 and y3 are 2 16-bit word consisting 32 most significant bits. From left to right, y3 is reverse of y0, y2 is reverse of y1, y1 is reverse of y2 and y0 is reverse of y3. So the first thing we need to do is create a lookup table A, where A[y] yields the reversed bits of y, lets say we name it PRECOMPUTED_REVERSE. Then the code looks like this:

def reverse_bits(n):
# first, use bitmask to extract 16 least significant bits (y0)
# then lookup in the table to get its reversed bits
# and left shift 3 times of masksize to make it become y3
# next right shift 16 bits to get the next 16 LSB
# then lookup in the table to get its reversed bits
# and left shift 2 times of masksize to make it become y2
# next right shift 32 bits to get the first 16 MSB (y2)
# then lookup in the table to get its reversed bits
# and left shift 1 time of masksize to make it become y1
# finally, right shift 3 times of masksize to get y3
# lookup to get its reversed bits
# the other bits.
# the for step can be described graphically like this:
# step1 we get: y0. - - -
# step2 we get: - y1. - -
# step3 we get: - - y2. -
# step4 we get: - - - y3.
# y1. means reverse of y1
# OR all of thems to get the final result



Time complexity: O(n/L) with L is width of word we choose (here is 16)

## Swap bits – flip XOR (Python3)

Theory:

A 64-bit integer can be viewed as an array of 64 bits, with the bit at index 0 corresponding to the least significant bit (LSB – the right most bit) and the bit at index 63 corresponding to the most significant bit (MSB – the left most bit).

Solution:

A brute-force approach would be to use bitmasks to extract the ith and the jth bits, saving them to local variables. Consequently, write the saved jth bit to index i and the saved ith bit to index j.

The brute-force approach works generally, if we were swapping objects stored in an array. However, since a bit can only take two values, we can do it a little better. We first test if the bits to be swapped differ. If they do, swapping them is the same as flipping their individual values.

Example:

The 8-bit integer 73 can be viewed as array of bits, with the LSB being at index 0.

MSB(0 1 0 0 1 0 0 1)LSB

Result of swapping the 6th and 1th bit, from right to left (in bold) respectively:

MSB(0 0 0 0 1 0 1 1)LSB

which is 11 in decimal.

def swap_bits(x, i, j):
# extract the i-th and j-th bits, and see if they differ
if (x >> i) & 1 != (x >> j) & 1:
# i-th and j-th bit differ. We will swap them by
# flipping their values.
# We can perform the 'fip XOR'
bit_mask = (1 << i) | (1 << j)
return x


Time complexity: O(1)

## Compute the parity of a word (Python3)

Theory:

• The parity of a word is 1 if the number of 1s in the word is odd; otherwise, it is 0. For example, the parity of 1011 is 1, and the parity of 10001000 is 0.
• Parity check are used to detect single bit errors in data storage and comminucation
• Trick must know: x&(x-1) equals with x with its lowest set bit erased. Example:

Given n = 845, in binary = 1101001101, n – 1 in binary = 1101001100, and n after n&(n-1) in binary = 1101001100. Continue, n after n&(n-1) in binary yields 1101001000, then 1101000000, so on. Until n becomes zero.

Solutions:

• Brute force
def parity(n):
result = 0
while n:
result ^= (n & 1)
n >>= 1
return result


The brute force algorithm iteratively tests the value of each bit while tracking the number of 1s seen so far.
First, we initialize result = 0, while n is still not 0, result is updated by applying the ^ (xor) operator on the returned value when we check if the least significant bit of n is 1 or 0. More explanation, value of result first is 0, so if there is 3 1s in n, result will be 1 (value of result is 1 or 0 each while-loop). n >>= 1 right shifts n 1 bit, eventually n will reach 0.
Time complexity: O(n) with n is number of bits of the given number.

• Improved brute force

Do you notice in the theory section above, the trick can be applied here to shorten the running time of the brute force solution. This is true because number of 1s equals with number of the lowest set bit. The change is minor:

def parity_improved(n):
result = 0
while n:
result ^= 1
n &= n-1
return result


Time complexity: O(k), with k is the number of lowest set bit.

Different approaches.

The problem statement refers to computing the parity for a very large number of words. When you have to perform a large number of parity computations, more generally, any kind of bit fiddling computations, two keys to performance are processing multiple bits at a time and caching results in an array-based lookup table.

• Caching approach

Clearly, we cannot cache the parity of every 64-bit integer, we would need pow(2,64) bits of storage, which is of the order of two exabytes. However, when computing the parity of a collection of bits, it does not matter how we group those bits, the computation is associative. Therefor, we can compute the parity of a 64-bit integer by grouping its bits into four non-overlapping 16 bit subwords, computing the parity of each subwords and then computing the parity of these four subresults. We choose 16 since pow(2, 16) is relatively small, which makes it feasible to cache the parity of all 16-bit words using an array. Furthermore, since 16 evenly divides 64.

Example with a lookup table for 2-bit words. The cache is [0, 1, 1, 0], these are parities of [00, 01, 10, 11], respectively. To compute parity of 11001010, we would compute the parities of 11, 00, 10, 10. By table lookup, we see these are 0,0,1,1, respectively, so the final result is 0.

To lookup the parity of the first two bits in 11101010, we right shift by 6, to get 00000011, and use this as an index into the cache. To lookup the parity of the next two bits 10, we right shift by 4. However, right shift does not remove the leading 11 when we right shift by 4 – results in 00001110. To get the last two bits after the right shift by 4, we bitwise-AND 00001110 with 00000011 (the “mask” to extract the last 2 bits). The result is 00000010. Similar masking needed fir the two other 2-bit lookups.

Given PRECOMPUTED_PARITIES is an array of parities of all numbers from 0 up to 65535. The code is fairly understandable.

def parity_caching(n):
# first right shift 48 to get first 16 bits
# then righ shift 32 to get next 16 bits
# then right shift 16 to get next 16 bits
# final and with BIT_MASK to get the final 16 bits
# and xor all of them together to get final result
return (PRECOMPUTED_PARITIES[n >> (3 * MASK_SIZE)] ^


Time complexity: O(n/L), with L is the width we cache the results.

• Use the parity property

XOR has the property of being associative, it does not matter how we group bits, as well as commutative, the order in which we perform the XORs does not change result. The XOR of a group of bits is its parity. For example, the parity of [b63,b62,…,b2,b1,b0] equals the parity of the XOR of [b63,b62,..b32] and [b31,b30,..,b2,b1]. The XOR of these two 32-bit values can be computed with a single shift and a single 32-bit XOR instruction. We repeat the same operation on 32-, 16-, 8-, 4-, 2- and 1-but operands to get the final result. Note that the leading bits are not meaningful, and we have to explicitly extract the result from the least-significant bit.

Example with an 8-bit word. The parity of 11010111 is the asme as the parity of 1101 XOR with 0111 – results in 1010. This in turn is the same as the parity of 10 XOR with 10 – results in 00. The final result of 0 XOR with 0 is 0. Note that the first XOR yields 11011010 and only the last 4 bits are relevant going forward. The second XOR yields 11101100, and only the last 2 bits are relevant. The third XOR yields 10011010. The last bit is the result, and to extract it we have to bitwise AND with 00000001. The code:

def parity_property(n):
x ^= x >> 32
x ^= x >> 16
x ^= x >> 8
x ^= x >> 4
x ^= x >> 2
x ^= x >> 1
return x & 0x1


Time complexity: O(logn)

c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r