1 / 4
May 2017

qsn link: http://www.spoj.com/problems/JULKA/3

Could you let me know any case for which it is failing.

include

using namespace std;

int length (char *s) {

int i=0;

while(s[i]) {

    i++;

}

return i;

}

void reverse(char *a) {

int len = length(a);

int i=0,j=len-1;

while(i <= j) {

    char temp = a[i];

    a[i] = a[j];

    a[j] = temp;

    i++;j--;

}

}

void reverseInt(int *a, int length) {

int i=0,j=length-1;

while(i <= j) {

    int temp = a[i];

    a[i] = a[j];

    a[j] = temp;

    i++;j--;

}

}

int main() {

int num = 10;

char a[110], b[110];int c[112];



while(num--) {

    cin >> a >>b;

//doing (x+y)
reverse(a);

    reverse(b);

    int i=0;

    int carry = 0;

    while(a[i] && b[i]) {

        int sum = (int)(a[i]-'0')+(int)(b[i]-'0')+carry;

        c[i] = sum%10;

        carry = sum/10;

        i++;

    }

    while(a[i]){

        int sum = (int)(a[i]-'0')+carry;

        c[i] = sum%10;

        carry = sum/10;

        i++;

    }

    while(b[i]){

        int sum = (int)(b[i]-'0')+carry;

        c[i] = sum%10;

        carry = sum/10;

        i++;

    }

    if(carry){

        c[i] = carry;

        i++;

    }

//doing previously calculated (x+y)/2
int size = i;

    carry=0;

    reverseInt(c, size);

    int temp;

    for(int i=0;i<size;i++) {

        temp = c[i];

        c[i] = (carry*10+c[i])/2;

        carry = (carry*10+temp)%2;

    }

    i=0;

    while(c[i] == 0 && i < size){

        i++;

    }

    if(i == size){

        size = 1;

    }else {

        int j=0;

        while(i < size){

            c[j]=c[i];

            i++;j++;

        }

        size = j;

    }

    for(i=0;i<size;i++)

        cout << c[i];

    cout << "\n";

    reverseInt(c, size);

//doing x-(x+y)/2
//(x+y)/2 is previously calculated

    int d[224],diff;

    i=0;

    carry = 0;

    while(i<size) {

        diff = (int)(a[i]-'0')-c[i]-carry;

        if(diff < 0) {

            diff = (10+(int)(a[i]-'0')) - c[i] - carry;

            carry = 1;

        } else {

            carry = 0;

        }

        d[i] = diff;

        i++;

    }

    

    while(a[i] != '\0'){

        d[i] = (int)(a[i]-'0')-carry;

        carry = 0;

        i++;

    }

    size = i;

    reverseInt(d, size);

    i=0;

    while(d[i] == 0 && i < size){

        i++;

    }

    if(i == size){

        size = 1;

    }else {

        int j=0;

        while(i < size){

            d[j]=d[i];

            i++;j++;

        }

        size = j;

    }

    for(i=0;i<size;i++)

        cout << d[i];

    cout << "\n";

}

return 0;

}

  • created

    May '17
  • last reply

    May '17
  • 3

    replies

  • 1.5k

    views

  • 2

    users

  • 1

    like

  • 1

    link

Your code works perfectly for first cases ,as num increases it is producing garbage value
For the test case : 16 8
2003 65
100 100
16 8
Your Output: 12 4
1034 969
100 0
62 54

Hint: Clear the values stored in the variables for each case!:smiley:

Issue is not this, it was that
if(!b[i]){
while(a[i]){
int sum = (int)(a[i]-'0')+carry;
c[i] = sum%10;
carry = sum/10;
i++;
}
} else {
while(b[i]){
int sum = (int)(b[i]-'0')+carry;
c[i] = sum%10;
carry = sum/10;
i++;
}
}
I had missed the if else condition due to which it was executing both in somecases. But thanks for the test case though!! I corrected and it got accepted :slight_smile: