Rearranging positive and negative values in an array | GeeksforGeeks

preview_player
Показать описание
Our courses :

Please Like, Comment and Share the Video among your friends.

Install our Android App:

If you wish, translate into the local language and help us reach millions of other geeks:

Follow us on Facebook:

And Twitter:

Also, Subscribe if you haven't already! :)
Рекомендации по теме
Комментарии
Автор

The order of the elements is not maintained in this approach, to maintain the order perform left / right rotation instead of swapping

manisherukulla
Автор

Using brute force method :

"newArray" is the newly created array, "posArray" has positive values in the input array, "negArray" has all the negative elements from the negative array :

for i<len(ar) && (j<len(posArray) || k<len(negArray)) {

if i%2 == 0 && k<len(negArray) {

newArray[i] = negArray[k]
i++
k++
} else {
if j==len(posArray) {
newArray[i] = negArray[k]
i++
k++
} else {
newArray[i] = posArray[j]
i++
j++
}

}

}

this will cover all the edge cases too i guess... but to create positive, negative array we need to traverse the original array once. it increases time complexity.
pls feel free to comment below if any improvements can be made

barathv
Автор

Nice and very detailed explanation thank you and i hope you explain all the upcoming videos

Odo_ela_sabil_rabek
Автор

Ok...but what if we would have 7 negative and one positive elements in a array with 8 indexes..?

soulvocals
Автор

u explained in an awesome way!! it was very nice...thank youuu<3

tanvybhola