Se le da una array de n enteros distintos. Su tarea es dividir esta array en tres conjuntos no vacíos para que se cumplan las siguientes condiciones:
- El primer conjunto debe contener los elementos tales que su producto sea menor que 0.
- El segundo conjunto debe contener los elementos tales que su producto debe ser mayor que 0.
- El tercer conjunto debe contener los elementos tales que su producto debe ser igual a 0.
Notas:-
- En la array dada, cada número debe aparecer exactamente en un conjunto.
- Se puede suponer que siempre podemos hacer tres conjuntos (hay al menos un elemento negativo y un 0 en la array de entrada).
Ejemplos:
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.
En este problema, solo necesitamos dividir los datos de entrada en 3 vectores: el primero contendrá números negativos, el segundo números positivos y el tercero ceros. Si el tamaño del primer vector es par, mueva un número de él al tercer vector. Si el segundo vector contiene solo 1, mueva dos números del primer vector al segundo vector.
C++
// 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; }
Java
// 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 */
Python3
# 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
C#
// 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
Javascript
<script> // JavaScript program to make three non-empty sets // as per the given conditions. function makeSets(arr, n) { let 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 (let i = 0; i < n; i++) { if (arr[i] == 0) third.push(arr[i]); if (arr[i] > 0) second.push(arr[i]); if (arr[i] < 0) first.push(arr[i]); } if (first.length == 0 || third.length == 0) { document.write("Not Possible"); return; } // if second set is empty. if (second.length == 0) { for (let i = 0; i < 2; i++) { second.push(first[first.length - 1]); first.pop(); } } // if length of first set is even. if (first.length % 2 == 0) { third.push(first[first.length - 1]); first.pop(); } // output the first set elements. for (let i = 0; i < first.length; i++) document.write(first[i] + " "); // output the second set elements. document.write("<br>"); for (let i = 0; i < second.length; i++) document.write(second[i] + " "); // output the third set elements. document.write("<br>"); for (let i = 0; i < third.length; i++) document.write(third[i] + " "); } // Driver Function let arr = [-1, 2, 0]; let n = arr.length; makeSets(arr, n); </script>
Producción:
-1 2 0
Complejidad de tiempo :- O(n)
Publicación traducida automáticamente
Artículo escrito por Sagar Shukla y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA