Skip to content

Instantly share code, notes, and snippets.

@danivijay
Created June 1, 2017 12:41
Show Gist options
  • Save danivijay/b8fe59e496434e27205666e4a558e8be to your computer and use it in GitHub Desktop.
Save danivijay/b8fe59e496434e27205666e4a558e8be to your computer and use it in GitHub Desktop.
package com.google.challenges;
public class Answer {
public static int answer(int[] l) {
// Your code goes here.
}
}
@danivijay
Copy link
Author

danivijay commented Jun 1, 2017

To find all permutations of an array :

public class Permute{
    static void permute(java.util.List<Integer> arr, int k){
        for(int i = k; i < arr.size(); i++){
            java.util.Collections.swap(arr, i, k);
            permute(arr, k+1);
            java.util.Collections.swap(arr, k, i);
        }
        if (k == arr.size() -1){
            System.out.println(java.util.Arrays.toString(arr.toArray()));
        }
    }
    public static void main(String[] args){
        Permute.permute(java.util.Arrays.asList(3,4,6,2,1), 0);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment