Skip to content
Official PYQ · 2015

GATE CS 2015 Set 3 Q49 — Asymptotic Worst Case Time and Space Complexity

Explain with AI

Suppose c=⟨c[0],…,c[k−1]⟩c = \langle c[0], \dots, c[k-1]\rangle is an array of length kk, where all the entries are from the set {0,1}\{0, 1\}. For any positive integers a and na \text{ and } n, consider the following pseudocode.

DOSOMETHING (c, a, n)

z ← 1
for i ← 0 to k-1
     do z ← z² mod n
     if c[i]=1
           then z ← (z × a) mod n
return z

If k=4,c=⟨1,0,1,1⟩,a=2, and n=8k=4, c = \langle 1, 0, 1, 1 \rangle , a = 2, \text{ and } n=8, then the output of DOSOMETHING(c, a, n) is _______.

View worked solution

Answer: 0

We can trace the execution of the algorithm step-by-step with the initial value z=1z = 1, a=2a = 2, and n=8n = 8:

  • Iteration i=0i = 0 (c[0]=1c[0] = 1):
    First, z←z2(mod8)  ⟹  12(mod8)=1z \leftarrow z^2 \pmod 8 \implies 1^2 \pmod 8 = 1.
    Since c[0]=1c[0] = 1, z←(z×a)(mod8)  ⟹  (1×2)(mod8)=2z \leftarrow (z \times a) \pmod 8 \implies (1 \times 2) \pmod 8 = 2.
  • Iteration i=1i = 1 (c[1]=0c[1] = 0):
    First, z←z2(mod8)  ⟹  22(mod8)=4z \leftarrow z^2 \pmod 8 \implies 2^2 \pmod 8 = 4.
    Since c[1]=0c[1] = 0, the condition is false, so zz remains 44.
  • Iteration i=2i = 2 (c[2]=1c[2] = 1):
    First, z←z2(mod8)  ⟹  42(mod8)=16(mod8)=0z \leftarrow z^2 \pmod 8 \implies 4^2 \pmod 8 = 16 \pmod 8 = 0.
    Since c[2]=1c[2] = 1, z←(z×a)(mod8)  ⟹  (0×2)(mod8)=0z \leftarrow (z \times a) \pmod 8 \implies (0 \times 2) \pmod 8 = 0.
  • Iteration i=3i = 3 (c[3]=1c[3] = 1):
    First, z←z2(mod8)  ⟹  02(mod8)=0z \leftarrow z^2 \pmod 8 \implies 0^2 \pmod 8 = 0.
    Since c[3]=1c[3] = 1, z←(z×a)(mod8)  ⟹  (0×2)(mod8)=0z \leftarrow (z \times a) \pmod 8 \implies (0 \times 2) \pmod 8 = 0.

At the end of the loop, the function returns z=0z = 0.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Asymptotic Worst Case Time and Space Complexity questions