Question

Part III: Permutations (10 points) Write a recursive method public static ArrayList<int[] > permuteArray (int[] array) that r

in java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;

public class PermuteArray {
  
public static void main(String[]args)
{
int[] arr = {4, 7, 1, 2};
ArrayList<int[]> list = permuteArray(arr);
for(int i = 0; i < list.size(); i++)
{
if(i % 6 == 0 && i != 0)
System.out.println();
  
for(int j = 0; j < list.get(i).length; j++)
{
System.out.print(list.get(i)[j] + " ");
}
System.out.println();
}
}
  
public static ArrayList<int[]> permuteArray(int[] array)
{
ArrayList<int[]> result = new ArrayList<>();
helper(0, array, result);
return result;
}
  
private static void helper(int start, int[] nums, ArrayList<int[]> result)
{
if(start == nums.length - 1)
{
ArrayList<Integer> list = new ArrayList<>();
for(int num : nums)
{
list.add(num);
}
result.add(list.stream().mapToInt(Integer::intValue).toArray());
return;
}
for(int i = start; i < nums.length; i++)
{
swap(nums, i, start);
helper(start + 1, nums, result);
swap(nums, i, start);
}
}
  
private static void swap(int[] nums, int i, int j)
{
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}

********************************************************************* SCREENSHOT ******************************************************

DD run: 4 7 1 2 4 7 2 1 4 1 7 2 4 1 2 7 4 2 1 7 4 2 7 1 7 4 1 2 7 4 2 1 7 1 4 2 7 1 2 4 7 2 1 4 7 2 4 1 1 74 2 1 7 2 4 1 47 2

Add a comment
Know the answer?
Add Answer to:
in java Part III: Permutations (10 points) Write a recursive method public static ArrayList<int[] > permuteArray...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array...

    4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array arr of integers argument returns a list of all permutations of these integers. (A permutation of a sequence of integers is a re-arrangement of the integers. For example, one permutation of 1, 3, 4, 8, 2 is 3, 1, 2, 8, 4.) For this problem, you may assume that the input array contains no duplicate entries. Your method should return an ArrayList of int...

  • Public class Permutations { // Helper method for outputting an array. private static v...

    public class Permutations { // Helper method for outputting an array. private static void PrintArray(string[] array) { foreach (string element in array) { Console.Write($"{element} "); } Console.WriteLine(); } // Helper method for invoking Generate. private static void Generate(string[] array) { Generate(array.Length, array); } public static void Main(string[] args) { } } procedure generate(k : integer, A : array of any): if k = 1 then output(A) else // Generate permutations with kth unaltered // Initially k == length(A) generate(k -...

  • Write a recursive method: public static int raise2ToN(int n) //computes and returns the value of 2n...

    Write a recursive method: public static int raise2ToN(int n) //computes and returns the value of 2n using //recursion. Note: 2^3 = 2 * 2^2 . 2^2 = 2 * 2^1 2^1 = 2 What value/values of n will stop the recursion (base case)? Sample output: 5 2 to 5 is 32

  • Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...

    Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...

  • //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList...

    //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a test program that does the following operations: 4. Creates an empty ArrayList of Integer elements; 5. Generates 20 integer values, drawn at random between 0 and 9 (included), and adds them to the array list; 6. Calls the method sort and prints both the original list, and the sorted one.

  • Write a recursive method in java to find GCD of two integers using Euclid's method. Integers...

    Write a recursive method in java to find GCD of two integers using Euclid's method. Integers can be positive or negative. public class Recursion {                 public static void main(String[] args) {                                 Recursion r = new Recursion();                                 System.out.println(“The GCD of 24 and 54 is “+r.findGCD(24,54)); //6                 }                 public int findGCD(int num1, int num2){                                 return -1;                 } }

  • A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers...

    A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers as a a parameter and returns a new ArrayList that has every element in the second half of the original ArrayList swapped with every element in the first half of the original ArrayList. Note: • You can assume that the ArrayList passed to this method as a parameter always contains an even number of elements and will always contain at least two elements. For...

  • Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int...

    Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int k = n; k > 0; k--) seq.add(new Integer(k+3)); return seq What is the final output of the following Java statement? System.out.println(mystery(3)); a. [1,2,4,5) b. [2,3,4,5) c. [6,5,4,3] d. [7, 7, 8, 8] e. [7,8,9, 8) O Consider the following method: public static int mystery(int[] arr, int k) ifk-0) return 0; }else{ return arr[k - 1] + mystery(arr, k-1):) The code segment below is...

  • Consider the following Java method: public static ArrayList<Integer nums ArrayList<Integer values new ArrayList<Integer0; for(int i=10; i<30;...

    Consider the following Java method: public static ArrayList<Integer nums ArrayList<Integer values new ArrayList<Integer0; for(int i=10; i<30; i-i+3) if(i%) values.add(i); return values: What is returned after the method call nums()? a. [12] b. [13] c. [14] d. [15] e. [16] O Which of the following represents the final output of the code segment below? int k: int[]A; A new int[3]: fork-0; k<A.length;k++) A[k]-A.length-k; forſk-0; k<A.length-1; k++) A[k+1]-A[k) for(int i-0;i<A.length;i++) System.out.print(A[i]+" "); a. 222 O b. 333 c. 444 d. 555 O...

  • public class F2{ public static int foo(ArrayList<Integer> a) { int sum = 0; for(int i =...

    public class F2{ public static int foo(ArrayList<Integer> a) { int sum = 0; for(int i = 0; i <a.size(); i++){ if(i % 3 == 1) sum += a.get(i); else if(i % 3 == 2) sum -= a.get(i); else sum++; } return sum; } } What do the following method calls return? 1. foo(new ArrayList<>(Arrays.asList(1,2,3,4))) 2. foo(new ArrayList<>()) 3. foo(new ArrayList<>(Arrays.asList(1,2,-3,4325,-2))) 4. foo(new ArrayList<>(Arrays.asList(0,0,0)))

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT