-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5da9df6
commit 50f5ed2
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package hackerRank.week2; | ||
|
||
import java.util.Arrays; | ||
|
||
public class FindZigZagSequence { | ||
|
||
|
||
/* public static void findZigZagSequence(int [] a, int n){ | ||
Arrays.sort(a); | ||
int mid = (n + 1)/2; | ||
int temp = a[mid]; | ||
a[mid] = a[n - 1]; | ||
a[n - 1] = temp; | ||
int st = mid + 1; | ||
int ed = n - 1; | ||
while(st <= ed){ | ||
temp = a[st]; | ||
a[st] = a[ed]; | ||
a[ed] = temp; | ||
st = st + 1; | ||
ed = ed + 1; | ||
} | ||
for(int i = 0; i < n; i++){ | ||
if(i > 0) System.out.print(" "); | ||
System.out.print(a[i]); | ||
} | ||
System.out.println(); | ||
}*/ | ||
|
||
// how it was above | ||
// how it became below | ||
// the trick below is that the first part of the method already give hints of what should become in the second part. be aware! | ||
public static void findZigZagSequence(int [] a, int n){ | ||
Arrays.sort(a); | ||
int mid = (n)/2; // changed here | ||
int temp = a[mid]; | ||
a[mid] = a[n - 1]; | ||
a[n - 1] = temp; | ||
|
||
int st = mid + 1; | ||
int ed = n - 2; // here | ||
while(st <= ed){ | ||
temp = a[st]; | ||
a[st] = a[ed]; | ||
a[ed] = temp; | ||
st = st + 1; | ||
ed = ed - 1; // here | ||
} | ||
for(int i = 0; i < n; i++){ | ||
if(i > 0) System.out.print(" "); | ||
System.out.print(a[i]); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
} |