Genera todas las combinaciones posibles en Java

Sheeraz Gul 12 octubre 2023
  1. Use la recurrencia para generar todas las combinaciones posibles en Java
  2. Use Incluir-Excluir para generar todas las combinaciones posibles en Java
Genera todas las combinaciones posibles en Java

Este tutorial demuestra cómo generar todas las combinaciones posibles de los elementos de un array en Java.

Use la recurrencia para generar todas las combinaciones posibles en Java

Primero, creamos un array vacía que almacenará las salidas. La idea es arreglar los elementos uno por uno y luego usar la recurrencia.

Finalmente, cuando el número de elementos del array inicial se vuelve igual al tamaño de las combinaciones, imprimimos el array inicial. Intentemos implementarlo en Java.

package delftstack;

import java.io.*;

public class Possible_Combinations {
  static void CombinationPossible(int Input_Array[], int Empty_Array[], int Start_Element,
      int End_Element, int Array_Index, int r) {
    // Current combination is ready to be printed, print it
    if (Array_Index == r) {
      for (int x = 0; x < r; x++) {
        System.out.print(Empty_Array[x] + " ");
      }
      System.out.println("");
      return;
    }

    for (int y = Start_Element; y <= End_Element && End_Element - y + 1 >= r - Array_Index; y++) {
      Empty_Array[Array_Index] = Input_Array[y];
      CombinationPossible(Input_Array, Empty_Array, y + 1, End_Element, Array_Index + 1, r);
    }
  }

  static void Print_Combination(int Input_Arrary[], int n, int r) {
    int Empty_Array[] = new int[r];
    CombinationPossible(Input_Arrary, Empty_Array, 0, n - 1, 0, r);
  }

  public static void main(String[] args) {
    int Input_Array[] = {10, 30, 50, 70, 90, 100};
    int r = 3;
    int n = Input_Array.length;
    Print_Combination(Input_Array, n, r);
  }
}

El código anterior generará todas las combinaciones posibles de el array dada en forma de tres números. Ver salida:

10 30 50
10 30 70
10 30 90
10 30 100
10 50 70
10 50 90
10 50 100
10 70 90
10 70 100
10 90 100
30 50 70
30 50 90
30 50 100
30 70 90
30 70 100
30 90 100
50 70 90
50 70 100
50 90 100
70 90 100

Use Incluir-Excluir para generar todas las combinaciones posibles en Java

De manera similar, creamos un array vacía y usamos el problema de identidad de Pascal para generar todas las combinaciones posibles de un array.

En este método, consideramos los elementos del arreglo dado y recurrimos usando los dos casos. El primer caso es el elemento incluido en la combinación actual.

El segundo caso es que el elemento está excluido en la combinación actual. Intentemos implementar este método en Java.

package delftstack;

import java.io.*;

public class Possible_Combinations {
  static void PossibleCombinations(
      int Input_Array[], int n, int Length, int Array_Index, int Empty_Array[], int x) {
    if (Array_Index == Length) {
      for (int y = 0; y < Length; y++) System.out.print(Empty_Array[y] + " ");
      System.out.println("");
      return;
    }

    if (x >= n)
      return;

    Empty_Array[Array_Index] = Input_Array[x];
    PossibleCombinations(Input_Array, n, Length, Array_Index + 1, Empty_Array, x + 1);

    PossibleCombinations(Input_Array, n, Length, Array_Index, Empty_Array, x + 1);
  }

  static void Print_Combination(int Input_Array[], int n, int Length) {
    int Empty_Array[] = new int[Length];

    PossibleCombinations(Input_Array, n, Length, 0, Empty_Array, 0);
  }

  public static void main(String[] args) {
    int Input_Array[] = {10, 30, 50, 70, 90, 100};
    int Length = 3;
    int n = Input_Array.length;
    Print_Combination(Input_Array, n, Length);
  }
}

El código anterior generará todas las combinaciones posibles de el array dada en forma de tres números. Ver salida:

10 30 50
10 30 70
10 30 90
10 30 100
10 50 70
10 50 90
10 50 100
10 70 90
10 70 100
10 90 100
30 50 70
30 50 90
30 50 100
30 70 90
30 70 100
30 90 100
50 70 90
50 70 100
50 90 100
70 90 100
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook