logo

عدد المصفوفات الفرعية التي يكون الحد الأقصى لعنصرها أكبر من k

نظرا لمجموعة من ن العناصر وعدد صحيح ك . المهمة هي العثور على عدد المصفوفة الفرعية التي تحتوي على الحد الأقصى للعنصر أكبر من K.

أمثلة :  

Input : arr[] = {1 2 3} and k = 2.  
Output : 3
All the possible subarrays of arr[] are
{ 1 } { 2 } { 3 } { 1 2 } { 2 3 }
{ 1 2 3 }.
Their maximum elements are 1 2 3 2 3 3.
There are only 3 maximum elements > 2.
Recommended Practice عدد المصفوفات الفرعية جربه!

النهج 1: حساب المصفوفات الفرعية التي تحتوي على الحد الأقصى للعنصر<= K and then subtracting from total subarrays.

تتمثل الفكرة في التعامل مع المشكلة عن طريق حساب المصفوفات الفرعية التي يكون الحد الأقصى لعنصرها أقل من أو يساوي k لأن حساب هذه المصفوفات الفرعية أسهل. للعثور على عدد المصفوفة الفرعية التي يكون الحد الأقصى لعنصرها أقل من أو يساوي k، قم بإزالة كل العناصر الأكبر من K وابحث عن عدد المصفوفة الفرعية مع العناصر اليسرى. 



بمجرد العثور على العدد أعلاه يمكننا طرحه من n*(n+1)/2 للحصول على النتيجة المطلوبة. لاحظ أنه يمكن أن يكون هناك عدد n*(n+1)/2 محتمل للمصفوفة الفرعية لأي مصفوفة بالحجم n. لذا فإن العثور على عدد المصفوفة الفرعية التي يكون الحد الأقصى لعنصرها أقل من أو يساوي K وطرحه من n*(n+1)/2 يعطينا الإجابة.

وفيما يلي تنفيذ هذا النهج:

