-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetSubArray.java
More file actions
44 lines (37 loc) · 1.37 KB
/
GetSubArray.java
File metadata and controls
44 lines (37 loc) · 1.37 KB
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
import java.util.*;
class GetSubArray{
public int[] getSubArray(int[] arr, int si, int ei){
int length = ei-si;
int[] result =new int[length];
for(int i= si; i<ei;i++){
result[i-si] = arr[i];
}
return result;
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
GetSubArray obj= new GetSubArray();
System.out.println("Enter the number of elements required :- ");
int n= sc.nextInt();
int[] arr= new int[n];
System.out.println("Enter the values into the array :- ");
for(int i=0; i<n;i++){
System.out.println("Enter the value at index value :- " + i);
arr[i] = sc.nextInt();
}
System.out.println("Enter the starting value of the sub-array :- ");
int si= sc.nextInt();
System.out.println("Enter the ending value of the sub-array :- ");
int ei= sc.nextInt();
int[] result= obj.getSubArray(arr,si,ei);
System.out.println("The array :-");
for(int num : arr){
System.out.print(num + " ");
}
System.out.println("--------------------");
System.out.println("The sub-array is as follows :- ");
for(int num : result){
System.out.print(num + " ");
}
}
}