Problem with JULKA

I don’t know why my solution is wrong for a SPOJ problem the problem name is JULKA.

Problem link is this.
link text

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
 
using namespace std;
int main()
{
    int t=10,a,b;
    while(t--) {
               cin>>a>>b;
               int x,y;
               //x+y = a;
               //x-y = b;
               cout<<(a+b)/2<<"\n"<<(a-b)/2<<endl;
    }  
    return 0; 
}

Please help me to find the exact solution of this problem.

2 Likes

check ur answer for larger inputs… see the statements " It is known that both girls have no more than 10^100 (1 and 100 zeros) apples together"
In my case i used BigInteger class in java which gave me an AC

@akashm please share some lines of your code because i am not good with java.

2 Likes

@upendra1234 sure…heres my solution to JULKA on SPOJ


import java.util.*;
import java.math.*;

class JULKA {
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		for (int t = 0; t < 10; t++) {
			BigInteger A = new BigInteger(in.next());
			BigInteger B = new BigInteger(in.next());
			BigInteger K = (A.add(B)).divide(new BigInteger("2"));
			BigInteger N = A.subtract(K);
			System.out.println(K.toString());
			System.out.println(N.toString());
		}
	}
}

Here’s my solution in Python. It works well for the big numbers given in the problem. But still i get a wrong answer every time. Somebody, please point out the errors.!!!

 c=10
    while c > 0:
    	c=c-1
    	n=int(input())
    	a=int(input())
    	print((n+a)/2)
    	print((n-a)/2)

Input :

10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
100000000000000000000
5
5
6
2
10
2
5
1
6
2
10
2
5
1
6
2
5
2
6
2
5
2

Output:

    5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000050000000000000000000
4999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999950000000000000000000
5
0
4
2
6
4
3
2
4
2
6
4
3
2
4
2
3
1
1 Like

@rishantagarwal this is your corrected code…

c=10
while c > 0:
    c=c-1
    n=int(input())
    a=int(input())
    k=(n+a)/2
    print(k)
    print(n-k)
c=10
while(c>0):
	c-=1
	n=int(raw_input())
	a=int(raw_input())
	k=(n-a)/2
	print n+a
	print k

for python we should use long instead of int and in c/c++ it can be done using char array .

i can share my code if you want.

what wrong in my JULKA code?
Please me to figure out that that ,i am getting Runtime error when submit.

#include
#include
#include
#include
#include
#include
using namespace std;
string a, b;
int i = 0, j = 0, k = 0, carry = 0;
void convert_to_int(string s,int ar1[]) {
	for (i = 0;i < s.length();i++)ar1[i] = s[i] - 48;
}
void add(int int_a[],int int_b[],int ar[]) {
	int ad[101] = { 0 };
	for (i = a.length() - 1, j = b.length() - 1;i >= 0;i--, j--) {
		int tmp = 0;
		if (j >= 0)tmp = int_a[i] + int_b[j];
		else tmp = int_a[i];
		ad[k++] = (tmp + carry) % 10;
		carry = tmp / 10;
	}
	while (carry) {
		ad[k++] = carry;
		carry /= 10;
	}
	int t = 0;
	for (int i = k-1;i >= 0;i--)ar[t++]=ad[i];
}
void subtract(int int_a[],int int_b[],int n) {
	int sub[101] = { 0 };
	k = 0;
	for (i = a.length() - 1, j = n;i >= 0;i--, j--) {
		int tmp = 0;
		if (j >= 0) {
			if (int_a[i] >= int_b[j])tmp = int_a[i] - int_b[j];
			else {
				for (int c = 1;c < a.length();c++) {
					if (int_a[i - c] > 0) {
						int_a[i - c] -= 1;
					}
					else int_a[i - c] = 9;
				}
				tmp = (10 + int_a[i]) - int_b[j];
			}
		}
		else tmp = int_a[i];
		if(tmp==0&&i==0)sub[k] = tmp;
		else sub[k++] = tmp;
	}
	for (int i = k - 1;i >= 0;i--)cout<<sub[i];
	cout << endl;
}
int divide(int int_a[],int d[]) {
	k = 0;
	carry = 0;
	for (i = 0;i < a.length();i++) {
		if ((int_a[i] + carry) / 2 == 0) {
			carry = (int_a[i] % 2) * 10;
			if (carry == 0)d[k++] = int_a[i];
		}
		else if ((int_a[i] + carry) / 2 != 0) {
			d[k++] = (int_a[i] + carry) / 2;
			carry = (int_a[i] + carry) % 2;
			carry *= 10;
		}
	}
	for (int i = 0;i < k;i++)cout << d[i];
	cout <> a >> b;
		int int_a[101] = { 0 }, int_b[101] = { 0 };
		convert_to_int(a, int_a);
		convert_to_int(b, int_b);
		int total[200] = { 0 };
		add(int_a, int_b, total);
		int d[100] = { 0 };
		int n = divide(total, d);
		subtract(int_a, d, n);
	}
	//system("pause");
	return 0;
}

for this type of question submission should not allowed with bigInteger or any of library function :frowning: because it does not make any sense.

1 Like

In C++, it is very difficult, but in JAVA, its a cakewalk.

import java.math.*;
import java.util.*;
class Julka
{
    public static void main(String args[])
    {
        Scanner sn=new Scanner(System.in);
        for(int i=1;i<=10;i++)
        {
            BigInteger s=new BigInteger(sn.nextLine());
            BigInteger c=new BigInteger(sn.nextLine());
            System.out.println(s.add(c).divide(new BigInteger("2")));
            System.out.println(s.subtract(c).divide(new BigInteger("2")));
        }
    }
}

@upendra1234 it doesn’t work as simple as that.Because even long long int cannot take input as big as 100 digits.U should make use of arrays or similar data structures.
See geeksforgeeks big integers problem for factorial Find the Factorial of a large number - GeeksforGeeks if u can solve this, u can solve all other big integer problems.

Thanks dude! i have done this problem.

if you want to learn somthing from this question do it in c++ or c

#include<bits/stdc++.h>
#include<boost/multiprecision/cpp_int.hpp>
using namespace std;
using namespace boost::multiprecision;
int main()
{
int t=10;
cpp_int a,b;
while(t–)
{
cin >> a >> b;
cout << (a+b)/2 << “\n” << (a-b)/2 << “\n”;
}
return 0;
}

Link to my complete solution: https://qr.ae/pNuJGL
In C++ using big integers
Status: Accepted