[Solved] Divide the Love CodeCrush by NewtonSchool

You are amazed at the relationship between John and Olivia. So you decided to tell that how many different pairs of relationships can be made from John and Olivia’s relationship. For that, there is an array of length N and you need to divide it into K components such that each component contains an equal number of elements, no element is in two different components, and together it contains all the elements. You also need to divide in such a way that the parity of the sum of all the components is the same.
If there exists more than one K. Output the maximum such K.

But the array itself is not given only the number of odd, the number of even elements is given.

Input

The only line contains two integers A, B such that A denotes the number of even elements in the array and B denotes the number of odd elements in the array.

Constraints
1 <= A <= 1010
1 <= B <= 1010

Output

Output a single integer K the maximum value of the number of the components.

Example

Sample Input 1:
5 4

Sample Output 1:
3

Solution

import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args) {
        // Your code here
        Scanner sc = new Scanner(System.in);
        long a = sc.nextLong();
        long b = sc.nextLong();
        long ans = 1;
        if(((a+b)%2==0) && (b%2==0)) ans = (a+b)/2;
        ArrayList<Long> ar = new ArrayList<Long>();
        for(long i=2;i<=Math.sqrt(a+b);i++){
            if((a+b)%i==0){
                ar.add((long)i);
                ar.add(((a+b)/i));
            }
        }
        for(int j = 0;j<ar.size();j++){
            long i = ar.get(j);
            if((b%2==0) && (i>ans)){
                ans = i;
            }
            if((b%2!=0) && (i%2!=0) && (i>ans) && (b>=i) && (a>=i)){
                ans = i;
            }
        }
        System.out.println(ans);
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *