Suppose 𝑐 = βŒ©π‘[0], … , 𝑐[π‘˜ βˆ’ 1]βŒͺ is an array of length π‘˜, where all the…

2015

Suppose 𝑐 = βŒ©π‘[0], … , 𝑐[π‘˜ βˆ’ 1]βŒͺ is an array of length π‘˜, where all the entries are from the set {0, 1}. For any positive integers π‘Ž and 𝑛, consider the following pseudocode.

image.png



If π‘˜ = 4, 𝑐 = 〈1, 0, 1, 1βŒͺ, π‘Ž = 2 and 𝑛 = 8, then the output of DOSOMETHING(𝑐, π‘Ž, 𝑛) is __________________.

Attempted by 45 students.

Show answer & explanation

Correct answer: 0

Step-by-step evaluation:

  1. Initialize z = 1.

  2. i = 0: square z: 1^2 mod 8 = 1. Since c[0] = 1, multiply by a: 1 Γ— 2 mod 8 = 2. Resulting z = 2.

  3. i = 1: square z: 2^2 mod 8 = 4. Since c[1] = 0, do not multiply. Resulting z = 4.

  4. i = 2: square z: 4^2 = 16 mod 8 = 0. Since c[2] = 1, multiply by a: 0 Γ— 2 mod 8 = 0. Resulting z = 0.

  5. i = 3: square z: 0^2 = 0 mod 8 = 0. Since c[3] = 1, multiply by a: 0 Γ— 2 mod 8 = 0. Resulting z = 0.

Return value: 0

Explore the full course: Gate Guidance By Sanchit Sir