CA 09 - Move All Negative Elements to end

Move all negative elements to end

Difficulty: EasyAccuracy: 56.24%Submissions: 174K+Points: 2

Given an unsorted array arr[ ] having both negative and positive integers. The task is to place all negative elements at the end of the array without changing the order of positive elements and negative elements.

NoteDon't return any array, just in-place on the array.

Examples:

Input : arr[] = [1, -1, 3, 2, -7, -5, 11, 6 ]
Output : [1, 3, 2, 11, 6, -1, -7, -5]
Explanation: By doing operations we separated the integers without changing the order.
Input : arr[] = [-5, 7, -3, -4, 9, 10, -1, 11]
Output : [7, 9, 10, 11, -5, -3, -4, -1]
MY CODE:
class Solution:
    def segregateElements(self, arr):
        a = []
        b = []
        
        for i in arr:
            if i >= 0:
                a.append(i)
            else:
                b.append(i)
        
        temp = a + b
        
        for i in range(len(arr)):
            arr[i] = temp[i]

Comments

Popular posts from this blog

CA 22 - Reverse a Linked List