\newcommand{\nfrac}[2]{\frac{\displaystyle{#1}}{\displaystyle{#2}}}

Exercises

1.1.4

Hints

  • You are given a square number nn. Given some integer kk, How can we verify it is the root?
  • Follow the exhaustive search strategy, to find the root of nn.
  • You are given a real number rr. Given some integer kk, How can we verify it is the floor of rr?
  • Follow the exhaustive search strategy, to find the floor of nn.
  • Combine all previous hints to find a unique definition of n\lfloor \sqrt{n} \rfloor.
  • 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 m=2m = 2 and n=3n = 3.
  • Why mmod  n=mm \mod n = m when m<nm < n?
  • Recall the definition of mod. What are the possible ranges of xmod  nx \mod n for any integer xx?

Solution

It shall swap them as r=mmod  n=mr = m \mod n = m when m<nm < n.

Only once. Given m>nm > n, Necessarily n>mmod  nn > m \mod n.

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 ab=ba| a - b | = | b - a |.
  • If we checked all elements with A[i]A[i], Do we need to test A[j]A[j] with A[i]A[i]?

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 A[i]==A[j]A[i] == A[j] which index shall be counted? What can we conclude about SS?

Solution

a. Tedious to typeset.

b. No. Observe counting only happens when strictly i<ji < j. If A[i]==A[j]A[i] == A[j] then the code counts A[i]A[i] not A[j]A[j]. Therefore A[i]A[i]

shall succeed A[j]A[j]. In fact equal cells are reversed in the sorted array.

c. No. It does not modify array AA but output is a different array SS.

1.4.2

Hint

  • For ascendingly ordered array AA, Is it possible for the target value tt to exist in A[i..n1]A[i..n-1] given the fact t>A[i]t > A[i]?
  • 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 tt:

a. Access some element xx in the array. If txt \neq x, We can ignore searching in the right/left side of xx.

b. While linear scanning, Terminate the algorithm earlier once some A[i]>tA[i] > t.

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