Open In App

Partition Array for unique second half

Last Updated : 26 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] having n integers, divide the array into two halves, such that all elements in the array’s second half are unique elements. The first half of the array may contain duplicate as well as unique elements. Print the partition index and the second half of the array.

Examples:

Input: n = 7, arr = {3, 7, 7, 7, 5, 2, 9} 
Output: The partition index is: 3, and the second half of the array is: {7, 5, 2, 9}  
Explanation: At index 0, the element 3 is unique, but this cannot be our partition index, as repeating numbers, may exist in the greater indices. At, index 1, 2, and 3, element 7 is there, which are duplicates. Now, from index 4, 5, and 6, unique elements only exist, hence, this could be our second half of the arrray. But, if we include index 3, element 7, then, also the subaarray {7, 5, 2, 9}, is unique. Hence, the partition index is 3. 

Input: n = 5, arr = {11, 4, 4, 4, 7}
Output: The partition index is: 3, and the second half of the array is: {4, 7}
Explanation: At index 0, the element 11 is unique, but this cannot be our partition index, as repeating numbers, may exist in the greater indices. At, index 1, 2, and 3, element 4 is there, which are duplicates. Now, from index 4, unique elements only exits, hence, this could be our second half of the array. But, if we include index 3, element 4, then, also the subarray {4, 7}, is unique. Hence, the paritition index is 3.

Approach: To solve the problem follow the below idea:

Traverse the array from n-1 to 0 index. As, the second half should contain, unique elements. While traversing at each index, we will check whether that element is present in the map, if not found then, push the element into the map, or if found the element, then, that index will be the last element, of the first half of the array. As, we are printing index of the first element of the second half of the array, as the partition index, hence, increment the found index by 1, to get the required answer. 

Below are the steps for the above approach:

  • Create an empty set s.
  • Traverse the array from n-1 to 0 index. If not found an element, then insert it in the set. 
  • If found the element in the set, a duplicate has been found, break the loop. 
  • Initialize a variable parititonIndex = i + 1
  • Print the paritionIndex and the second half of the array. 

Below is the implementation for the above approach:

C++

// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;

// Function to find partition index,
// and second half of the array.
void findPartitionIndex(vector<int> arr, int n)
{

    // set to store the unique elements.
    unordered_set<int> s;

    // i is the parition index.
    int i = n - 1;
    for (; i >= 0; i--) {
        if (s.count(arr[i]))
            break;
        s.insert(arr[i]);
    }

    int partitionIndex = i + 1;

    // Printing the partition index.
    cout << "The partition index is: " << partitionIndex
         << ", and the second half of the array is: ";

    // Printing second half of the array.
    for (i = partitionIndex; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

// Driver code
int main()
{

    int n = 7;
    vector<int> arr = { 3, 7, 7, 7, 5, 2, 9 };

    // Function Call
    findPartitionIndex(arr, n);

    return 0;
}

Java

// java code addition

import java.util.*;

public class Main {

    // function to find partition index,
    // and second half of the array.
    public static void findPartitionIndex(List<Integer> arr, int n)
    {

        // set to store the unique elements.
        Set<Integer> s = new HashSet<>();

        // i is the partition index.
        int i = n - 1;
        for (; i >= 0; i--) {
            if (s.contains(arr.get(i)))
                break;
            s.add(arr.get(i));
        }

        int partitionIndex = i + 1;

        // printing the partition index.
        System.out.print("The partition index is: " + partitionIndex + ", and the second half of the array is: ");

        // printing second half of the array.
        for (i = partitionIndex; i < n; i++) {
            System.out.print(arr.get(i) + " ");
        }
        System.out.println();
    }

    // Driver code
    public static void main(String[] args)
    {

        int n = 7;
        List<Integer> arr = new ArrayList<>(Arrays.asList(3, 7, 7, 7, 5, 2, 9));
        findPartitionIndex(arr, n);

        n = 5;
        arr = new ArrayList<>(Arrays.asList(11, 4, 4, 4, 7));
        findPartitionIndex(arr, n);

        n = 6;
        arr = new ArrayList<>(Arrays.asList(4, 4, 4, 4, 4, 4));
        findPartitionIndex(arr, n);
    }
}

// The code is contributed by Nidhi goel.

Javascript

// Javascript code addition 

// function to find partition index,
// and second half of the array.
function findPartitionIndex(arr, n) {

    // set to store the unique elements.
    const s = new Set();

    // i is the partition index.
    let i = n - 1;
    for (; i >= 0; i--) {
        if (s.has(arr[i])) break;
        s.add(arr[i]);
    }

    const partitionIndex = i + 1;

    // printing the partition index.
    process.stdout.write(`The partition index is: ${partitionIndex}, and the second half of the array is: `);

    // printing second half of the array.
    for (i = partitionIndex; i < n; i++) {
        process.stdout.write(arr[i] + " ");
    }
    console.log();
}

// Driver code
let n = 7;
let arr = [3, 7, 7, 7, 5, 2, 9];
findPartitionIndex(arr, n);

n = 5;
arr = [11, 4, 4, 4, 7];
findPartitionIndex(arr, n);

n = 6;
arr = [4, 4, 4, 4, 4, 4];
findPartitionIndex(arr, n);

// The code is contributed by Nidhi goel. 

Python3

# Python code addition 

# function to find partition index,
# and second half of the array.
def find_partition_index(arr, n):

    # set to store the unique elements.
    s = set()

    # i is the partition index.
    i = n - 1
    while i >= 0:
        if arr[i] in s:
            break
        s.add(arr[i])
        i -= 1

    partition_index = i + 1

    # printing the partition index.
    print("The partition index is:", partition_index, "and the second half of the array is: ", end ="")

    # printing second half of the array.
    for i in range(partition_index, n):
        print(arr[i], end =" ")
    print()

# Driver code
n = 7
arr = [3, 7, 7, 7, 5, 2, 9]
find_partition_index(arr, n)

n = 5
arr = [11, 4, 4, 4, 7]
find_partition_index(arr, n)

n = 6
arr = [4, 4, 4, 4, 4, 4]
find_partition_index(arr, n)

# The code is contributed by Nidhi goel. 

C#

// C# code addition

using System;
using System.Collections.Generic;

class Program {
    // function to find partition index,
    // and second half of the array.
    static void FindPartitionIndex(List<int> arr, int n)
    {
        // set to store the unique elements.
        HashSet<int> s = new HashSet<int>();

        // i is the partition index.
        int i = n - 1;
        for (; i >= 0; i--) {
            if (s.Contains(arr[i]))
                break;
            s.Add(arr[i]);
        }

        int partitionIndex = i + 1;

        // printing the partition index.
        Console.Write($ "The partition index is: {partitionIndex}, and the second half of the array is: ");

        // printing second half of the array.
        for (i = partitionIndex; i < n; i++) {
            Console.Write(arr[i] + " ");
        }
        Console.WriteLine();
    }

    // driver code
    static void Main(string[] args)
    {
        int n = 7;
        List<int> arr = new List<int>{ 3, 7, 7, 7, 5, 2, 9 };
        FindPartitionIndex(arr, n);

        n = 5;
        arr = new List<int>{ 11, 4, 4, 4, 7 };
        FindPartitionIndex(arr, n);

        n = 6;
        arr = new List<int>{ 4, 4, 4, 4, 4, 4 };
        FindPartitionIndex(arr, n);
    }
}

// The code is contributed by Nidhi goel.
Output

The partition index is: 3, and the second half of the array is: 7 5 2 9 

Time Complexity: O(N)
Auxiliary Space: O(1) 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads