-
-
Save mmloveaa/e16ccd7ddfc6a1a76e176244e7414cbf to your computer and use it in GitHub Desktop.
3-31
This file contains hidden or 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
| Zigzag Array | |
| A group of friends, who shall remain nameless, have hacked into the logistics system of an evil dictator. They want to deplete the dictator's fuel supply by making his army units travel as much distance as possible when they go to suppress rebellions in various locations up and down the coast. While these friends are good at cracking security, they need some help with elementary computing tasks. So they reach out to you. They want to be able to give you an array of positive and negative integers, representing distances north or south of the capital, and they want to see the elements of the array in zigzag order. This means that the largest member appears first, the smallest appears second, and the remaining elements alternate between the larger members decreasing from the largest, and the smaller members increasing from the smallest. | |
| For example, the array [1, 3, 6, 9, -3] becomes [9, -3, 6, 1, 3] in zigzag order. | |
| Specifically, your job is to complete the blank function zigzagArray to accomplish this task within a reasonable time. zigzagArray takes one argument, an array intArray of n integers, and returns an identically-sized array with the elements arranged in zigzag order. Code to process input and output is already present in the system and guaranteed to work with the format of the test case files. There is no need to modify that part of the program. | |
| Constraints | |
| 2 ≤ n ≤ 1,000,000 | |
| -1,000,000 ≤ intArray[i] ≤ 1,000,000 ∀ i ∈ [1, n] | |
| The elements of intArray may or may not include duplicates. | |
| Sample Input 1: | |
| intArray = [5, 2, 7, 8, -2, 25, 25] | |
| Sample Output 1: | |
| [25, -2, 25, 2, 8, 5, 7] | |
| Sample Input 2: | |
| intArray = [-27676, 211621, 904304, 161270, -292822, 732004, 860511, -825806, -721722, 536428, -927571, -287004] | |
| Sample Output 2: | |
| [904304, -927571, 860511, -825806, 732004, -721722, 536428, -292822, 211621, -287004, 161270, -27676] | |
| Note: We know that the zigzag function as described may not guarantee the largest cumulative travel distance for all inputs. | |
| Your task is to complete the zigzag function as specified, not to modify it to actually maximize distance. | |
| function zigzagArray(intArray) { | |
| intArray.sort((a,b) => a - b); | |
| var res =[]; | |
| while(intArray.length) { | |
| if( res.length % 2 ) { | |
| // odd loop | |
| res.push(intArray.shift()); | |
| } else { | |
| // even loop | |
| res.push(intArray.pop()); | |
| } | |
| } | |
| return res; | |
| } | |
| zigzagArray([5, 2, 7, 8,-2, 25, 25]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment