Exercises
1.1.4
Hints
- You are given a square number . Given some integer , How can we verify it is the root?
- Follow the exhaustive search strategy, to find the root of .
- You are given a real number . Given some integer , How can we verify it is the floor of ?
- Follow the exhaustive search strategy, to find the floor of .
- Combine all previous hints to find a unique definition of .
- Follow the exhaustive search strategy, to solve the main problem.
Solution
for i in n-1 .. 0
if (i)^2 <= n
return i
1.1.8
Hints
- Try this case on concrete examples like and .
- Why when ?
- Recall the definition of mod. What are the possible ranges of for any integer ?
Solution
It shall swap them as when .
Only once. Given , Necessarily .
1.2.5
Hints
- Convert a concrete decimal number to binary. Observe how the right most digit from the binary representation is obtained.
- Given a binary representation, What is the number we divide on it, so that the quotient eliminate the right most digit?
- Follow the Decrease and Conquer strategy, with the above two hints, to solve the problem.
Solution
DecToBin(n):
# input: integer n
# output: binary representation as a list
# binary representation
l = [ ]
while n != 0:
# kth digit from right to left
b.appendLeft( n % 2 )
# remove the rightmost digit
# division output is an integer
n = n/2
1.2.9
Hint
- Are there duplicated computations?
- Are there pairs tested twice?
- Observe .
- If we checked all elements with , Do we need to test with ?
Solution
MinDistance( A ):
# input: array of size n
# output: minimum distance between two distinct elements
dmin = infinity
for i in 0 .. n-1:
for j in i+1 .. n-1:
dis = | A[i] - A[j] |
if dis < dmin:
dmin = dis
1.3.1
Hints
- if which index shall be counted? What can we conclude about ?
Solution
a. Tedious to typeset.
b. No. Observe counting only happens when strictly . If then the code counts not . Therefore
shall succeed . In fact equal cells are reversed in the sorted array.
c. No. It does not modify array but output is a different array .
1.4.2
Hint
- For ascendingly ordered array , Is it possible for the target value to exist in given the fact ?
- Use the above hint to prune the search space.
- Which index of the array you think shall prune the greatest search space.
Solution
For target value :
a. Access some element in the array. If , We can ignore searching in the right/left side of .
b. While linear scanning, Terminate the algorithm earlier once some .
1.4.10
Hints
- Is it possible for two strings to be anagrams in case they different lengths?
- Is it possible for two strings to be anagrams if one of them has a character not present in the other?
- You can convert a character to its corresponding ascii number. Use that for a cheaper data strucutre.
- the ascii number corresponds to an index.
Solution
Two strings are anagrams if and only if they have the same count of characters.
AreStringsAnagrams(A, B):
# input two strings
# output True if anagrams and False otherwise
# if lengths are not the same, then not anagrams
if length(A) != length(B):
return False
# initialize characters counts to zeros for both strings
A_chCount = B_chCount = [ 0 ] * 26
# Count characters in both strings
for ch in A:
A_chCount[ int(ch) ] = A_chCount[ int(ch) ] + 1
for ch in B:
B_chCount[ int(ch) ] = B_chCount[ int(ch) ] + 1
# Anagrams if and only if characters count is exactly the same
return A_chCount == B_chCount