📌  相关文章
📜  从所有 Array 元素中重复减去最小正元素后,找到最后一个正元素

📅  最后修改于: 2022-05-13 01:56:05.038000             🧑  作者: Mango

从所有 Array 元素中重复减去最小正元素后,找到最后一个正元素

给定一个由N个正整数组成的数组arr[] ,任务是从所有数组元素中重复减去最小的正数组元素后,找到最后一个正数组元素。

例子:

朴素方法:解决给定问题的最简单方法是遍历给定数组arr[]并找到数组中最小的正元素并将其从所有元素中减去。执行此操作,直到数组中只剩下一个正元素。完成上述步骤后,打印剩余的正数组元素。

时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:上述方法可以通过观察最后一个正元素将是最大数组元素第二大数组元素之间的差来优化。请按照以下步骤解决问题:

  • 如果N = 1 ,则打印数组的第一个元素。
  • 否则打印最大第二大数组元素之间的差异。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate last positive
// element of the array
int lastPositiveElement(vector arr)
{
    int N = arr.size();
 
    // Return the first element if N = 1
    if (N == 1)
        return arr[0];
 
    // Stores the greatest and the second
    // greatest element
    int greatest = -1, secondGreatest = -1;
 
    // Traverse the array A[]
    for (int x : arr) {
 
        // If current element is greater
        // than the greatest element
        if (x >= greatest) {
            secondGreatest = greatest;
            greatest = x;
        }
 
        // If current element is greater
        // than second greatest element
        else if (x >= secondGreatest) {
            secondGreatest = x;
        }
    }
 
    // Return the final answer
    return greatest - secondGreatest;
}
 
// Driver Code
int main()
{
    vector arr = { 3, 5, 4, 7 };
    cout << lastPositiveElement(arr);
 
    return 0;
}


Java
// JAVA program for the above approach
import java.util.*;
class GFG{
 
// Function to calculate last positive
// element of the array
static int lastPositiveElement(int[] arr)
{
    int N = arr.length;
 
    // Return the first element if N = 1
    if (N == 1)
        return arr[0];
 
    // Stores the greatest and the second
    // greatest element
    int greatest = -1, secondGreatest = -1;
 
    // Traverse the array A[]
    for (int x : arr) {
 
        // If current element is greater
        // than the greatest element
        if (x >= greatest) {
            secondGreatest = greatest;
            greatest = x;
        }
 
        // If current element is greater
        // than second greatest element
        else if (x >= secondGreatest) {
            secondGreatest = x;
        }
    }
 
    // Return the final answer
    return greatest - secondGreatest;
}
 
// Driver Code
public static void main(String[] args){
 
    int[] arr = { 3, 5, 4, 7 };
    System.out.print(lastPositiveElement(arr));
}
}
 
// This code is contributed by sanjoy_62.


Python3
# Python3 program for the above approach
 
# Function to calculate last positive
# element of the array
def lastPositiveElement(arr) :
 
    N = len(arr);
 
    # Return the first element if N = 1
    if (N == 1) :
        return arr[0];
 
    # Stores the greatest and the second
    # greatest element
    greatest = -1; secondGreatest = -1;
 
    # Traverse the array A[]
    for x in arr :
 
        # If current element is greater
        # than the greatest element
        if (x >= greatest) :
            secondGreatest = greatest;
            greatest = x;
 
        # If current element is greater
        # than second greatest element
        elif (x >= secondGreatest) :
            secondGreatest = x;
 
    # Return the final answer
    return greatest - secondGreatest;
 
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 3, 5, 4, 7 ];
    print(lastPositiveElement(arr));
 
    # This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
 
class GFG {
 
    // Function to calculate last positive
    // element of the array
    static int lastPositiveElement(int[] arr)
    {
        int N = arr.Length;
 
        // Return the first element if N = 1
        if (N == 1)
            return arr[0];
 
        // Stores the greatest and the second
        // greatest element
        int greatest = -1, secondGreatest = -1;
 
        // Traverse the array A[]
        for (int x = 0; x < N; x++) {
 
            // If current element is greater
            // than the greatest element
            if (arr[x] >= greatest) {
                secondGreatest = greatest;
                greatest = arr[x];
            }
 
            // If current element is greater
            // than second greatest element
            else if (arr[x] >= secondGreatest) {
                secondGreatest = arr[x];
            }
        }
 
        // Return the final answer
        return greatest - secondGreatest;
    }
 
    // Driver Code
    public static void Main()
    {
 
        int[] arr = { 3, 5, 4, 7 };
        Console.Write(lastPositiveElement(arr));
    }
}
 
// This code is contributed by subhammahato348.


Javascript


输出:
2

时间复杂度: O(N)
辅助空间: O(1)