Remove all negatives from the given Array
Last Updated :
20 Mar, 2023
Given an array arr[] of size N, the task is to remove all negative elements from this array.
Examples:
Input: arr[] = {3, -4}
Output: {3}
Explanation: The only negative element of the array is -4.
Input: arr[] = {1, -3, 2}
Output: {1, 2}
Approach 1: The given problem can be solved using the following steps :
- Create a vector newArr to store only positive elements.
- Now traverse the array arr, and push positive element in newArr.
- Return newArr as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void removeNegative(vector< int >& arr)
{
vector< int > newArr;
for ( auto x : arr) {
if (x >= 0) {
newArr.push_back(x);
}
}
for ( auto x : newArr) {
cout << x << ' ' ;
}
}
int main()
{
vector< int > arr = { 1, -3, 2 };
removeNegative(arr);
return 0;
}
|
Java
import java.util.ArrayList;
class GFG {
static void removeNegative( int [] arr) {
ArrayList<Integer> newArr = new ArrayList<Integer>();
for ( int x : arr) {
if (x >= 0 ) {
newArr.add(x);
}
}
for ( int x : newArr) {
System.out.print(x + " " );
}
}
public static void main(String args[]) {
int [] arr = { 1 , - 3 , 2 };
removeNegative(arr);
}
}
|
Python3
def removeNegative(arr):
newArr = []
for x in range ( 0 , len (arr)):
if (arr[x] > = 0 ):
newArr.append(arr[x])
for x in range ( 0 , len (newArr)):
print (newArr[x], end = ' ' )
arr = [ 1 , - 3 , 2 ]
removeNegative(arr)
|
C#
using System;
using System.Collections;
class GFG {
static void removeNegative( int [] arr) {
ArrayList newArr = new ArrayList();
foreach ( int x in arr) {
if (x >= 0) {
newArr.Add(x);
}
}
foreach ( int x in newArr) {
Console.Write(x + " " );
}
}
public static void Main() {
int [] arr = { 1, -3, 2 };
removeNegative(arr);
}
}
|
Javascript
<script>
function removeNegative(arr)
{
let newArr = [];
for (let x of arr) {
if (x >= 0) {
newArr.push(x);
}
}
for (let x of newArr) {
document.write(x + " " )
}
}
let arr = [1, -3, 2];
removeNegative(arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Approach 2: Using two pointers
This approach uses two pointers, one to iterate over the array and another to keep track of the next index to place a non-negative number.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 2, 5, -6, 0, -1, 7, -9 };
int n = sizeof (arr) / sizeof (arr[0]);
int pos = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] >= 0)
{
arr[pos] = arr[i];
pos++;
}
}
for ( int i = 0; i < pos; i++)
{
cout << arr[i] << " " ;
}
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
int [] arr = { 2 , 5 , - 6 , 0 , - 1 , 7 , - 9 };
int n = arr.length;
int pos = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] >= 0 ) {
arr[pos] = arr[i];
pos++;
}
}
for ( int i = 0 ; i < pos; i++) {
System.out.print(arr[i] + " " );
}
}
}
|
C#
using System;
public class GFG{
static public void Main (){
int [] arr = { 2, 5, -6, 0, -1, 7, -9 };
int n = arr.Length;
int pos = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] >= 0)
{
arr[pos] = arr[i];
pos++;
}
}
for ( int i = 0; i < pos; i++)
{
Console.Write(arr[i] + " " );
}
}
}
|
Javascript
let arr = [2, 5, -6, 0, -1, 7, -9];
let n = arr.length;
let pos = 0;
for (let i = 0; i < n; i++) {
if (arr[i] >= 0) {
arr[pos] = arr[i];
pos++;
}
}
for (let i = 0; i < pos; i++) {
console.log(arr[i] + " " );
}
|
Python3
arr = [ 2 , 5 , - 6 , 0 , - 1 , 7 , - 9 ]
n = len (arr)
pos = 0
for i in range (n):
if arr[i] > = 0 :
arr[pos] = arr[i]
pos + = 1
for i in range (pos):
print (arr[i], end = " " )
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Approach 3(For Java): Java’s lambda function removeIf() can be used as well to remove negative elements.
Java
import java.util.*;
public class Solution {
public static ArrayList<Integer>
remNeg(ArrayList<Integer> arr)
{
arr.removeIf(n -> n < 0 );
return arr;
}
public static void main(String[] args)
{
ArrayList<Integer> arr
= new ArrayList<Integer>(
Arrays.asList( 1 , - 3 , 2 ));
arr = remNeg(arr);
for ( int i = 0 ; i < arr.size(); i++) {
System.out.print(arr.get(i) + " " );
}
}
}
|
C#
using System;
using System.Collections.Generic;
public class Solution {
static bool isEven( int i)
{
return i < 0;
}
static List< int >
remNeg(List< int > arr)
{
arr.RemoveAll(isEven);
return arr;
}
public static void Main(String[] args)
{
List< int > arr
= new List< int >( new int []{1, -3, 2});
arr = remNeg(arr);
for ( int i = 0; i < arr.Count; i++) {
Console.Write(arr[i] + " " );
}
}
}
|
Javascript
<script>
function remNeg( arr) {
for ( var i = 0; i < arr.length; i++) {
if (arr[i] < 0){
arr.splice(i,1);
}
}
return arr;
}
var arr = [1, -3, 2];
arr = remNeg(arr);
for (i = 0; i < arr.length; i++) {
document.write(arr[i] + " " );
}
</script>
|
Python3
from typing import List
def remNeg(arr: List [ int ]) - > List [ int ]:
arr = [x for x in arr if x > = 0 ]
return arr
if __name__ = = "__main__" :
arr = [ 1 , - 3 , 2 ]
arr = remNeg(arr)
for i in arr:
print (i, end = " " )
|
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector< int > remNeg(vector< int > arr)
{
arr.erase(remove_if(arr.begin(), arr.end(), []( int n) { return n < 0;}), arr.end());
return arr;
}
int main()
{
vector< int > arr {1, -3, 2};
arr = remNeg(arr);
for ( int i = 0; i < arr.size(); i++) {
cout << arr[i] << " " ;
}
return 0;
}
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...