Open In App

Split the array into two distinct array

Last Updated : 02 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

You are given an integer array arr[] of size N (N is an even number). You have to split the array into two equal parts, arr1 and arr2 such that arr1 should contain distinct integers and arr2 should contain distinct integers. In other words, there should be no duplicates in arr1 or arr2.

Return true if it is possible to split the array, and false otherwise.

Examples:

Input: arr[] = {1, 1, 2, 2, 3, 4}, N = 6
Output: true
Explanation: One of the possible ways to split arr[] is arr1[] = {1, 2, 3} and arr2[] = {1, 2, 4}.

Input: arr[] = {1, 1, 1, 1}, N = 4
Output: false
Explanation: The only possible way to split arr[] is arr1[] = {1, 1} and arr2[] = {1, 1}. Both arr1[] and arr2[] do not contain distinct elements. Therefore, we return false.

Approach: To solve the problem, follow the below idea:

If we count the frequency of all integers of the array, then there can be three cases:

  • Frequency of the integer is 1, then we can put that integer to any of the parts.
  • Frequency of the integer is 2, then we can put one occurrence of the integer in arr1 and another in arr2.
  • Frequency of the integer > 2, then we have to put more than one occurrence of the integer in any of the two parts, therefore it is impossible to split the array in two parts.

Therefore, we can count the frequency of all the elements in arr[] and if the count of any element is greater than 2, then it is impossible to split it into 2 parts otherwise we can always split the array arr[] into two parts of equal size with distinct elements only.

Step-by-step algorithm:

  • Create a HashMap freq to count the occurrences of each element in the input array.
  • Fill the HashMap with element counts from the input array.
  • If frequency of any element of array is greater then 2 return false.
  • Otherwise frequency of all element is less then or equal to 2 return true.

Below is the implementation of the above approach:

C++
#include <iostream>

#include <unordered_map>

#include <vector>

using namespace std;

// Function to check whether it is possible to split

// arr[] into 2 parts of equal size with distinct elements

bool isPossibleToSplit(vector<int>& arr) {

    int N = arr.size();

    // Hashmap to store the frequency of all elements in

    // the array

    unordered_map<int, int> freq;

    // Count the frequency of all the elements in arr[]

    for (int i : arr)

        freq[i]++;

    // Check if any element has frequency > 2, then

    // return false

    for (auto& it : freq) {

        if (it.second > 2)

            return false;

    }

    // If no element has frequency greater than 2,

    // return true

    return true;

}

int main() {

    // Sample Input

    vector<int> arr = {1, 1, 2, 2, 3, 4};

    cout << boolalpha << isPossibleToSplit(arr);

    return 0;

}
Java
import java.util.*;

class GFG {
    // function to check whether it is possible to split
    // arr[] into 2 parts of equal size with distinct
    // elements
    public static boolean isPossibleToSplit(int arr[])
    {
        int N = arr.length;
        // Hashmap to store the frequency of all elements in
        // the array
        HashMap<Integer, Integer> freq = new HashMap<>();

        // Count the frequency of all the elements in arr[]
        for (int i : arr)
            freq.put(i, freq.getOrDefault(i, 0) + 1);
        // Check if any elements has frequency > 2, then
        // return false
        for (int i : freq.keySet()) {
            if (freq.get(i) > 2)
                return false;
        }
        // If no element has frequency greater than 2,
        // return true
        return true;
    }
    public static void main(String[] args)
    {
        // Sample Input
        int arr[] = { 1, 1, 2, 2, 3, 4 };

        System.out.print(isPossibleToSplit(arr));
    }
}
Python
from collections import defaultdict

# Function to check whether it is possible to split
# arr[] into 2 parts of equal size with distinct
# elements
def is_possible_to_split(arr):
    # Dictionary to store the frequency of all elements in
    # the array
    freq = defaultdict(int)

    # Count the frequency of all the elements in arr[]
    for i in arr:
        freq[i] += 1

    # Check if any element has frequency > 2, then
    # return False
    for i in freq.keys():
        if freq[i] > 2:
            return False

    # If no element has frequency greater than 2,
    # return True
    return True

if __name__ == "__main__":
    # Sample Input
    arr = [1, 1, 2, 2, 3, 4]

    print(is_possible_to_split(arr))
    
 # This code is contributed by shivamgupta0987654321
JavaScript
// Function to check whether it is possible to split
// arr[] into 2 parts of equal size with distinct elements
function isPossibleToSplit(arr) {
    // Hashmap to store the frequency of all elements in
    // the array
    let freq = {};

    // Count the frequency of all the elements in arr[]
    for (let i of arr)
        freq[i] = (freq[i] || 0) + 1;

    // Check if any element has frequency > 2, then
    // return false
    for (let key in freq) {
        if (freq[key] > 2)
            return false;
    }

    // If no element has frequency greater than 2,
    // return true
    return true;
}

// Sample Input
let arr = [1, 1, 2, 2, 3, 4];
console.log(isPossibleToSplit(arr));

Output
true

Time Complexity: O(N), where N is the size of input array arr[].
Auxiliary Space: O(N)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads