Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 431735a

Browse files
authored
Create Zig_Zag_Array.java
1 parent db5a138 commit 431735a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

‎Arrays/Zig_Zag_Array.java‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.lang.*;
2+
import java.io.*;
3+
class GFG
4+
{
5+
public static void main (String[] args)
6+
{
7+
//code
8+
Scanner s = new Scanner(System.in);
9+
int t = s.nextInt();
10+
11+
while(t-- > 0){
12+
int n = s.nextInt();
13+
14+
int a[] = new int[n];
15+
for(int i = 0 ; i < n ; i++){
16+
a[i] = s.nextInt();
17+
}
18+
19+
for(int i = 1 ; i < n ; i++){
20+
if(i%2 != 0){
21+
if(a[i] < a[i-1]){
22+
int temp = a[i];
23+
a[i] = a[i-1];
24+
a[i-1] = temp;
25+
}
26+
}
27+
else{
28+
if(a[i] > a[i-1]){
29+
int temp = a[i];
30+
a[i] = a[i-1];
31+
a[i-1] = temp;
32+
}
33+
}
34+
}
35+
36+
for(int i = 0 ; i < n ; i++){
37+
System.out.print(a[i] + " ");
38+
}
39+
System.out.println();
40+
}
41+
}
42+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /