Divide given number into two even parts in Java
Dividing a number into two even parts is a fundamental mathematical concept that is relevant across various disciplines and everyday scenarios. It involves dividing a number into two equal halves.
Example 1:
Input: N = 20
Output:
Even number pairs that sum up to 20: (2, 18) (4, 16) (6, 14) (8, 12) (10, 10)
Example 2:
Input: N = 5
Output: No pairs found that sum up to 5
Using Naive Approach
Algorithm
Step 1: Accept an integer N representing the target sum.
Step 2: Loop through even numbers from 2 to N/2.
Step 3: For each even number i, check if (N - i) is even.
Step 4: If the sum of i and (N - i) equals N, output the pair (i, N - i).
Here is an implementation of dividing the number into two even parts using a naive approach:
FileName:EvenNumberPairs.java
public class EvenNumberPairs { public static void findEvenPairs(int N) { System.out.println("Even number pairs that sum up to " + N + ":"); boolean foundPairs = false; for (int i = 2; i <= N / 2; i += 2) { if ((N - i) % 2 == 0) { System.out.println("(" + i + ", " + (N - i) + ")"); foundPairs = true; } } if (!foundPairs) { System.out.println("No even number pairs found that sum up to " + N); } } public static void main(String[] args) { int N = 20; findEvenPairs(N); } }
Output:
Even number pairs that sum up to 20: (2, 18) (4, 16) (6, 14) (8, 12) (10, 10)
Complexity analysis: The time complexity of the above naive approach is O(N/2) due to the loop iterating through all possible even numbers up to N/2. Within the loop, there are constant-time operations. Therefore, the overall time complexity is O(N). The space complexity is O(1) as there is no additional space usage dependent on the input size.
Using Iterative Approach
Algorithm
Step 1: Take N as input.
Step 2: Loop through even numbers from 2 to N/2.
Step 3: For each even number i, check if (N - i) is even and less than or equal to i. If so, add (i, N - i) to the list of pairs.
Step 4: Print the list of pairs or "No pairs found" if empty.
Here is an implementation of dividing the number into two even parts using an iterative approach:
FileName:DivideEvenPartsIterative.java
import java.util.ArrayList; import java.util.List; public class DivideEvenPartsIterative { // Function to check if a given number can be divided into two even parts public static void canDivideEven(int num) { List> pairs = new ArrayList<>(); for (int i = 2; i <= num / 2; i += 2) { if ((num - i) % 2 == 0) { List
pair = new ArrayList<>(); pair.add(i); pair.add(num - i); pairs.add(pair); } } if (!pairs.isEmpty()) { System.out.println("Even number pairs that sum up to " + num + ":"); for (List pair : pairs) { System.out.println("(" + pair.get(0) + ", " + pair.get(1) + ")"); } } else { System.out.println("No pairs found that sum up to " + num); } } public static void main(String[] args) { int N = 20; // Change N to the desired input canDivideEven(N); } }
Output:
Even number pairs that sum up to 20: (2, 18) (4, 16) (6, 14) (8, 12) (10, 10)
Complexity analysis: The time complexity of the above iterative approach is O(N) as it iterates through all even numbers up to N/2 to find pairs summing up to N. The space complexity is also O(N) due to the storage of pairs in a list.