-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsert.java
More file actions
57 lines (39 loc) · 904 Bytes
/
Copy pathInsert.java
File metadata and controls
57 lines (39 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.*;
public class Insert
{
static int Insertion(int[] arr,int n,int cap,int x,int pos)
{
if(n==cap)
return n;
int index=pos-1;
for(int i=n-1;i>=index;i--)
{
arr[i+1]=arr[i];
}
arr[index]=x;
return n+1;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the capacity of array: ");
int cap=sc.nextInt();
int[] arr=new int[cap];
System.out.print("Enter the size: ");
int n=sc.nextInt();
System.out.println("Enter the elemnts: ");
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Enter the element you want to insert: ");
int x=sc.nextInt();
System.out.println("Enter the position you want to insert: ");
int pos=sc.nextInt();
n=Insertion(arr,n,cap,x,pos);
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+" ");
}
}
}