Advertisement
Hiteshw11

Split 1 Array in 2 new Arrays from specific position

Jul 14th, 2020
1,179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class trial {
  4.  
  5.     public static void main(String args[])
  6.     {
  7.         Scanner src=new Scanner(System.in);
  8.         System.out.println("Enter the size of array");
  9.         int a= src.nextInt();
  10.         int arr[]=new int[a];
  11.         System.out.println("Enter the numbers");
  12.        
  13.         for(int i=0;i<a;i++)
  14.         {
  15.           arr[i]=src.nextInt();
  16.         }
  17.         System.out.println("Enter the position at which you wish to split (position starts with 1)");
  18.         int pos=src.nextInt();
  19.         int pos3=pos;
  20.         //int pos2=pos-1;
  21.         int m=0,n=0;
  22.         //System.out.println("The first part is");
  23.         for(int j=0;j<pos;j++)
  24.         {
  25.             //System.out.println(arr[j]);
  26.             m++;
  27.         }
  28.         //System.out.println("The second part is");
  29.         for(int k=pos;k<a;k++)
  30.         {
  31.             //System.out.println(arr[k]);
  32.             n++;
  33.         }
  34.         int arr2[]=new int[m];
  35.         int arr3[]=new int[n];
  36.         for(int p=0;p<m;p++)
  37.         {
  38.        
  39.             arr2[p]=arr[p];
  40.         }
  41.         for(int q=0;q<n;q++)
  42.         {
  43.            
  44.             arr3[q]=arr[pos3];
  45.             pos3++;
  46.         }
  47.         System.out.println("The First new array is");
  48.         for(int s=0;s<m;s++)
  49.         {
  50.             System.out.println(arr2[s]);
  51.         }
  52.         System.out.println("The second new array is");
  53.         for(int t=0;t<n;t++)
  54.         {
  55.             System.out.println(arr3[t]);
  56.         }
  57.        
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement