This is the Java Program to Split an Array and Concatenate First Part to the End.
Given an array of integers, and a value n, denoting the number of parts in which array is to be splitted. Concatenate the first part of the array to the end.
Example:
Array1: [1,2,3,4,5]
n = 3
Output:
Array = [1,4,5]
Create a new array. If the number of elements in the array is a multiple of n. Then the size of the new array will be (number of elements / n)*2, else size = (number of elements / n) + (number of elements % n). Concatenate the first and the last parts.
Here is the source code of the Java Program to Split an Array and Concatenate First Part to the End. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.
// Java Program to Split an Array and Concatenate First Part to the End.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class SplitAndConcatenate {
// Function concatenate the first and last part
static int[] splitAndConcatenate(int[] array,int n){
int size = (array.length/n);
if(array.length%n == 0)
size*=2;
else
size+=array.length%n;
int[] output = new int[size];
int i,j=0;
for(i=0; i<array.length/n; i++){
output[j++] = array[i];
}
for(i= (array.length%n == 0 ? array.length-array.length/n
: array.length - array.length%n ); i<array.length; i++){
output[j++] = array[i];
}
return output;
}
// Function to read input and display the output
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int size;
System.out.println("Enter the size of the array");
try {
size = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("Invalid Input");
return;
}
int[] array = new int[size];
System.out.println("Enter array elements");
int i;
for (i = 0; i < array.length; i++) {
try {
array[i] = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("An error occurred");
return;
}
}
int n;
System.out.println("Enter the number of parts in" +
" which array is to splitted");
try {
n = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("Invalid Input");
return;
}
System.out.println("The final array is");
if(n==0){
System.out.println(Arrays.toString(array));
}
else{
int[] output = splitAndConcatenate(array,n);
System.out.println(Arrays.toString(output));
}
}
}
1. In function splitAndConcatenate(), first, the size of one part is calculated as int size = (array.length/n).
2. Next the size of the total size of the output array is calculated as per the if conditions. If number of parts is a multiple of total array size, the output size is doubled, otherwise, the remainder is added to it.
3. Now, the output array is created and the loop for(i=0; i<array.length/n; i++) add the first part of the array to the output array.
4. The loop for(i= (array.length%n == 0 ? array.length-array.length/n : array.length – array.length%n ); i<array.length; i++) adds the last part of the array to the output array.
5. The variable i is initialized as per if total array size is a multiple of n the number of parts in which array is to be broken.
Time Complexity: O(n) where n is the number of elements in the array.
Case 1 (Simple Test Case): Enter the size of the array 5 Enter array elements 1 2 3 4 5 Enter the number of parts in which array is to splitted 3 The final array is [1, 4, 5] Case 2 (Simple Test Case - another example): Enter the size of the array 8 Enter array elements 12 23 45 23 2 56 5 87 Enter the number of parts in which array is to splitted 4 The final array is [12, 23, 5, 87]
Sanfoundry Global Education & Learning Series – Java Programs.
- Apply for Java Internship
- Check Java Books
- Apply for Computer Science Internship
- Practice BCA MCQs
- Check Programming Books