📌  相关文章
📜  所有元素均大于X的段数

📅  最后修改于: 2021-04-22 03:00:20             🧑  作者: Mango

给定一个由N个整数组成的数组和一个数字X,任务是查找段的数量,以使该段中的所有元素都大于X。该计数不应包括重叠的段,并且两个或多个连续的段应被视为一个。

例子:

方法:标志计数器初始化为false ,每当出现任何大于X的元素时,将其标记为true。每当任何元素不大于X时,如果标志true ,则对段进行计数。每当出现任何<= X元素时,将标志重新初始化为false。

下面是上述方法的实现。

C++
// C++ program to count the number of segments
// in which all elements are greater than X
#include 
using namespace std;
  
// Function to count number of segments
int countSegments(int a[], int n, int x)
{
    bool flag = false;
  
    int count = 0;
  
    // Iterate in the array
    for (int i = 0; i < n; i++) {
  
        // check if array element
        // greater then X or not
        if (a[i] > x) {
            flag = true;
        }
        else {
  
            // if flag is true
            if (flag)
                count += 1;
  
            flag = false;
        }
    }
  
    // After iteration complete
    // check for the last segment
    if (flag)
        count += 1;
  
    return count;
}
  
// Driver Code
int main()
{
    int a[] = { 8, 25, 10, 19, 19, 18, 20, 11, 18 };
    int n = sizeof(a) / sizeof(a[0]);
    int x = 13;
    cout << countSegments(a, n, x);
    return 0;
}


Java
// Java program to count the number of segments
// in which all elements are greater than X
  
import java.io.*;
  
class GFG {
  
  
// Function to count number of segments
static int countSegments(int a[], int n, int x)
{
    boolean flag = false;
  
    int count = 0;
  
    // Iterate in the array
    for (int i = 0; i < n; i++) {
  
        // check if array element
        // greater then X or not
        if (a[i] > x) {
            flag = true;
        }
        else {
  
            // if flag is true
            if (flag)
                count += 1;
  
            flag = false;
        }
    }
  
    // After iteration complete
    // check for the last segment
    if (flag)
        count += 1;
  
    return count;
}
  
// Driver Code
  
    public static void main (String[] args) {
        int a[] = { 8, 25, 10, 19, 19, 18, 20, 11, 18 };
    int n =a.length;
    int x = 13;
    System.out.println(countSegments(a, n, x));
    }
}
// This code is contributed by anuj_67..


Python3
# Python 3 program to count the number of segments
# in which all elements are greater than X
  
# Function to count number of segments
def countSegments(a, n, x):
    flag = False
  
    count = 0
  
    # Iterate in the array
    for i in range(n):
          
        # check if array element greater 
        # then X or not
        if (a[i] > x):
            flag = True
          
        else:
              
            # if flag is true
            if (flag):
                count += 1
  
            flag = False
      
    # After iteration complete
    # check for the last segment
    if (flag):
        count += 1
  
    return count
  
# Driver Code
if __name__ == '__main__':
    a = [8, 25, 10, 19, 19, 18, 20, 11, 18]
    n = len(a)
    x = 13
    print(countSegments(a, n, x))
  
# This code is contributed by
# Sanjit_Prasad


C#
// C# program to count the number of segments 
// in which all elements are greater than X 
using System;
  
public class GFG{
      
// Function to count number of segments 
static int countSegments(int []a, int n, int x) 
{ 
    bool flag = false; 
  
    int count = 0; 
  
    // Iterate in the array 
    for (int i = 0; i < n; i++) { 
  
        // check if array element 
        // greater then X or not 
        if (a[i] > x) { 
            flag = true; 
        } 
        else { 
  
            // if flag is true 
            if (flag) 
                count += 1; 
  
            flag = false; 
        } 
    } 
  
    // After iteration complete 
    // check for the last segment 
    if (flag) 
        count += 1; 
  
    return count; 
} 
  
    static public void Main (){
      
    int []a = { 8, 25, 10, 19, 19, 18, 20, 11, 18 }; 
    int n =a.Length; 
    int x = 13; 
    Console.WriteLine(countSegments(a, n, x)); 
    } 
}


PHP
 $x) 
        { 
            $flag = true; 
        } 
        else 
        { 
  
            // if flag is true 
            if ($flag) 
                $count += 1; 
  
            $flag = false; 
        } 
    } 
  
    // After iteration complete 
    // check for the last segment 
    if ($flag) 
        $count += 1; 
  
    return $count; 
} 
  
// Driver Code 
$a = array( 8, 25, 10, 19, 19, 
            18, 20, 11, 18 ); 
$n = sizeof($a); 
$x = 13; 
echo countSegments($a, $n, $x); 
  
// This code is contributed by ajit
?>


输出:
3

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