C++
// C++ program to count number of subarrays // whose maximum element is greater than K. #include    using namespace std; // Return number of subarrays whose maximum // element is less than or equal to K. int countSubarray(int arr[] int n int k) {  // To store count of subarrays with all  // elements less than or equal to k.  int s = 0;  // Traversing the array.  int i = 0;  while (i < n) {  // If element is greater than k ignore.  if (arr[i] > k) {  i++;  continue;  }  // Counting the subarray length whose  // each element is less than equal to k.  int count = 0;  while (i < n && arr[i] <= k) {  i++;  count++;  }  // Summing number of subarray whose  // maximum element is less than equal to k.  s += ((count * (count + 1)) / 2);  }  return (n * (n + 1) / 2 - s); } // Driven Program int main() {  int arr[] = { 1 2 3 };  int k = 2;  int n = sizeof(arr) / sizeof(arr[0]);  cout << countSubarray(arr n k);  return 0; } 
Java
// Java program to count number of subarrays // whose maximum element is greater than K. import java.util.*; class GFG {  // Return number of subarrays whose maximum  // element is less than or equal to K.  static int countSubarray(int arr[] int n int k)  {  // To store count of subarrays with all  // elements less than or equal to k.  int s = 0;  // Traversing the array.  int i = 0;  while (i < n) {  // If element is greater than k ignore.  if (arr[i] > k) {  i++;  continue;  }  // Counting the subarray length whose  // each element is less than equal to k.  int count = 0;  while (i < n && arr[i] <= k) {  i++;  count++;  }  // Summing number of subarray whose  // maximum element is less than equal to k.  s += ((count * (count + 1)) / 2);  }  return (n * (n + 1) / 2 - s);  }  // Driver code  public static void main(String[] args)  {  int arr[] = { 1 2 3 };  int k = 2;  int n = arr.length;  System.out.print(countSubarray(arr n k));  } } // This code is contributed by Anant Agarwal. 
Python3
# Python program to count # number of subarrays # whose maximum element # is greater than K. # Return number of # subarrays whose maximum # element is less than or equal to K. def countSubarray(arr n k): # To store count of # subarrays with all # elements less than # or equal to k. s = 0 # Traversing the array. i = 0 while (i < n): # If element is greater # than k ignore. if (arr[i] > k): i = i + 1 continue # Counting the subarray # length whose # each element is less # than equal to k. count = 0 while (i < n and arr[i] <= k): i = i + 1 count = count + 1 # Summing number of subarray whose # maximum element is less # than equal to k. s = s + ((count*(count + 1))//2) return (n*(n + 1)//2 - s) # Driver code arr = [1 2 3] k = 2 n = len(arr) print(countSubarray(arr n k)) # This code is contributed # by Anant Agarwal. 
C#
// C# program to count number of subarrays // whose maximum element is greater than K. using System; class GFG {  // Return number of subarrays whose maximum  // element is less than or equal to K.  static int countSubarray(int[] arr int n int k)  {  // To store count of subarrays with all  // elements less than or equal to k.  int s = 0;  // Traversing the array.  int i = 0;  while (i < n) {  // If element is greater than k ignore.  if (arr[i] > k) {  i++;  continue;  }  // Counting the subarray length whose  // each element is less than equal to k.  int count = 0;  while (i < n && arr[i] <= k) {  i++;  count++;  }  // Summing number of subarray whose  // maximum element is less than equal to k.  s += ((count * (count + 1)) / 2);  }  return (n * (n + 1) / 2 - s);  }  // Driver code  public static void Main()  {  int[] arr = {1 2 3};  int k = 2;  int n = arr.Length;  Console.WriteLine(countSubarray(arr n k));  } } // This code is contributed by vt_m. 
JavaScript
<script>  // Javascript program to count number of subarrays  // whose maximum element is greater than K.    // Return number of subarrays whose maximum  // element is less than or equal to K.  function countSubarray(arr n k)  {  // To store count of subarrays with all  // elements less than or equal to k.  let s = 0;    // Traversing the array.  let i = 0;  while (i < n) {    // If element is greater than k ignore.  if (arr[i] > k) {  i++;  continue;  }    // Counting the subarray length whose  // each element is less than equal to k.  let count = 0;  while (i < n && arr[i] <= k) {  i++;  count++;  }    // Summing number of subarray whose  // maximum element is less than equal to k.  s += parseInt((count * (count + 1)) / 2 10);  }    return (n * parseInt((n + 1) / 2 10) - s);  }    let arr = [1 2 3];  let k = 2;  let n = arr.length;  document.write(countSubarray(arr n k));   </script> 
PHP
 // PHP program to count number of subarrays // whose maximum element is greater than K. // Return number of subarrays whose maximum // element is less than or equal to K. function countSubarray( $arr $n $k) { // To store count of subarrays with all // elements less than or equal to k. $s = 0; // Traversing the array. $i = 0; while ($i < $n) { // If element is greater than k // ignore. if ($arr[$i] > $k) { $i++; continue; } // Counting the subarray length  // whose each element is less // than equal to k. $count = 0; while ($i < $n and $arr[$i] <= $k) { $i++; $count++; } // Summing number of subarray whose // maximum element is less than // equal to k. $s += (($count * ($count + 1)) / 2); } return ($n * ($n + 1) / 2 - $s); } // Driven Program $arr = array( 1 2 3 ); $k = 2; $n = count($arr); echo countSubarray($arr $n $k); // This code is contributed by anuj_67. ?> 

الإخراج
3 

تعقيد الوقت: O(ن).
المساحة المساعدة: O(1)

النهج 2: حساب المصفوفات الفرعية التي تحتوي على الحد الأقصى للعنصر> K

في هذا النهج، نقوم ببساطة بإيجاد عدد المصفوفات الفرعية التي يمكن تشكيلها عن طريق تضمين عنصر في الفهرس i وهو أكبر من K. لذلك، إذا افترضنا وصول [أنا] > ك فإن جميع المصفوفات الفرعية التي يوجد فيها هذا العنصر سيكون لها قيمة أكبر من k لذلك نقوم فقط بحساب كل هذه المصفوفات الفرعية لكل عنصر أكبر من K ونضيفها في الإجابة. نقوم أولاً بتهيئة متغيرين السنوات = 0 هذا يحتوي على الإجابة و السابق = -1 يؤدي هذا إلى تتبع فهرس العنصر السابق الذي كان أكبر من K.

