-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRotateAndDelete.java
More file actions
80 lines (59 loc) · 1.27 KB
/
RotateAndDelete.java
File metadata and controls
80 lines (59 loc) · 1.27 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.Scanner;
public class RotateAndDelete{
static int n;
public static void main(String[] args) {
int t, i, ans;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of test cases: ");
t = sc.nextInt();
while( t!=0 ) {
System.out.println("Enter the value of n: ");
n = sc.nextInt();
// Declaring array
int arr[] = new int[n];
// Input array
System.out.println("Enter the array: ");
for( i = 0; i < n; i++ ) {
arr[i] = sc.nextInt();
}
ans = rotateAndDelete( arr, n );
System.out.println("Answer: "+ans);
t--;
}
}
static int rotateAndDelete( int arr[], int n ) {
int N = n, j, l, i;
for( j = 1; j <= N-1; j++ ) {
rotate( arr, n );
printArray( arr, n );
l = N-j+1;
i = n-l;
delete( arr, n, i );
n = n - 1;
}
return arr[0];
}
static void rotate( int arr[], int n ) {
int temp, i;
temp = arr[n-1];
for( i = n-1; i > 0; i-- ) {
arr[i] = arr[i-1];
}
arr[0] = temp;
}
static void delete( int arr[], int n, int d ) {
int i;
i = d;
while( i < n-1 ) {
arr[i] = arr[i+1];
i++;
}
arr[n-1] = 0;
}
static void printArray( int arr[], int n ) {
int i;
System.out.println();
for( i = 0; i < n; i++ )
System.out.print(arr[i]+" ");
}
}