Make three non-empty sets with negative, positive and 0 products

You are given an array of n distinct integers. Your task is to divide this array into three non-empty sets so as the following conditions hold:

  1. The first set should contain the elements such that their product should be less than 0.
  2. The Second set should contain the elements such that their product should be greater than 0.
  3. The Third set should contain the elements such that their product should be equal to 0.

Notes:-

  1. In the give array, each number must occur in exactly one set.
  2. It may be assumed that we can always make three sets (there is at-least one negative element and one 0 in input array).

Examples:

Input : 4
        arr[] = -1 -2 -3 0
Output :  -1
          -3 -2 
           0
In this example, product of first
set is negative, product of second
set is positive and product of third
set is 0.

In this problem we just need to Split input data into 3 vectors: first will contain negative numbers, second positive numbers, third zeroes. If size of first vector is even move one number from it to the third vector. If second vector contains only 1, then move two numbers from first vector to the second vector.



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to make three non-empty sets
// as per the given conditions.
#include <bits/stdc++.h>
using namespace std;
  
void makeSets(int arr[], int n)
{
    vector<int> first, second, third;
  
    // insert number equal to 0 to third set.
    // numbers greater than 0 to second set.
    // insert numbers less than 0 to first set.
    for (int i = 0; i < n; i++) {
        if (arr[i] == 0)
            third.push_back(arr[i]);
        if (arr[i] > 0)
            second.push_back(arr[i]);
        if (arr[i] < 0)
            first.push_back(arr[i]);
    }
  
    if (first.size() == 0 || third.size() == 0)
    {
        cout << "Not Possible";
        return;
    }
  
    // if second set is empty.
    if (second.size() == 0) {
        for (int i = 0; i < 2; i++)
        {
            second.push_back(first.back()); 
            first.pop_back();
        }
    }
  
    // if length of first set is even.
    if (first.size() % 2 == 0) {
        third.push_back(first.back());
        first.pop_back();
    }
  
    // output the first set elements.
    for (int i = 0; i < first.size(); i++) 
        cout << first[i] << " ";    
      
    // output the second set elements.
    cout << endl;
    for (int i = 0; i < second.size(); i++) 
        cout << second[i] << " ";    
  
    // output the third set elements.
    cout << endl;
    for (int i = 0; i < third.size(); i++) 
        cout << third[i] << " ";    
}
  
// Driver Function
int main()
{
    int arr[] = { -1, 2, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
    makeSets(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to make three non-empty sets
// as per the given condition.
import java.util.*;
  
class GFG
{
  
    static void makeSets(int arr[], int n) 
    {
        Vector<Integer> first = new Vector<Integer>();
        Vector<Integer> second = new Vector<Integer>();
        Vector<Integer> third = new Vector<Integer>();
  
        // insert number equal to 0 to third set.
        // numbers greater than 0 to second set.
        // insert numbers less than 0 to first set.
        for (int i = 0; i < n; i++) 
        {
            if (arr[i] == 0
            {
                third.add(arr[i]);
            }
            if (arr[i] > 0)
            {
                second.add(arr[i]);
            }
            if (arr[i] < 0
            {
                first.add(arr[i]);
            }
        }
  
        if (first.size() == 0 || third.size() == 0
        {
            System.out.print("Not Possible");
            return;
        }
  
        // if second set is empty.
        if (second.size() == 0
        {
            for (int i = 0; i < 2; i++) 
            {
                second.add(first.lastElement());
                first.remove(first.size() - 1);
            }
        }
  
        // if length of first set is even.
        if (first.size() % 2 == 0)
        {
            third.add(first.lastElement());
            first.remove(first.size() - 1);
        }
  
        // output the first set elements.
        for (int i = 0; i < first.size(); i++) 
        {
            System.out.print(first.get(i) + " ");
        }
  
        // output the second set elements.
        System.out.println();
        for (int i = 0; i < second.size(); i++)
        {
            System.out.print(second.get(i) + " ");
        }
  
        // output the third set elements.
        System.out.println();
        for (int i = 0; i < third.size(); i++) 
        {
            System.out.print(third.get(i) + " ");
        }
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        int arr[] = {-1, 2, 0};
        int n = arr.length;
        makeSets(arr, n);
    }
}
  
/* This code contributed by PrinciRaj1992 */

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to make three non-empty sets
# as per the given conditions.
def makeSets(arr, n):
    first = [] 
    second = []
    third = []
      
    # insert number equal to 0 to third set.
    # numbers greater than 0 to second set.
    # insert numbers less than 0 to first set.
    for i in range(n):
        if (arr[i] == 0):
            third.append(arr[i])
        if (arr[i] > 0):
            second.append(arr[i])
        if (arr[i] < 0):
            first.append(arr[i])
      
    if (len(first) == 0 or len(third) == 0):
        print("Not Possible")
        return
      
    # if second set is empty.
    if (len(second)== 0):
        for i in range(2):
            second.append(first[-1]) 
            first.pop()
              
    # if length of first set is even.
    if (len(first) % 2 == 0):
        third.append(first[-1])
        first.pop()
      
    # output the first set elements.
    for i in range(len(first)):
        print(first[i], end = " ")
      
    # output the second set elements.
    print()
    for i in range(len(second)):
        print(second[i], end = " ")
          
    # output the third set elements.
    print()
    for i in range(len(third)):
        print(third[i], end = " ")
          
# Driver Function
arr = [-1, 2, 0]
n = len(arr)
makeSets(arr, n)
  
# This Code is contributed by SHUBHAMSINGH10

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to make three non-empty sets
// as per the given condition.
using System;
using System.Collections.Generic;
  
class GFG
{
  
    static void makeSets(int []arr, int n) 
    {
        List<int> first = new List<int>();
        List<int> second = new List<int>();
        List<int> third = new List<int>();
  
        // insert number equal to 0 to third set.
        // numbers greater than 0 to second set.
        // insert numbers less than 0 to first set.
        for (int i = 0; i < n; i++) 
        {
            if (arr[i] == 0) 
            {
                third.Add(arr[i]);
            }
            if (arr[i] > 0)
            {
                second.Add(arr[i]);
            }
            if (arr[i] < 0) 
            {
                first.Add(arr[i]);
            }
        }
  
        if (first.Count == 0 || third.Count == 0) 
        {
            Console.Write("Not Possible");
            return;
        }
  
        // if second set is empty.
        if (second.Count == 0) 
        {
            for (int i = 0; i < 2; i++) 
            {
                second.Add(first[first.Count-1]);
                first.Remove(first.Count - 1);
            }
        }
  
        // if length of first set is even.
        if (first.Count % 2 == 0)
        {
            third.Add(first[first.Count-1]);
            first.Remove(first.Count - 1);
        }
  
        // output the first set elements.
        for (int i = 0; i < first.Count; i++) 
        {
            Console.Write(first[i] + " ");
        }
  
        // output the second set elements.
        Console.WriteLine();
        for (int i = 0; i < second.Count; i++)
        {
            Console.Write(second[i] + " ");
        }
  
        // output the third set elements.
    Console.WriteLine();
        for (int i = 0; i < third.Count; i++) 
        {
            Console.Write(third[i] + " ");
        }
    }
  
    // Driver code
    public static void Main(String[] args) 
    {
        int []arr = {-1, 2, 0};
        int n = arr.Length;
        makeSets(arr, n);
    }
}
  
// This code has been contributed by 29AjayKumar

chevron_right



Output:

-1 
2 
0

Time Complexity :- O(n)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.