للقيام بذلك نحتاج فقط إلى ثلاث قيم لكل arr [ i ] > K .

  1. عدد المصفوفات الفرعية بدءًا من الفهرس أنا . سيكون هذا ( ن - ط ) . ملاحظة: في هذا قمنا بتضمين المصفوفة الفرعية التي تحتوي على عنصر واحد وهو هذا العنصر نفسه. { آر [ أنا ] }
  2. عدد المصفوفات الفرعية التي تنتهي بهذا الفهرس أنا لكن فهرس البداية لهذه المصفوفات الفرعية يكون بعد الفهرس السابق العنصر السابق الذي كان أكبر من K لماذا نفعل هذا؟ لأنه بالنسبة لهذه العناصر يجب أن نكون قد حسبنا إجابتنا بالفعل لذلك لا نريد حساب نفس المصفوفات الفرعية أكثر من مرة. لذلك سوف تكون هذه القيمة ( ط - السابق - 1 ) . ملاحظة: في هذا نطرح 1 لأننا قمنا بالفعل بحساب المصفوفة الفرعية { arr [ i ] } التي لها نفسها كعنصر واحد. انظر ملاحظة النقطة أعلاه. 
  3. عدد المصفوفات الفرعية التي تحتوي على مؤشر بداية أقل من أنا بل أعظم من السابق ومؤشر النهاية أكبر من أنا . لذلك جميع المصفوفات الفرعية التي يوجد فيها arr[i] بينهما. يمكننا حساب ذلك عن طريق ضرب القيمتين أعلاه. دعنا نقول لهم كما ل = ( ن - ط - 1 ) و ص = ( ط - السابق -1 ). الآن نقوم فقط بضرب هذين L و R لأنه لكل فهرس واحد على الجانب الأيسر من i يوجد فهرس R يمكنه إنشاء مصفوفات فرعية مختلفة في الرياضيات الأساسية. لذلك يصبح هذا ل * ر . لاحظ هنا في val L أننا قمنا بالفعل بطرح 1 إذا لم نفعل ذلك، ثم قمنا بتضمين الفهرس i في L*R مما يعني أننا قمنا بتضمين المصفوفات الفرعية من النوع رقم 1 مرة أخرى. انظر النقطة 1.    

وفيما يلي تنفيذ هذا النهج:

