📌  相关文章
📜  根据相邻数组元素的绝对值最大程度地去除它们

📅  最后修改于: 2021-05-14 01:30:24             🧑  作者: Mango

给定一个正整数和负整数的数组arr [] ,任务是从数组的最后一个索引开始删除相邻的数组元素后打印该数组。
可以根据以下条件删除数组元素:

  • 带有相反符号的两个相邻元素仅需要比较。
  • 具有相反符号的两个相邻元素的较小绝对值将被丢弃。
  • 具有相同符号的两个相等的绝对值相邻元素都将被丢弃。
  • 如果结果数组为空,则打印-1。

例子:

方法:
要解决上述问题,请按照以下步骤操作:

  • 遍历给定的输入数组,如果元素为正,则将其插入向量中。
  • 如果元素为负,则以相反的方式遍历最终向量,并检查向量中的最后一个元素为正还是负,从而找到该整数的正确状态。
  • 如果为正,则通过比较两个整数的绝对值来检查其中哪一个将被丢弃。
  • 如果向量为空或其中的最后一个元素为负,则将当前元素推入最终向量。
  • 如果通过比较两个整数的绝对值得到相同的值,则丢弃向量中的最后一个元素。

下面是上述方法的实现:

C++
// C++ Program to maximize removals
// of adjacent array elements based
// on their absolute value
#include 
using namespace std;
  
// Function to find remaining
// array elements after removals
void removals(int arr[], int n)
{
    vector v;
  
    for (int i = 0; i < n; i++) {
        // If i-th element is having
        // positive value (moving right)
        if (arr[i] > 0)
            // Push them into
            // the vector
            v.push_back(arr[i]);
  
        else {
            // If the last element of the vector
            // is of opposite sign and is less
            // than the value of current
            // integer then pop that element
            while (!v.empty() && (v.back() > 0)
                   && v.back() < abs(arr[i]))
                v.pop_back();
  
            // Check if vector is empty or the
            // last element has same sign
            if (v.empty() || v.back() < 0)
                v.push_back(arr[i]);
  
            // Check if the value is same
            else if (v.back() == abs(arr[i]))
                v.pop_back();
        }
    }
  
    // If vector is empty
    if (v.size() == 0) {
        cout << -1;
        return;
    }
  
    // Print the final array
    for (int i = 0; i < v.size(); i++) {
        cout << v[i] << " ";
    }
  
    return;
}
  
// Driver Code
int main()
{
    int arr[] = { 2, 7, -9, 3, 8, -5 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    removals(arr, n);
  
    return 0;
}


Java
// Java Program to maximize removals
// of adjacent array elements based
// on their absolute value
import java.util.*;
class GFG{
  
// Function to find remaining
// array elements after removals
static void removals(int arr[], int n)
{
    Vector v = new Vector();
  
    for (int i = 0; i < n; i++) 
    {
        // If i-th element is having
        // positive value (moving right)
        if (arr[i] > 0)
          
            // Push them into
            // the vector
            v.add(arr[i]);
  
        else 
        {
              
            // If the last element of the vector
            // is of opposite sign and is less
            // than the value of current
            // integer then pop that element
            while (!v.isEmpty() && (v.lastElement() > 0) && 
                    v.lastElement() < Math.abs(arr[i]))
                v.remove(v.size()-1);
  
            // Check if vector is empty or the
            // last element has same sign
            if (v.isEmpty() || v.lastElement() < 0)
                v.add(arr[i]);
  
            // Check if the value is same
            else if (v.lastElement() == Math.abs(arr[i]))
                v.remove(v.size() - 1);
        }
    }
  
    // If vector is empty
    if (v.size() == 0)
    {
        System.out.print(-1);
        return;
    }
  
    // Print the final array
    for (int i = 0; i < v.size(); i++) 
    {
        System.out.print(v.get(i) + " ");
    }
    return;
}
  
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 2, 7, -9, 3, 8, -5 };
  
    int n = arr.length;
  
    removals(arr, n);
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program to maximize removals
# of adjacent array elements based
# on their absolute value
  
# Function to find remaining
# array elements after removals
def removals(arr, n):
  
    v = []
  
    for i in range(n):
          
        # If i-th element is having
        # positive value (moving right)
        if (arr[i] > 0):
              
            # Push them into
            # the vector
            v.append(arr[i])
  
        else:
              
            # If the last element of the vector
            # is of opposite sign and is less
            # than the value of current
            # integer then pop that element
            while (len(v) != 0 and v[-1] > 0 and 
                                   v[-1] < abs(arr[i])):
                v.pop()
  
            # Check if vector is empty or the
            # last element has same sign
            if (len(v) == 0 or v[-1] < 0):
                v.append(arr[i])
  
            # Check if the value is same
            elif (v[-1] == abs(arr[i])):
                v.pop()
  
    # If vector is empty
    if (len(v) == 0):
        print(-1)
        return
  
    # Print the final array
    for i in range(len(v)):
        print(v[i], end = " ")
  
    return
  
# Driver Code
if __name__ == "__main__":
      
    arr = [ 2, 7, -9, 3, 8, -5 ]
    n = len(arr)
      
    removals(arr, n)
  
# This code is contributed by chitranayal


C#
// C# program to maximize removals 
// of adjacent array elements based 
// on their absolute value 
using System;
using System.Collections.Generic;
  
class GFG{ 
  
// Function to find remaining 
// array elements after removals 
static void removals(int []arr, int n) 
{ 
    List v = new List(); 
  
    for(int i = 0; i < n; i++) 
    { 
  
        // If i-th element is having 
        // positive value (moving right) 
        if (arr[i] > 0) 
          
            // Push them into 
            // the vector 
            v.Add(arr[i]); 
  
        else
        { 
              
            // If the last element of the vector 
            // is of opposite sign and is less 
            // than the value of current 
            // integer then pop that element 
            while (v.Count != 0 && (v[v.Count - 1] > 0) && 
                 v[v.Count - 1] < Math.Abs(arr[i])) 
                v.RemoveAt(v.Count - 1); 
  
            // Check if vector is empty or the 
            // last element has same sign 
            if (v.Count == 0 || v[v.Count - 1] < 0) 
                v.Add(arr[i]); 
  
            // Check if the value is same 
            else if (v[v.Count - 1] == Math.Abs(arr[i])) 
                v.RemoveAt(v.Count - 1); 
        } 
    } 
  
    // If vector is empty 
    if (v.Count == 0) 
    { 
        Console.Write(-1); 
        return; 
    } 
  
    // Print the readonly array 
    for(int i = 0; i < v.Count; i++) 
    { 
        Console.Write(v[i] + " "); 
    } 
    return; 
} 
  
// Driver Code 
public static void Main(String[] args) 
{ 
    int []arr = { 2, 7, -9, 3, 8, -5 }; 
  
    int n = arr.Length; 
  
    removals(arr, n); 
} 
} 
  
// This code is contributed by 29AjayKumar


输出:
-9 3 8