📜  在数组中的正整数或负整数的计数中找到最大值

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

在数组中的正整数或负整数的计数中找到最大值

给定一个由N个整数组成的排序数组arr[] ,任务是在数组arr[]中的正整数或负整数的计数中找到最大值。

例子:

方法:给定的问题可以通过使用二分搜索来解决,其思想是找到第一个值为正的索引,然后打印idx(N - idx)的最大值作为结果。请按照以下步骤解决给定的问题:

  • 初始化两个变量,比如0(N – 1)
  • 执行二分搜索 在给定的数组arr[]上通过迭代直到low <= high并按照以下步骤操作:
    • 找到mid的值为(low + high) / 2
    • 如果arr[mid]的值为,则通过将high的值更新为(mid – 1)来跳过右半部分。否则,通过将low的值更新为(mid + 1)来跳过左半部分。
  • 完成上述步骤后,打印出low的最大值和(N-low)作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include "bits/stdc++.h"
using namespace std;
 
// Function to find the maximum of the
// count of positive or negative elements
int findMaximum(int arr[], int size)
{
 
    // Initialize the pointers
    int i = 0, j = size - 1, mid;
 
    while (i <= j) {
 
        // Find the value of mid
        mid = i + (j - i) / 2;
 
        // If element is negative then
        // ignore the left half
        if (arr[mid] < 0)
            i = mid + 1;
 
        // If element is positive then
        // ignore the right half
        else if (arr[mid] > 0)
            j = mid - 1;
    }
 
    // Return maximum among the count
    // of positive & negative element
    return max(i, size - i);
}
 
// Driver Code
int main()
{
    int arr[] = { -9, -7, -4, 1, 5, 8, 9 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << findMaximum(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
public class GFG {
     
    // Function to find the maximum of the
    // count of positive or negative elements
    static int findMaximum(int arr[], int size)
    {
     
        // Initialize the pointers
        int i = 0, j = size - 1, mid;
     
        while (i <= j) {
     
            // Find the value of mid
            mid = i + (j - i) / 2;
     
            // If element is negative then
            // ignore the left half
            if (arr[mid] < 0)
                i = mid + 1;
     
            // If element is positive then
            // ignore the right half
            else if (arr[mid] > 0)
                j = mid - 1;
        }
     
        // Return maximum among the count
        // of positive & negative element
        return Math.max(i, size - i);
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int arr[] = { -9, -7, -4, 1, 5, 8, 9 };
        int N = arr.length;
     
        System.out.println(findMaximum(arr, N));
 
    }
 
}
 
// This code is contributed by AnkThon


Python3
# python program for the above approach
 
# Function to find the maximum of the
# count of positive or negative elements
def findMaximum(arr, size):
 
    # Initialize the pointers
    i = 0
    j = size - 1
 
    while (i <= j):
 
         # Find the value of mid
        mid = i + (j - i) // 2
 
        # If element is negative then
        # ignore the left half
        if (arr[mid] < 0):
            i = mid + 1
 
            # If element is positive then
            # ignore the right half
        elif (arr[mid] > 0):
            j = mid - 1
 
        # Return maximum among the count
        # of positive & negative element
    return max(i, size - i)
 
# Driver Code
if __name__ == "__main__":
 
    arr = [-9, -7, -4, 1, 5, 8, 9]
    N = len(arr)
 
    print(findMaximum(arr, N))
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
 
public class GFG
{
     
    // Function to find the maximum of the
    // count of positive or negative elements
    static int findMaximum(int []arr, int size)
    {
     
        // Initialize the pointers
        int i = 0, j = size - 1, mid;
     
        while (i <= j) {
     
            // Find the value of mid
            mid = i + (j - i) / 2;
     
            // If element is negative then
            // ignore the left half
            if (arr[mid] < 0)
                i = mid + 1;
     
            // If element is positive then
            // ignore the right half
            else if (arr[mid] > 0)
                j = mid - 1;
        }
     
        // Return maximum among the count
        // of positive & negative element
        return Math.Max(i, size - i);
    }
     
    // Driver Code
    public static void Main (string[] args)
    {
        int []arr = { -9, -7, -4, 1, 5, 8, 9 };
        int N = arr.Length;
     
        Console.WriteLine(findMaximum(arr, N));
    }
}
 
// This code is contributed by AnkThon


Javascript


输出:
4

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