📜  计算数组中两个给定元素之间的元素数

📅  最后修改于: 2021-04-27 09:17:31             🧑  作者: Mango

给定n个元素的未排序数组,并给定两个点num1和num2。任务是计算给定点之间(不包括num1和num2)之间发生的元素数量。
如果存在num1和num2多次出现,则需要考虑最左边的num1和最右边的num2。
例子:

Input : arr[] = {3 5 7 6 4 9 12 4 8}
        num1 = 5
        num2 = 4
Output : 5
Number of elements between leftmost occurrence
of 5 and rightmost occurrence of 4 is five.

Input : arr[] = {4, 6, 8, 3, 6, 2, 8, 9, 4}
        num1 = 4
        num2 = 4
Output : 7

Input : arr[] = {4, 6, 8, 3, 6, 2, 8, 9, 4}
        num1 = 4
        num2 = 10
Output : 0

在所有情况下(当不存在单个或两个元素时),解决方案应仅遍历数组一次

这个想法是从左边遍历数组并找到第一个出现的num1。如果到达末尾,则返回0。然后从最右边的元素开始遍历并找到num2。我们仅遍历直到大于num1的索引的点。如果到达末尾,则返回0。如果找到两个元素,则使用找到的元素的索引返回计数。

CPP
// Program to count number of elements between
// two given elements.
#include 
using namespace std;
 
// Function to count number of elements
// occurs between the elements.
int getCount(int arr[], int n, int num1, int num2)
{
    // Find num1
    int i = 0;
    for (i = 0; i < n; i++)
        if (arr[i] == num1)
            break;
 
    // If num1 is not present or present at end
    if (i >= n-1)
        return 0;
 
    // Find num2
    int j;
    for (j = n-1; j >= i+1; j--)
        if (arr[j] == num2)
            break;
 
    // If num2 is not present
    if (j == i)
        return 0;
 
    // return number of elements between
    // the two elements.
    return (j - i - 1);
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 5, 7, 6, 4, 9, 12, 4, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int num1 = 5, num2 = 4;
    cout << getCount(arr, n, num1, num2);
    return 0;
}


Java
// Program to count number of elements
// between two given elements.
import java.io.*;
 
class GFG
{
    // Function to count number of elements
    // occurs between the elements.
    static int getCount(int arr[], int n,
                            int num1, int num2)
    {
        // Find num1
        int i = 0;
        for (i = 0; i < n; i++)
            if (arr[i] == num1)
                break;
     
        // If num1 is not present
        // or present at end
        if (i >= n - 1)
            return 0;
     
        // Find num2
        int j;
        for (j = n - 1; j >= i + 1; j--)
            if (arr[j] == num2)
                break;
     
        // If num2 is not present
        if (j == i)
            return 0;
     
        // return number of elements
        // between the two elements.
        return (j - i - 1);
    }
     
    // Driver program
    public static void main (String[] args)
    {
        int arr[] = { 3, 5, 7, 6, 4, 9, 12, 4, 8 };
        int n = arr.length;
        int num1 = 5, num2 = 4;
        System.out.println( getCount(arr, n, num1, num2));
 
    }
}
// This code is contributed by vt_m


Python3
# Python Program to count number of elements between
# two given elements.
 
# Function to count number of elements
# occurs between the elements.
def getCount(arr, n, num1, num2):
     
    # Find num1
    for i in range(0,n):
        if (arr[i] == num1):
            break
             
    #If num1 is not present or present at end
    if (i >= n-1):
        return 0
 
    # Find num2
    for j in range(n-1, i+1, -1):
        if (arr[j] == num2):
            break
 
    # If num2 is not present
    if (j == i):
        return 0
 
    # return number of elements between
    # the two elements.
    return (j - i - 1)
 
# Driver Code
arr= [ 3, 5, 7, 6, 4, 9, 12, 4, 8 ]
n=len(arr)
num1 = 5
num2 = 4
print(getCount(arr, n, num1, num2))
 
# This code is contributed by SHARIQ_JMI


C#
// C# Program to count number of elements
// between two given elements.
using System;
 
class GFG  {
     
    // Function to count number of elements
    // occurs between the elements.
    static int getCount(int []arr, int n,
                        int num1, int num2)
    {
         
        // Find num1
        int i = 0;
        for (i = 0; i < n; i++)
            if (arr[i] == num1)
                break;
     
        // If num1 is not present
        // or present at end
        if (i >= n - 1)
            return 0;
     
        // Find num2
        int j;
        for (j = n - 1; j >= i + 1; j--)
            if (arr[j] == num2)
                break;
     
        // If num2 is not present
        if (j == i)
            return 0;
     
        // return number of elements
        // between the two elements.
        return (j - i - 1);
    }
     
    // Driver Code
    public static void Main ()
    {
        int []arr = {3, 5, 7, 6, 4, 9, 12, 4, 8};
        int n = arr.Length;
        int num1 = 5, num2 = 4;
        Console.WriteLine(getCount(arr, n, num1, num2));
 
    }
}
 
// This article is contributed by vt_m.


PHP
= $n - 1)
        return 0;
 
    // Find num2
    $j;
    for ($j = $n - 1; $j >= $i + 1; $j--)
        if ($arr[$j] == $num2)
            break;
 
    // If num2 is not present
    if ($j == $i)
        return 0;
 
    // return number of elements
    // betweenthe two elements.
    return ($j - $i - 1);
}
 
// Driver Code
$arr = array(3, 5, 7, 6, 4, 9, 12, 4, 8);
$n = sizeof($arr);
$num1 = 5; $num2 = 4;
echo(getCount($arr, $n, $num1, $num2));
 
// This code is contributed by Ajit.
?>


Javascript


输出:

5