-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2D arrays
190 lines (165 loc) · 4.93 KB
/
2D arrays
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// TRANSPOSE OF A MATRIX
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // 3
int a[][] = new int[n][n]; // 1 2 3
for(int i=0;i<n;i++){ // 4 5 6
for(int j=0;j<n;j++){ // 7 8 9
a[i][j]=sc.nextInt();
}
}
//extra space
// for(int i=0;i<n;i++){
// for(int j=0;j<n;j++){
// System.out.print(a[j][i] + " ");
// }
// System.out.println();
// }
// by using swapping Method(no extra space is required) // 1 4 7
for(int i=0;i<n;i++){ // 2 5 8
for(int j=i+1;j<n;j++){ // 3 6 9
int temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] =temp;
}
}
// ROTATE A MATIRX BY 90 DEGREE CLOCKWISE
for(int i=0;i<n;i++){
int start =0;
int end = n-1;
while(start<end){
int temp =a[i][start]; //coulmn changes
a[i][start]=a[i][end];
a[i][end] =temp;
start++;
end--;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){ // 7 4 1
System.out.print(a[i][j] + " "); // 8 5 2
} // 9 6 3
System.out.println();
}
}
}
/* You are given a chessboard of size N x N, where the top left square is black. Each square contains a value.
Find the sum of values of all black square and all white squares */
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
int a[][] = new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
a[i][j] =sc.nextInt();
}
}
int sum_black =0;
int sum_white =0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if((i+j)%2==0){
sum_black += a[i][j];
}
else{
sum_white +=a[i][j];
}
}
}
System.out.println(sum_black);
System.out.println(sum_white);
}
}
/* Given a 2D array, the task is to print the 2D in alternate manner (First row from left to right, then from right to left, and so on).
Examples: Input : arr[][2] = [[1, 2], [2, 3]]; Output : 1 2 3 2 */
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc =new Scanner(System.in);
int m =sc.nextInt();
int n =sc.nextInt();
int a[][]= new int[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
a[i][j] =sc.nextInt();
}
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(i%2==0){
System.out.print(a[i][j]+" ");
}
else{
System.out.print(a[i][n-j-1]+" ");
}
}
}
}
}
// SPIRALLY TRAVERSING THE MATRIX
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int m =sc.nextInt(); // 3 3
int n = sc.nextInt(); // 1 2 3
int a[][]= new int[m][n]; // 4 5 6
for(int i=0;i<m;i++){ // 7 8 9
for(int j=0;j<n;j++){
a[i][j] =sc.nextInt();
}
}
int left = 0;
int right = n-1;
int top =0;
int bottom =m-1;
int dir =0;
while(top<=bottom && left<=right){
if(dir==0){
for(int i =left;i<=right;i++){
System.out.print(a[top][i]+" ");
}
top++;
dir=1;
}
else if(dir==1){
for(int i=top;i<=bottom;i++){
System.out.print(a[i][right]+" "); // 1 2 3 6 9 8 7 4 5
}
right--;
dir =2;
}
else if(dir==2){
for(int i=right;i>=left;i--){
System.out.print(a[bottom][i]+" ");
}
bottom--;
dir =3;
}
else if(dir==3){
for(int i=bottom;i>=top;i--){
System.out.print(a[i][left]+" ");
}
left++;
dir=0;
}
}
}
}