 # DEBUGARR - Editorial

Practice

Contest

Author : Shone Pansambal

Tester : Raghu Raman

Editorialist : Yash Mehta

DIFFICULTY:
Easy

PREREQUISITES:
Basic debugging skills

PROBLEM :

Out of N given positive integers, if there exist k such integers whose sum is a perfect square, print the first set of such k integers. If there are no k integers whose sum is a perfect square, print -1.

EXPLANATION:

• Traverse the given array arr[] and check whether the element is perfect square or not.

• If the current element is perfect square then change the value of the array at that index to 1. Else change the value at that index to 0.

• Now the given array is converted into a Binary Array.

• Now, find the count of subarray with sum equals to k in the above Binary Array using the approach in the code.

POINTS WHERE DEBUGGING WAS NECESSARY:

• Line 37 : sum = sum + a[j] should be replaced by sum = sum + a[j] - a[j-k] (Since we want the sum of k consecutive elements and the first element must be removed)

• Line 40 : left = j should be replaced by left = j - k + 1 (as the temporary sum should start from j - k + 1th element) and semicolon should be added

• Line 41 : right = j + k - 1 should be replaced by right = j (as the temporary sum should end at j)

NOTE : Debugging may be unique to the individuals. The correct code given above is just how the author debugged it in a way which has the as less editing as possible (with respect to the buggy code) according to the author. There may be some of you who may have debugged this with less editing, in which case, kudos !