📜  在给定的数组中找到唯一的正数或唯一的负数

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

在给定的数组中找到唯一的正数或唯一的负数

给定一个数组arr[]要么完全是正整数,要么完全是负整数,除了一个数字。任务是找到那个号码。

例子

方法:给定的问题可以通过比较 arr[] 的前三个数字来解决。之后应用线性搜索并找到数字。请按照以下步骤解决问题。

  • 如果arr[]的大小小于3 ,则返回0
  • 初始化变量CpCn
  • 其中Cp = 正整数计数, Cn = 负整数计数
  • 迭代前3 个元素
    • 如果(arr[i]>0) ,将Cp增加1
    • 否则将Cn增加1
  • Cp可以是23 ,类似地, Cn也可以是23
  • 如果Cp ,则单个元素为正,应用线性搜索并找到它。
  • 如果Cn ,则单个元素是否定的,应用线性搜索并找到它。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to return the single element
int find(int* a, int N)
{
    // Size can not be smaller than 3
    if (N < 3)
        return 0;
 
    // Initialize the variable
    int i, Cp = 0, Cn = 0;
 
    // Check the single element is
    // positive or negative
    for (i = 0; i < 3; i++) {
 
        if (a[i] > 0)
            Cp++;
        else
            Cn++;
    }
 
    // Check for positive single element
    if (Cp < Cn) {
        for (i = 0; i < N; i++)
            if (a[i] > 0)
                return a[i];
    }
 
    // Check for negative single element
    else {
        for (i = 0; i < N; i++)
            if (a[i] < 0)
                return a[i];
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 5, 2, 8, -7, 6, 9 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << find(arr, N);
}


Java
// Java program for the above approach
import java.util.*;
public class GFG {
 
// Function to return the single element
static int find(int []a, int N)
{
    // Size can not be smaller than 3
    if (N < 3)
        return 0;
 
    // Initialize the variable
    int i, Cp = 0, Cn = 0;
 
    // Check the single element is
    // positive or negative
    for (i = 0; i < 3; i++) {
 
        if (a[i] > 0)
            Cp++;
        else
            Cn++;
    }
 
    // Check for positive single element
    if (Cp < Cn) {
        for (i = 0; i < N; i++)
            if (a[i] > 0)
                return a[i];
    }
 
    // Check for negative single element
    else {
        for (i = 0; i < N; i++)
            if (a[i] < 0)
                break;
    }
    return a[i];
}
 
// Driver Code
public static void main(String args[])
{
    int []arr = { 3, 5, 2, 8, -7, 6, 9 };
    int N = arr.length;
    System.out.println(find(arr, N));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# python program for the above approach
 
# Function to return the single element
def find(a, N):
 
    # Size can not be smaller than 3
    if (N < 3):
        return 0
 
    # Initialize the variable
    Cp = 0
    Cn = 0
 
    # Check the single element is
    # positive or negative
    for i in range(0, 3):
 
        if (a[i] > 0):
            Cp += 1
        else:
            Cn += 1
 
        # Check for positive single element
    if (Cp < Cn):
        for i in range(0, N):
            if (a[i] > 0):
                return a[i]
 
        # Check for negative single element
    else:
        for i in range(0, N):
            if (a[i] < 0):
                return a[i]
 
# Driver Code
if __name__ == "__main__":
 
    arr = [3, 5, 2, 8, -7, 6, 9]
    N = len(arr)
 
    print(find(arr, N))
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
class GFG {
 
// Function to return the single element
static int find(int []a, int N)
{
    // Size can not be smaller than 3
    if (N < 3)
        return 0;
 
    // Initialize the variable
    int i, Cp = 0, Cn = 0;
 
    // Check the single element is
    // positive or negative
    for (i = 0; i < 3; i++) {
 
        if (a[i] > 0)
            Cp++;
        else
            Cn++;
    }
 
    // Check for positive single element
    if (Cp < Cn) {
        for (i = 0; i < N; i++)
            if (a[i] > 0)
                return a[i];
    }
 
    // Check for negative single element
    else {
        for (i = 0; i < N; i++)
            if (a[i] < 0)
                break;
    }
    return a[i];
}
 
// Driver Code
public static void Main()
{
    int []arr = { 3, 5, 2, 8, -7, 6, 9 };
    int N = arr.Length;
    Console.Write(find(arr, N));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
-7

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