Genere todas las subsecuencias distintas de la array utilizando el retroceso

Dada una array arr[] que consta de N enteros positivos, la tarea es generar todas las subsecuencias distintas de la array.

Ejemplos:

Entrada: arr[] = {1, 2, 2}
Salida: {} {1} {1, 2} {1, 2, 2} {2} {2, 2}
Explicación:
Las subsecuencias totales de la array dada son {}, {1}, {2}, {2}, {1, 2}, {1, 2}, {2, 2}, {1, 2, 2}.
Dado que {2} y {1, 2} se repiten dos veces, imprima todas las subsecuencias restantes de la array.

Entrada: arr[] = {1, 2, 3, 3}
Salida: {} {1} {1, 2} {1, 2, 3} {1, 2, 3, 3} {1, 3} {1 , 3, 3} {2} {2, 3} {2, 3, 3} {3} {3, 3}

 

Enfoque: siga los pasos a continuación para resolver el problema:

  1. Ordenar la array dada .
  2. Inicialice un vector de vectores para almacenar todas las subsecuencias distintas.
  3. Atraviese la array y considere dos opciones para cada elemento de la array, incluirlo en una subsecuencia o no incluirlo.
  4. Si se encuentran duplicados, ignórelos y verifique los elementos restantes. De lo contrario, agregue el elemento de array actual a la subsecuencia actual y recorra los elementos restantes para generar subsecuencias.
  5. Después de generar las subsecuencias, elimine el elemento de array actual.

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to generate all distinct
// subsequences of the array using backtracking
void backtrack(vector<int>& nums, int start,
               vector<vector<int> >& resultset,
               vector<int> curr_set)
{
    resultset.push_back(curr_set);
 
    for (int i = start; i < nums.size(); i++) {
 
        // If the current element is repeating
        if (i > start
            && nums[i] == nums[i - 1]) {
            continue;
        }
 
        // Include current element
        // into the subsequence
        curr_set.push_back(nums[i]);
 
        // Proceed to the remaining array
        // to generate subsequences
        // including current array element
        backtrack(nums, i + 1, resultset,
                  curr_set);
 
        // Remove current element
        // from the subsequence
        curr_set.pop_back();
    }
}
 
// Function to sort the array and generate
// subsequences using Backtracking
vector<vector<int> > AllSubsets(
    vector<int> nums)
{
    // Stores the subsequences
    vector<vector<int> > resultset;
 
    // Stores the current
    // subsequence
    vector<int> curr_set;
 
    // Sort the vector
    sort(nums.begin(), nums.end());
 
    // Backtrack function to
    // generate subsequences
    backtrack(nums, 0, resultset,
              curr_set);
 
    // Return the result
    return resultset;
}
 
// Function to print all subsequences
void print(vector<vector<int> > result)
{
    for (int i = 0; i < result.size();
         i++) {
 
        cout << "{";
        for (int j = 0; j < result[i].size();
             j++) {
 
            cout << result[i][j];
            if (j < result[i].size() - 1) {
 
                cout << ", ";
            }
        }
        cout << "} ";
    }
}
 
// Driver Code
int main()
{
    vector<int> v{ 1, 2, 2 };
 
    // Function call
    vector<vector<int> > result
        = AllSubsets(v);
 
    // Print function
    print(result);
 
    return 0;
}

Java

// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to generate all distinct
// subsequences of the array using backtracking
public static void backtrack(ArrayList<Integer> nums,
                             int start,
                             ArrayList<Integer> curr_set)
{
    System.out.print(curr_set + " ");
 
    for(int i = start; i < nums.size(); i++)
    {
         
        // If the current element is repeating
        if (i > start &&
            nums.get(i) == nums.get(i - 1))
        {
            continue;
        }
 
        // Include current element
        // into the subsequence
        curr_set.add(nums.get(i));
 
        // Proceed to the remaining array
        // to generate subsequences
        // including current array element
        backtrack(nums, i + 1, curr_set);
 
        // Remove current element
        // from the subsequence
        curr_set.remove(curr_set.size() - 1);
    }
}
 
// Function to sort the array and generate
// subsequences using Backtracking
public static void AllSubsets(ArrayList<Integer> nums)
{
 
    // Stores the current
    // subsequence
    ArrayList<Integer> curr_set = new ArrayList<>();
 
    // Sort the vector
    Collections.sort(nums);
 
    // Backtrack function to
    // generate subsequences
    backtrack(nums, 0, curr_set);
}
 
// Driver Code
public static void main(String[] args)
{
    ArrayList<Integer> v = new ArrayList<>();
    v.add(1);
    v.add(2);
    v.add(2);
     
    // Function call
    AllSubsets(v);
}
}
 
// This code is contributed by hemanthswarna1506

