Java Program to Split the Array and Add First Part to the End

«
»

This is the Java Program to Split an Array and Concatenate First Part to the End.

Problem Description

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.

advertisement

Example:
Array1: [1,2,3,4,5] n = 3

Output:

Array = [1,4,5]

Problem Solution
advertisement
advertisement

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.

Program/Source Code

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.

  1.  
  2. // Java Program to Split an Array and Concatenate First Part to the End.
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6. import java.util.Arrays;
  7.  
  8. public class SplitAndConcatenate {
  9.     // Function concatenate the first and last part
  10.     static int[] splitAndConcatenate(int[] array,int n){
  11.         int size = (array.length/n);
  12.         if(array.length%n == 0)
  13.             size*=2;
  14.         else
  15.             size+=array.length%n;
  16.         int[] output = new int[size];
  17.         int i,j=0;
  18.         for(i=0; i<array.length/n; i++){
  19.             output[j++] = array[i];
  20.         }
  21.         for(i= (array.length%n == 0 ? array.length-array.length/n 
  22.                : array.length - array.length%n ); i<array.length; i++){
  23.             output[j++] = array[i];
  24.         }
  25.         return output;
  26.     }
  27.     // Function to read input and display the output
  28.     public static void main(String[] args) {
  29.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  30.         int size;
  31.         System.out.println("Enter the size of the array");
  32.         try {
  33.             size = Integer.parseInt(br.readLine());
  34.         } catch (Exception e) {
  35.             System.out.println("Invalid Input");
  36.             return;
  37.         }
  38.         int[] array = new int[size];
  39.         System.out.println("Enter array elements");
  40.         int i;
  41.         for (i = 0; i < array.length; i++) {
  42.             try {
  43.                 array[i] = Integer.parseInt(br.readLine());
  44.             } catch (Exception e) {
  45.                 System.out.println("An error occurred");
  46.                 return;
  47.             }
  48.         }
  49.         int n;
  50.         System.out.println("Enter the number of parts in" +
  51.                              " which array is to splitted");
  52.         try {
  53.             n = Integer.parseInt(br.readLine());
  54.         } catch (Exception e) {
  55.             System.out.println("Invalid Input");
  56.             return;
  57.         }
  58.         System.out.println("The final array is");
  59.         if(n==0){
  60.             System.out.println(Arrays.toString(array));
  61.         }
  62.         else{
  63.             int[] output = splitAndConcatenate(array,n);
  64.             System.out.println(Arrays.toString(output));
  65.         }
  66.     }
  67. }
Program Explanation

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.

advertisement

Time Complexity: O(n) where n is the number of elements in the array.

Runtime Test Cases
 
 
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.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.