[09 Feb 2020] Rearrange positive and negative numbers with constant extra space
Rearrange positive and negative numbers with constant extra space
Given an array of positive and negative numbers, arrange them such that all negative integers appear before all the positive integers in the array without using any additional data structure like hash table, arrays, etc. The order of appearance should be maintained.
Examples:
Input: [12 11 -13 -5 6 -7 5 -3 -6]
Output:
-12 -13 -5 -7 -3 -6 11 6 5
Output:
-12 -13 -5 -7 -3 -6 11 6 5
Output:
-12 -13 -5 -7 -3 -6 11 6 5
Output:
-12 -13 -5 -7 -3 -6 11 6 5
Time complexity of above solution is O(n2) and auxiliary space is O(1). We have maintained the order of appearance and have not used any other data structure.
Output:
-12 -13 -5 -7 -3 -6 11 6 5
Output:
-12 -13 -5 -7 -3 -6 11 6 5
Comments
Post a Comment