`{"id":"1e287278-2279-4446-aa40-c5bb6519bd1c","name":"Wiggle Sort 2","description":"1. Given an integer array 'arr'.\r\n2. Reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....\r\n3. You may assume the input array always has a valid answer.\r\nNote : You can return answer in any order.","inputFormat":"Input: nums = [1,5,1,1,6,4]\r\n","outputFormat":"Output: [1,6,1,5,1,4]\r\nExplanation: [1,4,1,5,1,6] is also accepted.","constraints":"1. 1 &lt;= nums.length &lt;= 10^4\r\n2. 0 &lt;= nums[i] &lt;= 5000\r\n3. It is guaranteed that there will be an answer for the given input 'arr'.\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid wiggleSort2(vector<int>& nums)\n{\n //write your code here\n}\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\n wiggleSort2(arr);\n\n for(int i=0; i <n-1; i++)\n {\n if(i%2 == 0 && arr[i] >= arr[i+1])\n {\n cout<<false;\n return false;\n }\n else if(i%2 == 1 && arr[i] <= arr[i+1])\n {\n cout<<false;\n return false;\n }\n }\n\n cout<<\"true\";\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~User''s Section~~~~~~~~~~~~\r\n public static void wiggleSort2(int[] nums) {\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 int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n wiggleSort2(arr);\r\n /*\r\n -if index is even then smaller than next,\r\n -if index is odd then greater than next element,\r\n -to check any correct order, we will not print array, we will check inequality\r\n -if print false, that means wrong answer, if true than correct answer.\r\n */\r\n for (int i = 0; i < n - 1; i++) {\r\n if (i % 2 == 0 && arr[i] >= arr[i + 1]) {\r\n System.out.println(false);\r\n return;\r\n } else if (i % 2 == 1 && arr[i] <= arr[i + 1]) {\r\n System.out.println(false);\r\n return;\r\n }\r\n }\r\n System.out.println(true);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"6\r\n1 5 1 1 6 4","sampleOutput":"true","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":"998b6be9-479a-4796-af66-b8c7658a49a5","name":"Wiggle Sort 2","slug":"wiggle-sort-2","type":1}],"next":{"id":"8ff882d4-18d0-4423-8608-d2914469b657","name":"Wiggle Sort - 2","type":3,"slug":"wiggle-sort-2"},"prev":{"id":"6597302a-d474-4f73-82f9-82ad85cf04e8","name":"Wiggle Sort 1 MCQ","type":0,"slug":"wiggle-sort-1-mcq"}}}`

# Wiggle Sort 2

1. Given an integer array 'arr'. 2. Reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... 3. You may assume the input array always has a valid answer. Note : You can return answer in any order.

`{"id":"1e287278-2279-4446-aa40-c5bb6519bd1c","name":"Wiggle Sort 2","description":"1. Given an integer array 'arr'.\r\n2. Reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....\r\n3. You may assume the input array always has a valid answer.\r\nNote : You can return answer in any order.","inputFormat":"Input: nums = [1,5,1,1,6,4]\r\n","outputFormat":"Output: [1,6,1,5,1,4]\r\nExplanation: [1,4,1,5,1,6] is also accepted.","constraints":"1. 1 &lt;= nums.length &lt;= 10^4\r\n2. 0 &lt;= nums[i] &lt;= 5000\r\n3. It is guaranteed that there will be an answer for the given input 'arr'.\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid wiggleSort2(vector<int>& nums)\n{\n //write your code here\n}\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\n wiggleSort2(arr);\n\n for(int i=0; i <n-1; i++)\n {\n if(i%2 == 0 && arr[i] >= arr[i+1])\n {\n cout<<false;\n return false;\n }\n else if(i%2 == 1 && arr[i] <= arr[i+1])\n {\n cout<<false;\n return false;\n }\n }\n\n cout<<\"true\";\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~User''s Section~~~~~~~~~~~~\r\n public static void wiggleSort2(int[] nums) {\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 int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n wiggleSort2(arr);\r\n /*\r\n -if index is even then smaller than next,\r\n -if index is odd then greater than next element,\r\n -to check any correct order, we will not print array, we will check inequality\r\n -if print false, that means wrong answer, if true than correct answer.\r\n */\r\n for (int i = 0; i < n - 1; i++) {\r\n if (i % 2 == 0 && arr[i] >= arr[i + 1]) {\r\n System.out.println(false);\r\n return;\r\n } else if (i % 2 == 1 && arr[i] <= arr[i + 1]) {\r\n System.out.println(false);\r\n return;\r\n }\r\n }\r\n System.out.println(true);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"6\r\n1 5 1 1 6 4","sampleOutput":"true","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":"998b6be9-479a-4796-af66-b8c7658a49a5","name":"Wiggle Sort 2","slug":"wiggle-sort-2","type":1}],"next":{"id":"8ff882d4-18d0-4423-8608-d2914469b657","name":"Wiggle Sort - 2","type":3,"slug":"wiggle-sort-2"},"prev":{"id":"6597302a-d474-4f73-82f9-82ad85cf04e8","name":"Wiggle Sort 1 MCQ","type":0,"slug":"wiggle-sort-1-mcq"}}}`

Editor

# Wiggle Sort 2

easy

1. Given an integer array 'arr'. 2. Reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... 3. You may assume the input array always has a valid answer. Note : You can return answer in any order.

## Constraints

1. 1 <= nums.length <= 10^4 2. 0 <= nums[i] <= 5000 3. It is guaranteed that there will be an answer for the given input 'arr'.

## Format

### Input

Input: nums = [1,5,1,1,6,4]

### Output

Output: [1,6,1,5,1,4] Explanation: [1,4,1,5,1,6] is also accepted.

## 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 1 5 1 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;}true`

Discussions

Show Discussion

Related Resources