Queries with Fixed Length

Sort by

recency

|

32 Discussions

|

  • + 0 comments

    Here is Hackerrank Queries with fixed length problem solution in Python Java c++ c and javascript

  • + 0 comments
    public static List<Integer> solve(List<Integer> arr, List<Integer> queries) {
            // Write your code here
            int i, j, n = arr.size();
            List <Integer> result = new ArrayList<>();
            for (Integer query : queries) {
                List <Integer> max = new ArrayList<>();
                for (i = 0; i <= n - query; i++) {
                    max.add(Collections.max(arr.subList(i, i+query)));
                }
                result.add(Collections.min(max));
            }
            return result;
        }
    
  • + 0 comments
    from collections import deque
    
    def min_of_max_in_subarrays_with_size(d, arr):
        # use sliding window to iterate through array
        dq = deque()
        candidtaes = []
        for i in range(len(arr)):
            # remove out-of-window index
            if dq and dq[0] <= i-d:
                dq.popleft()
            # before adding the current index, make sure to remove all indexes
            # indicating to value not larger than that of the current i
            # this way can make sure the maximum is in the front of queue
            while dq and arr[dq[-1]] <= arr[i]:
                dq.pop()
            dq.append(i)
            # add to max subarrays
            if i >= d-1:
                candidtaes.append(arr[dq[0]])
        return min(candidtaes)
    
    def solve(arr, queries):
        result = []  # the min of max subarrays with size d
        for d in queries:
            result.append(min_of_max_in_subarrays_with_size(d, arr))
        return result
    
  • + 0 comments

    C++20

    vector<int> solve(vector<int> arr, vector<int> queries) {
        vector<int> results;
        int N = arr.size();
        for(auto q : queries){
            auto it = max_element(arr.begin()+ 0, arr.begin() + 0 + q );
            int max_e = *it;
            int min_qu = *it;
            cout << max_e << ", ";
            for(int i=1; i<N-q+1; i++){
                if(arr[i+q-1] > max_e) max_e = arr[i+q-1];
                if(arr[i-1] == max_e){
                    auto it = max_element(arr.begin()+ i, arr.begin() + i + q );
                    max_e = *it;
                }
                            
                cout << max_e << ", ";
                min_qu = min(min_qu, max_e);
            }
            
            results.push_back(min_qu);
        }
        return results;
    }
    
  • + 0 comments
    def solve(arr: Array[Int], queries: Array[Int]): Array[Int] = {
        
        val dq = scala.collection.mutable.ArrayDeque.empty[Int]
        
        queries.map{ q => {
          var minOfMax = Int.MaxValue 
          dq.clear()
          
          arr.indices.foreach{ i => {
            
            while (dq.nonEmpty && i >= q && dq.head <= i-q ) 
              dq.removeHead()
            
            while (dq.nonEmpty && arr(dq.last) <= arr(i)) 
              dq.removeLast()
            
            dq.append(i)
    
            if (i >= q-1) 
              minOfMax = arr(dq.head).min(minOfMax)
            
          }}
          
          minOfMax
        }}