Find final Array by removing adjacent elements with different sign
Last Updated :
28 Aug, 2023
Given an array arr[] of positive and negative integers. If two adjacent elements have different signs, remove them. Find the final array if the operation is performed repeatedly.
Examples:
Input: arr[] = {4, 2, -2, 1}
Output: 4 1
Explanation: As at indices 1 and 2, 2 and -2 have different sign, they are removed. And the the final array is: 4 1.
Input: arr[] = {2, -2, 1, -1}
Output: []
Explanation: As at indices 0 and 1, 2 and -2 have different sign, so they are removed. Now the array is 1 -1.Now 1 and -1 are also removed as they have different sign. So the final array is empty.
Approach: The problem can be solved based on the following idea:
Create a vector and keep storing the elements if the current element and the last inserted element in the vector have the same sign. Otherwise, remove the last inserted element from the vector.
Follow the steps mentioned below to implement the idea:
- Define a vector of integers (say ans[]) to store the final state of the array.
- Start iterating the array arr[]:
- If the last element in ans[] has the same sign as the current element of arr[], then push the current element into ans[].
- If the last element in ans[] does not have the same sign as the current element, then pop the last element of the answer vector.
- Return ans[] as the required final array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > makeBeautiful(vector< int > arr)
{
vector< int > ans;
for ( int i = 0; i < arr.size(); i++) {
if (ans.size() == 0
|| ans.back() >= 0 && arr[i] >= 0
|| ans.back() < 0 && arr[i] < 0)
ans.push_back(arr[i]);
else
ans.pop_back();
}
return ans;
}
int main()
{
vector< int > arr = { 4, 2, -2, 1 };
vector< int > res;
res = makeBeautiful(arr);
for ( int x : res)
cout << x << " " ;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static List<Integer>makeBeautiful(List<Integer> arr)
{
List<Integer> ans = new ArrayList<Integer>();
for ( int i = 0 ; i < arr.size(); i++) {
if (ans.size() == 0
|| ans.get(ans.size() - 1 ) >= 0
&& arr.get(i) >= 0
|| ans.get(ans.size() - 1 ) < 0
&& arr.get(i) < 0 )
ans.add(arr.get(i));
else
ans.remove(ans.size() - 1 );
}
return ans;
}
public static void main(String[] args)
{
List<Integer> arr = new ArrayList<Integer>(
Arrays.asList( 4 , 2 , - 2 , 1 ));
List<Integer> res;
res = makeBeautiful(arr);
for ( int x : res)
System.out.print(x + " " );
}
}
|
Python3
def makeBeautiful(arr):
ans = []
for i in range ( len (arr)):
if len (ans) = = 0 or (ans[ - 1 ] > = 0 and arr[i] > = 0 ) or (ans[ - 1 ] < 0 and arr[i] < 0 ):
ans.append(arr[i])
else :
ans.pop()
return ans
arr = [ 4 , 2 , - 2 , 1 ]
res = makeBeautiful(arr)
for x in res:
print (x, end = " " )
|
C#
using System;
using System.Collections.Generic;
public class Solution
{
public static List< int > MakeBeautiful(List< int > arr)
{
List< int > ans = new List< int >();
for ( int i = 0; i < arr.Count; i++)
{
if (ans.Count == 0 || ans[ans.Count - 1] >= 0 && arr[i] >= 0 || ans[ans.Count - 1] < 0 && arr[i] < 0)
ans.Add(arr[i]);
else
ans.RemoveAt(ans.Count - 1);
}
return ans;
}
public static void Main( string [] args)
{
List< int > arr = new List< int >() { 4, 2, -2, 1 };
List< int > res;
res = MakeBeautiful(arr);
foreach ( int x in res)
Console.Write(x + " " );
}
}
|
Javascript
function makeBeautiful(arr) {
let ans = [];
for (let i = 0; i < arr.length; i++) {
if (ans.length === 0 || ans[ans.length - 1] >= 0 && arr[i] >= 0 || ans[ans.length - 1] < 0 && arr[i] < 0) {
ans.push(arr[i]);
}
else {
ans.pop();
}
}
return ans;
}
let arr = [4, 2, -2, 1];
let res = makeBeautiful(arr);
for (let x of res) {
console.log(x + " " );
}
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...