NOTE: The code snippet given may be incomplete or have compile time, runtime or logical errors.

How to attempt Debugging Exercise?

  1. Copy the code to your code editor (e.g. VS Code).

  2. Add relevant header files like “#include <iostream>” etc.

  3. Run the code.

  4. You will notice the expected output is not printing at the console.

  5. Apply your smart coder mind to Debug the code.

  6. Warning: Only see the solution after you have tried enough.

  7. Debug the code.

bool isPowerOfFour(int n) {
        if(n<=1){
           return false;
        }
        if(n%4!=0){
           return false;
        }
       
        return isPowerOfFour(n/4);
    }
  1. Debug the code.
double myPow(double x, int n) {

       int p = abs(n);
       double ans = 1.0;
       while(p>=1){
          p /= 2 ;
          x *= x;
       }
       return n<0 ? 1/ans : ans;    
    }
  1. Debug the code.
int subset(int input[], int n, int output[][50], int k) {
    if(n==0){
        output[0][0] = 0;
        return 1;
    }
    
    int small = subset(input+1,n-1,output,k);
    int count = 0;
    
    for(int i=0;i<small;i++){
        output[small+count][1] = input[0];
        int sum = input[0];
        int l = 0;
        for(int j=2;output[small-count-1][j-1]!=0;j++){
            l++;
            output[small+count][j] = output[small-count-1][j-1];
            sum = sum + output[small-count-1][j-1];
        }
        output[small+count][0] = -1;
        if(sum==k){
            output[small+count][0] = l+1;
        }
        count++;
    }
    return small+count;
}

int subsetSumToK(int input[], int n, int output[][50], int k) {
    int temp[1110000][50];
    int size = subset(input,n,temp,k);
    int m=0;
    for(int i=0;i<size;i++){
        if(temp[i][0]>0){
            for(int j=0;j<temp[i][0];j++){
                output[m][j] = temp[i][j];
            }
        }
    }
    
    return m;
}
  1. Debug the code.
int returnPermutations(string input, string output[]){
    int count = 0;
    string created;
    for(int i=0;input[i]!='\\0' && input.length()>1;i++){
        char ch = input[i];
        int start = count;
        created = input.substr(0,i) + input.substr(i);
        count = count + returnPermutations(created,output+count);
        for(int k=start;k<count;k++){
            output[k] = ch + output[k];
        }
    }
    if(input.length()==1){
        for(int k=0;input[k]=='\\0';k++){
            output[count] = output[count] + input[k];
            count++;
        }
    }
    
    return count;
}
  1. Debug the code.
void removeConsecutiveDuplicates(char *input) {
	if(input[0] == '\\0'){
        return;
    }
	if(input[0] == input[1]){
        int i = 2;
        for(;input[i]!='\\0';i++){
            input[i-1] = input[i]; 
        }
        input[i-1] = input[i];
    }
    removeConsecutiveDuplicates(input);
}
/*
Example:
xxab --> xab
*/
  1. Debug the code.
void shift(char input[]){
    int i =1;
    for(i=1;input[i]!='\\0';i++)
        input[i-1] = input[i];
    input[i-1] = '\\0';
}
void removeX(char input[]) {
    if(input[0]=='x'){
        shift(input);
    	removeX(input);
    }
    else
    removeX(input+1);
}

/*
Example:
xxab --> ab
*/

solution-1.txt

solution-2.txt

solution-3.txt

solution-4.txt

solution-5.txt

solution-6.txt