C++
// C++ program to count number of subarrays // whose maximum element is greater than K. #include    using namespace std; long long countSubarray(int arr[] int n int k) {  long long ans = 0 ;  int prev = - 1; //prev for keeping track of index of previous element > k;  for(int i = 0 ; i < n ; i++ ) {  if ( arr [ i ] > k ) {  ans += n - i ; //subarrays starting at index i.  ans += i - prev - 1 ; //subarrays ending at index i but starting after prev.  ans += ( n - i - 1 ) * 1LL * ( i - prev - 1 ) ; //subarrays having index i element in between.  prev = i; // updating prev  }  }  return ans; } // Driven Program int main() {  int arr[] = { 4 5 1 2 3 };  int k = 2;  int n = sizeof(arr) / sizeof(arr[0]);  cout << countSubarray(arr n k);  return 0; } // This Code is contributed by Manjeet Singh. 
Java
// Java program to count number of subarrays // whose maximum element is greater than K. import java.util.*; public class GFG {  static long countSubarray(int arr[] int n int k)  {  long ans = 0 ;  int prev = - 1; //prev for keeping track of index of previous element > k;  for(int i = 0 ; i < n ; i++ ) {  if ( arr [ i ] > k ) {  ans += n - i ; //subarrays starting at index i.  ans += i - prev - 1 ; //subarrays ending at index i but starting after prev.  ans += ( n - i - 1 ) * 1L * ( i - prev - 1 ) ; //subarrays having index i element in between.  prev = i; // updating prev  }  }  return ans;  }  // Driver code  public static void main(String[] args)  {  int arr[] = { 4 5 1 2 3 };  int k = 2;  int n = arr.length;  System.out.print(countSubarray(arr n k));  } } //This Code is contributed by Manjeet Singh 
Python3
# Python program to count number of subarrays # whose maximum element is greater than K. def countSubarray( arr n k): ans = 0 ; prev = - 1; #prev for keeping track of index of previous element > k; for i in range(0n): if ( arr [ i ] > k ) : ans += n - i ; #subarrays starting at index i. ans += i - prev - 1 ; #subarrays ending at index i but starting after prev. ans += ( n - i - 1 ) * ( i - prev - 1 ) ; #subarrays having index i element in between. prev = i; # updating prev return ans; # Driven Program arr = [ 4 5 1 2 3 ]; k = 2; n = len(arr); print(countSubarray(arr n k)); # this code is contributed by poojaagarwal2. 
C#
// C# program to count number of subarrays // whose maximum element is greater than K. using System; public class GFG {  static long countSubarray(int[] arr int n int k)  {  long ans = 0;  int prev = -1; // prev for keeping track of index of  // previous element > k;  for (int i = 0; i < n; i++) {  if (arr[i] > k) {  ans += n - i; // subarrays starting at index  // i.  ans += i - prev  - 1; // subarrays ending at index i  // but starting after prev.  ans += (n - i - 1) * (long)1  * (i - prev  - 1); // subarrays having index i  // element in between.  prev = i; // updating prev  }  }  return ans;  }  // Driver code  public static void Main(string[] args)  {  int[] arr = { 4 5 1 2 3 };  int k = 2;  int n = arr.Length;  Console.Write(countSubarray(arr n k));  } } // This Code is contributed by Karandeep1234 
JavaScript
// Javascript program to count number of subarrays // whose maximum element is greater than K. function countSubarray(arr n k) {  let ans = 0 ;  //prev for keeping track of index of previous element > k;  let prev = - 1;   for(let i = 0 ; i < n ; i++ ) {  if ( arr [ i ] > k ) {  //subarrays starting at index i.  ans += n - i ;   //subarrays ending at index i but starting after prev.  ans += i - prev - 1 ;  //subarrays having index i element in between.  ans += ( n - i - 1 ) * 1 * ( i - prev - 1 ) ;   // updating prev  prev = i;   }  }  return ans; } // Driven Program  let arr = [ 4 5 1 2 3 ];  let k = 2;  let n = arr.length;  document.write(countSubarray(arr n k));   

الإخراج
12 

تعقيد الوقت: O(ن).

النهج 3 : تقنية النافذة المنزلقة.

الخوارزمية:

1. تهيئة متغير السنوات = 0 متغير العنصر الأقصى = 0 ومتغير العدد = 0 .

2. قم بالتكرار عبر المصفوفة وقم بما يلي لكل عنصر:

  أ. إذا كان العنصر الحالي أي. وصول [أنا] أكبر من الحد الأقصى الحالي للتحديث، أي الحد الأقصى. الراديو = آر ] وأعد ضبط العد إلى 0.

  ب. إذا كان العنصر الحالي أقل من أو يساوي الحد الأقصى الحالي، فقم بزيادة العدد.

  ج. لو maxElement هو أكبر من ك ثم إضافة العد من المصفوفات الفرعية للإجابة النهائية وتحديث maxElement إلى العنصر الحالي

3. عودة الجواب النهائي.

وهنا تنفيذ تقنية النافذة المنزلقة.