Python3

# Python3 program to implement
# the above approach
result = []
  
# Function to generate all distinct
# subsequences of the array
# using backtracking
def backtrack(nums, start, curr_set):
     
    # Global result
    result.append(list(curr_set))
     
    for i in range(start, len(nums)):
         
        # If the current element is repeating
        if (i > start and nums[i] == nums[i - 1]):
            continue
         
        # Include current element
        # into the subsequence
        curr_set.append(nums[i])
  
        # Proceed to the remaining array
        # to generate subsequences
        # including current array element
        backtrack(nums, i + 1, curr_set)
  
        # Remove current element
        # from the subsequence
        curr_set.pop()
     
# Function to sort the array and generate
# subsequences using Backtracking
def AllSubsets(nums):
 
    # Stores the current
    # subsequence
    curr_set = []
     
    # Sort the vector
    nums.sort()
     
    # Backtrack function to
    # generate subsequences
    backtrack(nums, 0, curr_set)
     
# Function to prints all subsequences
def prints():
     
    global result
 
    for i in range(len(result)):
        print('{', end = '')
         
        for j in range(len(result[i])):
            print(result[i][j], end = '')
 
            if (j < len(result[i]) - 1):
                print(',', end = ' ')
                 
        print('} ', end = '')
         
# Driver Code
if __name__=='__main__':
     
    v = [ 1, 2, 2 ]
      
    # Function call
    AllSubsets(v)
  
    # Print function
    prints()
 
# This code is contributed by rutvik_56

C#

// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
     
// Function to generate all distinct
// subsequences of the array using
// backtracking
public static void backtrack(List<int> nums,
                             int start,
                             List<int> curr_set)
{
  Console.Write(" {");
   
  foreach(int i in curr_set)
  {
    Console.Write(i);
    Console.Write(", ");
  }
 
  Console.Write("}");
   
  for(int i = start;
          i < nums.Count; i++)
  {
    // If the current element
    // is repeating
    if (i > start &&
        nums[i] == nums[i - 1])
    {
      continue;
    }
 
    // Include current element
    // into the subsequence
    curr_set.Add(nums[i]);
 
    // Proceed to the remaining array
    // to generate subsequences
    // including current array element
    backtrack(nums, i + 1, curr_set);
 
    // Remove current element
    // from the subsequence
    curr_set.Remove(curr_set.Count - 1);
  }
}
 
// Function to sort the array and generate
// subsequences using Backtracking
public static void AllSubsets(List<int> nums)
{
  // Stores the current
  // subsequence
  List<int> curr_set = new List<int>();
 
  // Sort the vector
  nums.Sort();
 
  // Backtrack function to
  // generate subsequences
  backtrack(nums, 0, curr_set);
}
 
// Driver Code
public static void Main(String[] args)
{
  List<int> v = new List<int>();
  v.Add(1);
  v.Add(2);
  v.Add(2);
 
  // Function call
  AllSubsets(v);
}
}
 
// This code is contributed by 29AjayKumar

Javascript

<script>
    // Javascript program to implement the above approach
     
    // Function to generate all distinct
    // subsequences of the array using
    // backtracking
    function backtrack(nums, start, curr_set)
    {
      document.write(" {");
 
      for(let i = 0; i < curr_set.length - 1; i++)
      {
        document.write(curr_set[i]);
        document.write(", ");
      }
       
      if(curr_set.length >= 1)
      {
          document.write(curr_set[curr_set.length - 1]);
          document.write("}");
      }
      else
      {
          document.write("}");
      }
       
 
      for(let i = start; i < nums.length; i++)
      {
        // If the current element
        // is repeating
        if (i > start && nums[i] == nums[i - 1])
        {
          continue;
        }
 
        // Include current element
        // into the subsequence
        curr_set.push(nums[i]);
 
        // Proceed to the remaining array
        // to generate subsequences
        // including current array element
        backtrack(nums, i + 1, curr_set);
 
        // Remove current element
        // from the subsequence
        curr_set.pop();
      }
    }
 
    // Function to sort the array and generate
    // subsequences using Backtracking
    function AllSubsets(nums)
    {
      // Stores the current
      // subsequence
      let curr_set = [];
 
      // Sort the vector
      nums.sort();
 
      // Backtrack function to
      // generate subsequences
      backtrack(nums, 0, curr_set);
    }
     
    let v = [];
    v.push(1);
    v.push(2);
    v.push(2);
 
    // Function call
    AllSubsets(v);
 
// This code is contributed by mukesh07.
</script>
Producción

{} {1} {1, 2} {1, 2, 2} {2} {2, 2}

Complejidad temporal: O(2 N )
Espacio auxiliar: O(N)

Publicación traducida automáticamente

Artículo escrito por manupathria y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *