Print all possible combinations in an array (which may have duplicate elements) of N.

Source: Internet
Author: User

Input:

{1, 2, 3, 4}, r = 2

Output:

{1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4} and {3, 4 }.



Package recursion; import Java. util. arraylist; import Java. util. collections; public class outputs {/* input: {1, 2, 3, 4}, r = 2 output: {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4} and {3, 4 }. */public static void main (string [] ARGs) {int [] S1 = {1, 2, 3, 4 }; arraylist <integer> set1 = new arraylist <integer> (); For (int I: S1) {set1.add (I);} int R1 = 2; arraylist <integer> ret1 = new arraylist <integer> (); rec2 (set1, R1, 0, ret1, new arraylist <integer> ()); system. out. println (ret1); int [] S2 = {1, 2, 1, 3, 2}; arraylist <integer> set2 = new arraylist <integer> (); for (int I: S2) {set2.add (I);} int r2 = 2; arraylist <integer> ret2 = new arraylist <integer> (); collections. sort (set2); rec2 (set2, R2, 0, ret2, new arraylist <integer> (); system. out. println (ret2);} // solution 1: analysis: the output is available. The first element added to Al is selected cyclically, and then the subinterval is reduced, recursive processing of subproblems // first is the first element currently processing subintervals public static void Rec (arraylist <integer> set, int R, int first, arraylist <integer> ret, arraylist <integer> Al) {If (Al. size () = r) {// exit condition: Exit RET when the number in Al reaches R. add (New arraylist <integer> (Al); Return ;}for (INT I = first; I <set. size (); I ++) {// from the first element of the subinterval to the last element will have the opportunity to be added to the Al if (I + 1 <set. size () & set. get (I + 1) = set. get (I) {// Skip duplicate elements, but check whether the row is out of bounds;} Al. add (set. get (I); // note that the parameters used in the for loop are I rather than firstrec (set, R, I + 1, RET, Al ); // shorten the subinterval by 1 distance to Al. remove (Al. size ()-1) ;}}// solution 2: select or do not select an element public static void rec2 (arraylist <integer> set, int R, int first, arraylist <integer> ret, arraylist <integer> Al) {If (r = Al. size () {// exit condition: when the number of ALS reaches R, RET can be exited. add (New arraylist <integer> (Al); return;} If (first> = set. size () {// return if the first traversal exceeds the set capacity;} while (first + 1 <set. size () & set. get (first + 1) = set. get (first) {// skip repeated elements, but check whether the cross-border first ++;} rec2 (set, R, first + 1, RET, Al ); // do not select the current element Al. add (set. get (first); // select the current element rec2 (set, R, first + 1, RET, Al); Al. remove (Al. size ()-1 );}}




Http://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.