`{"id":"357ad677-16ac-4278-8339-f8b483e6d91a","name":"Wiggle Sort 1","description":"1. Given an unsorted array 'arr'.\r\n2. Reorder it in-place such that : arr[0] <= arr[1] >= arr[2] <= arr[3] . . . .\r\n3. Please sort the array in place and do not define additional arrays.\r\n4. Allowed Time Complexity : O(n)","inputFormat":"Input: [3, 5, 2, 1, 6, 4]","outputFormat":"Output: [1, 6, 2, 5, 3, 4]","constraints":"1. arr.length &lt;= 10^5\r\n2. -10^9 &lt;= arr[i] &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid wiggleSort(vector<int> &arr)\n{\n //write your code here\n}\n\nint main()\n{\n int n;\n cin>>n;\n\n vector<int> arr(n);\n\n for(int i=0; i<n; i++)\n {\n cin>>arr[i];\n }\n wiggleSort(arr);\n\n for(int val:arr)\n {\n cout<<val<<\" \";\n }\n cout<<endl;\n\n\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~~User Section~~~~~~~~~~~~~~\r\n public static void wiggleSort(int[] arr) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~~~Input Management~~~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n wiggleSort(arr);\r\n\r\n for (int val : arr) {\r\n System.out.print(val + \" \");\r\n }\r\n System.out.println();\r\n }\r\n\r\n}"},"python":{"code":"def main():\r\n pass\r\n\r\nif __name__ == '__main__':\r\n main()"}},"points":10,"difficulty":"easy","sampleInput":"6\r\n3 5 2 1 6 4","sampleOutput":"3 5 1 6 2 4","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"1c885bcd-4848-49f8-aea7-5d3ae355e113","name":"Wiggle Sort 1","slug":"wiggle-sort-1","type":1}],"next":{"id":"31c7b2c8-121e-41cf-b2fa-e952a8896cca","name":"Wiggle Sort 1","type":3,"slug":"wiggle-sort-1"},"prev":{"id":"d1eae7da-a1ca-4fa2-8236-f096fe23faaf","name":"Partition Array Into Disjoint Intervals MCQ","type":0,"slug":"partition-array-into-disjoint-intervals-mcq"}}}`

# Wiggle Sort 1

1. Given an unsorted array 'arr'. 2. Reorder it in-place such that : arr[0] <= arr[1] >= arr[2] <= arr[3] . . . . 3. Please sort the array in place and do not define additional arrays. 4. Allowed Time Complexity : O(n)

`{"id":"357ad677-16ac-4278-8339-f8b483e6d91a","name":"Wiggle Sort 1","description":"1. Given an unsorted array 'arr'.\r\n2. Reorder it in-place such that : arr[0] <= arr[1] >= arr[2] <= arr[3] . . . .\r\n3. Please sort the array in place and do not define additional arrays.\r\n4. Allowed Time Complexity : O(n)","inputFormat":"Input: [3, 5, 2, 1, 6, 4]","outputFormat":"Output: [1, 6, 2, 5, 3, 4]","constraints":"1. arr.length &lt;= 10^5\r\n2. -10^9 &lt;= arr[i] &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid wiggleSort(vector<int> &arr)\n{\n //write your code here\n}\n\nint main()\n{\n int n;\n cin>>n;\n\n vector<int> arr(n);\n\n for(int i=0; i<n; i++)\n {\n cin>>arr[i];\n }\n wiggleSort(arr);\n\n for(int val:arr)\n {\n cout<<val<<\" \";\n }\n cout<<endl;\n\n\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~~User Section~~~~~~~~~~~~~~\r\n public static void wiggleSort(int[] arr) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~~~Input Management~~~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n wiggleSort(arr);\r\n\r\n for (int val : arr) {\r\n System.out.print(val + \" \");\r\n }\r\n System.out.println();\r\n }\r\n\r\n}"},"python":{"code":"def main():\r\n pass\r\n\r\nif __name__ == '__main__':\r\n main()"}},"points":10,"difficulty":"easy","sampleInput":"6\r\n3 5 2 1 6 4","sampleOutput":"3 5 1 6 2 4","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"1c885bcd-4848-49f8-aea7-5d3ae355e113","name":"Wiggle Sort 1","slug":"wiggle-sort-1","type":1}],"next":{"id":"31c7b2c8-121e-41cf-b2fa-e952a8896cca","name":"Wiggle Sort 1","type":3,"slug":"wiggle-sort-1"},"prev":{"id":"d1eae7da-a1ca-4fa2-8236-f096fe23faaf","name":"Partition Array Into Disjoint Intervals MCQ","type":0,"slug":"partition-array-into-disjoint-intervals-mcq"}}}`

Editor

# Wiggle Sort 1

easy

1. Given an unsorted array 'arr'. 2. Reorder it in-place such that : arr[0]

## Constraints

1. arr.length <= 10^5 2. -10^9 <= arr[i] <= 10^9

## Format

### Input

Input: [3, 5, 2, 1, 6, 4]

### Output

Output: [1, 6, 2, 5, 3, 4]

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}6 3 5 2 1 6 4```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}3 5 1 6 2 4`

Discussions

Show Discussion

Related Resources