Skip to content

Commit

Permalink
zigzag sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
brendonmiranda committed Jan 3, 2025
1 parent 5da9df6 commit 50f5ed2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ This repo register my evolution in the Cracking The Coding Interview book as I g

[Tower Breakers](https://github.com/brendonmiranda/CrackingTheCodingInterview/blob/main/src/main/java/hackerRank/week2/TowerBreakers.java)

[Find ZigZag Sequence]()

# AWS Challenges

[Review Score](https://github.com/brendonmiranda/CrackingTheCodingInterview/blob/main/src/main/java/aws/ReviewScore.java)
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/hackerRank/week2/FindZigZagSequence.java
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();
}

}

0 comments on commit 50f5ed2

Please sign in to comment.