C++
#include    using namespace std; int countSubarray(int arr[] int n int k) {  int maxElement = 0 count = 0 ans = 0;  for(int i=0; i<n; i++) {  if(arr[i] > maxElement) {  maxElement = arr[i];  count = 0;  }  else {  count++;  }  if(maxElement > k) {  ans += (i - count + 1);  maxElement = arr[i];  count = 0;  }  }  return ans; } int main() {  int arr[] = {1 2 3 4};  int k = 1;  int n = sizeof(arr) / sizeof(arr[0]);  cout << countSubarray(arr n k);  return 0; } // This code is contributed by Vaibhav Saroj 
C
#include  int countSubarray(int arr[] int n int k) {  int maxElement = 0 count = 0 ans = 0;  for(int i=0; i<n; i++) {  if(arr[i] > maxElement) {  maxElement = arr[i];  count = 0;  }  else {  count++;  }  if(maxElement > k) {  ans += (i - count + 1);  maxElement = arr[i];  count = 0;  }  }  ans += (count * (count + 1)) / 2;  return ans; } int main() {  int arr[] = {1 2 3 4};  int k = 1;  int n = sizeof(arr) / sizeof(arr[0]);  printf('%dn' countSubarray(arr n k));  return 0; } // This code is contributed by Vaibhav Saroj 
Java
import java.util.*; public class GFG {  // Function to count the number of subarrays with the maximum element greater than k  public static int countSubarray(int[] arr int n int k) {  int maxElement = 0; // Variable to store the maximum element encountered so far  int count = 0; // Variable to count the length of the subarray with elements <= k  int ans = 0; // Variable to store the final result  for (int i = 0; i < n; i++) {  if (arr[i] > maxElement) {  // If the current element is greater than the maximum element  // update the maximum element and reset the count to zero.  maxElement = arr[i];  count = 0;  } else {  // increment the count  count++;  }  if (maxElement > k) {  // If the maximum element in the current subarray is greater than k  // add the count of subarrays ending at the current index (i - count + 1) to the result.  ans += (i - count + 1);  // Reset the maximum element and count to zero.  maxElement = arr[i];  count = 0;  }  }  // Return the final result  return ans;  }  public static void main(String[] args) {  int[] arr = {1 2 3 4};  int k = 1;  int n = arr.length;  // Call the countSubarray function to count the number of subarrays with maximum element greater than k  int result = countSubarray(arr n k);  System.out.println(result);  } } // THIS CODE IS CONTRIBUTED BY KIRTI AGARWAL 
Python3
def countSubarray(arr n k): maxElement count ans = 0 0 0 for i in range(n): if arr[i] > maxElement: maxElement = arr[i] count = 0 else: count += 1 if maxElement > k: ans += (i - count + 1) maxElement = arr[i] count = 0 ans += (count * (count + 1)) // 2 return ans arr = [1 2 3 4] k = 1 n = len(arr) print(countSubarray(arr n k)) # This code is contributed by Vaibhav Saroj 
C#
using System; public class Program {  public static int CountSubarray(int[] arr int n int k) {  int maxElement = 0 count = 0 ans = 0;  for(int i=0; i<n; i++) {  if(arr[i] > maxElement) {  maxElement = arr[i];  count = 0;  }  else {  count++;  }  if(maxElement > k) {  ans += (i - count + 1);  maxElement = arr[i];  count = 0;  }  }  ans += (count * (count + 1)) / 2;  return ans;  }  public static void Main() {  int[] arr = {1 2 3 4};  int k = 1;  int n = arr.Length;  Console.WriteLine(CountSubarray(arr n k));  } } // This code is contributed by Vaibhav Saroj 
JavaScript
function countSubarray(arr n k) {  let maxElement = 0 count = 0 ans = 0;  for(let i=0; i<n; i++) {  if(arr[i] > maxElement) {  maxElement = arr[i];  count = 0;  }  else {  count++;  }  if(maxElement > k) {  ans += (i - count + 1);  maxElement = arr[i];  count = 0;  }  }  ans += (count * (count + 1)) / 2;  return ans; } let arr = [1 2 3 4]; let k = 1; let n = arr.length; console.log(countSubarray(arr n k)); // This code is contributed by Vaibhav Saroj 

الإخراج
9 

يتم المساهمة في تقنية النافذة المنزلقة بواسطة فايبهاف ساروج .

تعقيد الوقت: O(n).
تعقيد الفضاء: O( 1 ).

ممارسة هنا عدد المصفوفات الفرعية .

إنشاء اختبار