Question

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 arrays.

Next, test your method using a main method; the main method should pass in the following array: [1, 5, 4, 2]; then, it should print to the console the resulting list of permutations.

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

package pk1;

import java.util.ArrayList;
import java.util.List;

public class PermutationofInt {

   public List<List<Integer>> gerPermute(int arr[])
           {
       List<List<Integer>> list=new ArrayList<>();
       permuteEvaluator(list, new ArrayList<>(),arr);
       return list;
           }
   private void permuteEvaluator(List<List<Integer>> list, List<Integer>resultList, int[] arr)
   {
       if(resultList.size()==arr.length)
       {
           list.add(new ArrayList<>(resultList));
       }
       else
       {
           for(int i=0;i<arr.length;i++)
           {
               if(resultList.contains(arr[i]))
               {
                   continue;
               }
               resultList.add(arr[i]);
               permuteEvaluator(list, resultList, arr);
               resultList.remove(resultList.size()-1);
           }
       }
      
      
   }
   public static void main(String[] args)
   {
       PermutationofInt i=new PermutationofInt();
       int arr[]={1,5,4,2};
      
       List<List<Integer>> perm=i.gerPermute(arr);
       System.out.println("Permutations os array : [1,5,4,2] are :");
       System.out.println("************************************");
       for(List<Integer> p:perm)
       {
           System.out.println(p);
       }

   }

}

Add a comment
Know the answer?
Add Answer to:
4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array...
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
  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • in java Part III: Permutations (10 points) Write a recursive method public static ArrayList<int[] > permuteArray...

    in java Part III: Permutations (10 points) Write a recursive method public static ArrayList<int[] > permuteArray (int[] array) that returns an ArrayList of all permutations of the the parameter array. The ArrayList may contain the ar- rays in any order. Example: Suppose array = {4, 7, 1, 2}. Then the ArrayList would contain the following arrays, but in any order: 4 7 12 7 4 1 2 2 1 4 7 1 24 7 4 7 2 1 7 4...

  • JAVA Write a static method that takes an array of a generic type as its only...

    JAVA Write a static method that takes an array of a generic type as its only argument. The method should display the array and return the number of elements in the array. Test the method in main with at least three arrays of objects. SAMPLE OUTPUT Here is an Integer array 12 21 7 16 8 13 That array held 6 elements Here is a String array one two three four That array held 4 elements Here is a Double...

  • Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement...

    Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...

  • Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods...

    Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods are meant to determine if an array of objects corresponds to a heap. The methods are generic, and they should work with an array of any type T that implement Comparable<T>. Implement the second method so that it uses recursion to process the complete tree/subtree whose root is at position i in the array arr. The method should return true if that tree/subtree is...

  • ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...

    ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of integers (that the user inputs) as a parameter and returns a new array twice as large as the original, replacing every integer from the original array with a pair of integers, each half the original. If a number in the original array is odd, then the first number in the new pair should be one higher than the second so that the sum equals...

  • Recursion Exercises These exercises provide practice with recursion in Java. Objectives Module: To write recursive solutions...

    Recursion Exercises These exercises provide practice with recursion in Java. Objectives Module: To write recursive solutions for basic problems that require iteration. To identify and address base cases in a recursive method. Reminders during development Each of your solutions to the problems below should be in their own method. If a method header is provided for a problem, then your solution should have that exact method header. Any changes to the method header will receive a zero for that problem....

  • JAVA Write a method that accepts a String as an argument. The method should use recursion...

    JAVA Write a method that accepts a String as an argument. The method should use recursion to display each individual character in the String. Then, modify the method you just wrote so it displays the String backwards. The following code does not correctly display the String backwards. It merely moves the first character of the String to the end: public static void displayCharacter(String s)    {        if(s.length() == 0)            return;        else       ...

  • [Tests problem solving and a little bit of Java language] Write a Java method removeDuplicates that...

    [Tests problem solving and a little bit of Java language] Write a Java method removeDuplicates that removes all duplicates in a given list. Assume the following: a. The method accepts an object of type List b. The return type of the method is void c. Duplicates are determined using the equals() method (rather than by the == operator) Your implementation of removeDuplicates should handle, in an appropriate way, the case in which a null List is passed in to the...

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

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