Split the Array

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.
I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.
You are given an integer array nums of even length. You have to split the array into two parts nums1 and nums2 such that:
nums1.length == nums2.length == nums.length / 2.nums1should contain distinct elements.nums2should also contain distinct elements.
Return true if it is possible to split the array, and false otherwise.
LeetCode Problem - 3046
class Solution {
public boolean isPossibleToSplit(int[] nums) {
int n = nums.length;
// Sorting the array to make it easier to split into two sets.
Arrays.sort(nums);
// Creating two arrays to hold elements from alternate positions
// in the sorted array, ensuring that each set has unique elements.
int[] arr1 = new int[n/2]; // Array for even-indexed elements
int[] arr2 = new int[n/2]; // Array for odd-indexed elements
int evenIdx = 0; // Index for even elements
int oddIdx = 1; // Index for odd elements
// Loop to fill arr1 and arr2 with alternate elements from the sorted array.
for (int i=0; i<n/2; i++){
arr1[i] = nums[evenIdx]; // Fill arr1 with even-indexed elements
arr2[i] = nums[oddIdx]; // Fill arr2 with odd-indexed elements
// Moving to the next even and odd indices.
oddIdx += 2;
evenIdx += 2;
}
// Checking for duplicates in each array.
for (int i=0; i<arr1.length-1; i++){
if (arr1[i]==arr1[i+1]){ // If there are duplicate elements in arr1.
return false; // Return false as it's not possible to split the array.
}
if (arr2[i]==arr2[i+1]){ // If there are duplicate elements in arr2.
return false; // Return false as it's not possible to split the array.
}
}
// If no duplicates are found in both sets, return true.
return true; // It's possible to split the array into two sets as required.
}
}




