📌  相关文章
📜  Array 中索引范围 [L, R] 的计数,以便删除其所有实例对 Array 进行排序

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

Array 中索引范围 [L, R] 的计数,以便删除其所有实例对 Array 进行排序

给定一个长度为N的数组arr[] ,任务是在数组 arr[] 中找到Good Ranges的数量

一个好的范围被定义为左右索引的范围,即[L. R]在数组arr[]中,这样通过删除数组 arr[] 的范围[L, R]中的所有数字以及范围之外的那些元素的外观,数组arr []变为非递减排序命令。

例子:

方法:方法是找到每个子数组[l, r]并检查剩余数组是否已排序。如果数组是排序的,那么,范围的左边部分在l和右边部分从r到末尾,每个子数组都是答案。下面是上述方法的实现:

  • 将变量count初始化为0以存储此类子数组的数量。
  • 定义一个函数chk_sorted(l, r, a)来检查剩余的数组a[]是否已排序:
    • 初始化列表l以将子数组从lr的所有元素存储在数组a[] 中。
    • 初始化数组chk[]以存储数组[]中不存在于[l, r] 范围内的元素。
    • 使用变量i迭代范围[0, N]并执行以下步骤:
      • 如果列表l中不存在a[i] ,则将元素存储在数组chk[] 中。
    • 初始化列表chk1以存储数组chk[]。
    • 对列表chk1进行排序并检查chk1是否等于chk,然后返回true ,否则返回false。
  • 使用变量i迭代范围[0, N]并执行以下步骤:
    • 使用变量i迭代范围[i+1, N]并执行以下步骤:
      • 调用函数chk_sorted(i, j, a) ,如果函数返回true,则将count的值增加len(a)-j中断循环
  • 返回count的值作为答案。

下面是上述方法的实现。

C++
// C++ program to implement the above approach
#include 
using namespace std;
 
// Function to chk array is sorted or not
bool chk_sorted(int l, int r, vector a)
{
     
    // Taking range element separately
    // to be removed
    vector temp;
    unordered_set s;
    for(int i = l; i <= r; i++)
    {
        temp.push_back(a[i]);
        s.insert(a[i]);
    }
 
    vector chk;
    for(int i = 0; i < a.size(); i++)
    {
         
        // Checking is all range element
        // occurrence is removed
        if (s.find(a[i]) == s.end())
        {
            chk.push_back(a[i]);
        }
    }
 
    vector chk1 = chk;
 
    sort(chk1.begin(), chk1.end());
 
    // If array is sorted return true
    for(int i = 0; i < chk.size(); i++)
    {
        if (chk[i] != chk1[i])
        {
            return false;
        }
    }
    return true;
}
 
// Function to count all good ranges
int countp(vector a)
{
     
    // Initial count 0
    int count = 0;
 
    // Nested loop implementation
    for(int i = 0; i < a.size(); i++)
    {
        for(int j = i + 1; j < a.size(); j++)
        {
             
            // Checking if range is good
            if (chk_sorted(i, j, a))
            {
                 
                // According to observation as given
                // in approach
                count += a.size() - j;
                break;
            }
        }
    }
    return count;
}
 
// Driver code
int main()
{
    int N = 5;
    vector A = { 5, 3, 1, 5, 2 };
     
    cout << (countp(A));
}
 
// This code is contributed by Potta Lokesh


Java
// Java program to implement the above approach
import java.util.*;
 
class GFG{
 
// Function to chk array is sorted or not
static boolean chk_sorted(int l, int r, int []a)
{
     
    // Taking range element separately
    // to be removed
    Vector temp = new Vector();
    HashSet s = new HashSet();
    for(int i = l; i <= r; i++)
    {
        temp.add(a[i]);
        s.add(a[i]);
    }
 
    Vector chk=new Vector();
    for(int i = 0; i < a.length; i++)
    {
         
        // Checking is all range element
        // occurrence is removed
        if (!s.contains(a[i]))
        {
            chk.add(a[i]);
        }
    }
 
    Vector chk1 = new Vector(chk);
 
    Collections.sort(chk1);
 
    // If array is sorted return true
    for(int i = 0; i < chk.size(); i++)
    {
        if (chk.get(i) != chk1.get(i))
        {
            return false;
        }
    }
    return true;
}
 
// Function to count all good ranges
static int countp(int []a)
{
     
    // Initial count 0
    int count = 0;
 
    // Nested loop implementation
    for(int i = 0; i < a.length; i++)
    {
        for(int j = i + 1; j < a.length; j++)
        {
             
            // Checking if range is good
            if (chk_sorted(i, j, a))
            {
                 
                // According to observation as given
                // in approach
                count += a.length - j;
                break;
            }
        }
    }
    return count;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 5;
    int [] A = { 5, 3, 1, 5, 2 };
     
    System.out.print(countp(A));
}
}
// This code is contributed by shikhasingrajput


Python3
# Python program to implement the above approach
 
# function to chk array is sorted or not
def chk_sorted(l, r, a):
 
    # taking range element separately
    # to be removed
    l = list(a[l:r + 1])
 
    chk = []
    for i in a:
        # checking is all range element
        # occurrence is removed
        if(i not in l):
            chk.append(i)
    chk1 = list(chk)
    chk1.sort()
    # if array is sorted return true
    if(chk1 == chk):
        return True
    else:
        return False
 
 
# function to count all good ranges
def countp(a):
 
    # initial count 0
    count = 0
 
    # nested loop implementation
    for i in range(len(a)):
        for j in range(i + 1, len(a)):
 
            # checking if range is good
            if(chk_sorted(i, j, a)):
 
                # according to observation as given
                # in approach
                count += len(a)-j
                break
    return count
 
 
# Driver code
N = 5
A = [5, 3, 1, 5, 2]
print(countp(A))


C#
// C# program to implement the above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Function to chk array is sorted or not
    static bool chk_sorted(int l, int r, int[] a)
    {
 
        // Taking range element separately
        // to be removed
        List temp = new List();
        HashSet s = new HashSet();
        for (int i = l; i <= r; i++) {
            temp.Add(a[i]);
            s.Add(a[i]);
        }
 
        List chk = new List();
        for (int i = 0; i < a.Length; i++) {
 
            // Checking is all range element
            // occurrence is removed
            if (!s.Contains(a[i])) {
                chk.Add(a[i]);
            }
        }
 
        List chk1 = new List(chk);
 
        chk1.Sort();
 
        // If array is sorted return true
        for (int i = 0; i < chk.Count; i++) {
            if (chk[i] != chk1[i]) {
                return false;
            }
        }
        return true;
    }
 
    // Function to count all good ranges
    static int countp(int[] a)
    {
 
        // Initial count 0
        int count = 0;
 
        // Nested loop implementation
        for (int i = 0; i < a.Length; i++) {
            for (int j = i + 1; j < a.Length; j++) {
 
                // Checking if range is good
                if (chk_sorted(i, j, a)) {
 
                    // According to observation as given
                    // in approach
                    count += a.Length - j;
                    break;
                }
            }
        }
        return count;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        int[] A = { 5, 3, 1, 5, 2 };
 
        Console.WriteLine(countp(A));
    }
}
 
// This code is contributed by ukasp.


Javascript


输出
7

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