Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

If I have array like [1,2,3,4] and k = 3 then output should be [1,2,3][2,3,4][1,3,4] which is in sorted order.

I can do it when k = 2 but can't think of a way to do it more generic for any value of k.

    final int arr[] = new int[] { 1, 2, 3, 4 };
    final int max = 2;
    for (int i = 0; i < arr.length - 1; i++) {
        for (int j = i + 1; j < arr.length; j++) {
            for (int k = 0; k < max; k = k + 2) {
                System.out.println(i + "" + j);
            }
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
457 views
Welcome To Ask or Share your Answers For Others

1 Answer

As I said above try to convert array to ArrayList, sort it. Imagine that now you have an original paper with list of your integer values. You copy this paper, cross off one of the integer values in copy and put this paper into your folder (HashSet in the following code). Then copy original again and so on.

package Subarrays;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Subarrays {

public static void main(String[] arg) {
    int arraySize = 0;
    int initElementNumber = 0;
    int initElementValue = 0;

    Scanner scanner = new Scanner(System.in);
    // Creates an array
    System.out.println("Type the size of the array: ");
    do {
        try {
            arraySize = scanner.nextInt();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } while (arraySize == 0);

    System.out.println("Type the number of element, which you want to initialize yourself: ");
    do {
        try {
            initElementNumber = scanner.nextInt();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } while (initElementNumber == 0);

    System.out.println("Type value of the element: ");
    do {
        try {
            initElementValue = scanner.nextInt();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } while (initElementValue == 0);

    Integer arr[] = new Integer[arraySize];
    arr[initElementNumber] = initElementValue;

    for (int i = 0; i < arr.length; i++) {
        if (i != initElementNumber) {
            arr[i] = i;
        }
    }

    // Converts the array to arrayList
    ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(arr));

    // Sorts the list
    Collections.sort(list);

    // Creates a clone of the list
    ArrayList<Integer> listTemp = (ArrayList<Integer>) list.clone();

    // Creates a set of the future lists
    Set<ArrayList<Integer>> set = new HashSet<ArrayList<Integer>>();

    // Iterates over the list and removes one element
    for (int i = 0; i < list.size(); i++) {
        listTemp.remove(i); // listTemp restructured here
        // Adding listItem to set
        set.add(listTemp);
        // Creates new clone of the list
        listTemp = (ArrayList<Integer>) list.clone();
    }

    // Writes content of the set to the console
    for (List<Integer> list2 : set) {
        System.out.print("List: ");
        for (Integer integer2 : list2) {
            System.out.print(" " + integer2 + " ");
        }
        System.out.println();
    }
}

}

Result of the executing of the code will be:

Type the size of the array: 
5
Type the number of element, which you want to initialize yourself: 
2
Type value of the element: 
8
List:  0  3  4  8 
List:  0  1  4  8 
List:  1  3  4  8 
List:  0  1  3  8 
List:  0  1  3  4 

UPDATE: I read your question more attentively and decided to rewrite the code for meeting the main requirement: find all sorted subarrays with size k in the array with size n.

package Subarrays;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Subarrays2 {

private static int ARRAY_SIZE = 0;
private static int SIZE_OF_SUBARRAYS = 0;
private static Set<ArrayList<Integer>> set = new HashSet<ArrayList<Integer>>();

public static void main(String[] arg) {

    Scanner scanner = new Scanner(System.in);
    // Creates an array
    System.out.println("Type the size of the array: ");
    do {
        try {
            ARRAY_SIZE = scanner.nextInt();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } while (ARRAY_SIZE == 0);

    System.out.println("Type the size of subarrays: ");
    do {
        try {
            SIZE_OF_SUBARRAYS = scanner.nextInt();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } while (SIZE_OF_SUBARRAYS == 0);
    scanner.close();

    Integer arr[] = new Integer[ARRAY_SIZE];

    for (int i = 0; i < ARRAY_SIZE; i++) {
        arr[i] = i;
    }

    // Converts the array to arrayList
    ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(arr));

    // Sorts the list
    Collections.sort(list);

    recursion(list); // This method work until find all of the sorted lists 
    // with size SIZE_OF_SUBARRAYS in array with size ARRAY_SIZE 

    // Writes content of the set to the console
    for (List<Integer> list2 : set) {
        System.out.print("List: ");
        for (Integer integer2 : list2) {
            System.out.print(" " + integer2 + " ");
        }
        System.out.println();
    }
}


private static void recursion(ArrayList<Integer> notSmallestCombination) {
    ArrayList<Integer> listTemp = (ArrayList<Integer>) notSmallestCombination.clone();
    for (int i = 0; i < notSmallestCombination.size(); i++) {
        listTemp.remove(i);
        if (listTemp.size() == SIZE_OF_SUBARRAYS) {
            for (int j = 0; j < notSmallestCombination.size(); j++) {
                ArrayList<Integer> list2Temp = (ArrayList<Integer>) notSmallestCombination.clone();
                list2Temp.remove(j);
                set.add(list2Temp);
            }
        } else {
            recursion(listTemp);
        }
        listTemp = (ArrayList<Integer>) notSmallestCombination.clone();
    }
}
}

And result is:

Type the size of the array: 
5
Type the size of subarrays: 
3
List:  0  3  4 
List:  0  2  3 
List:  0  1  2 
List:  1  3  4 
List:  1  2  3 
List:  0  2  4 
List:  0  1  3 
List:  2  3  4 
List:  1  2  4 
List:  0  1  4 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...