Queries with Fixed Length
Problem Statement :
Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The second query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . Return . Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, n and q. The second line consists of n space-separated integers, the elements of arr. Each of the q subsequent lines contains a single integer denoting the value of d for that query.
Solution :
Solution in C :
In C ++ :
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define li long int
#define INT_MAX 1000000007
li tree[1000000];
li A[200000];
void build(int node, int start, int end)
{
if(start == end)
{
tree[node] = A[start];
}
else
{
int mid = (start + end) / 2;
build(2*node, start, mid);
build(2*node+1, mid+1, end);
tree[node] = max(tree[2*node] , tree[2*node+1]);
}
}
int query(int node, int start, int end, int l, int r)
{
if(r < start or end < l)
{
return 0;
}
if(l <= start and end <= r)
{
return tree[node];
}
int mid = (start + end) / 2;
int p1 = query(2*node, start, mid, l, r);
int p2 = query(2*node+1, mid+1, end, l, r);
return max(p1 , p2);
}
int main() {
li q,n,d;
cin >> n >> q;
for(int i=0;i<n;++i) cin >> A[i];
build(1,0,n-1);
while(q--)
{
cin >> d;
li min=INT_MAX;
for(int i=0;i<=n-d;++i)
{
li temp_max=query(1,0,n-1,i,i+d-1);
if(temp_max<min) min = temp_max;
}
cout<<min<<endl;
}
return 0;
}
In Java :
import java.util.Scanner;
public class Solution
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int dataCount = scan.nextInt();
int queriesCount = scan.nextInt();
int[] data = new int[dataCount];
for (int i = 0; i < dataCount; ++i)
data[i] = scan.nextInt();
for (int i = 0; i < queriesCount; ++i)
System.out.println(findMinMax(data, scan.nextInt()));
}
private static int findMinMax(int[] data, int range)
{
if (range >= data.length)
return easyMax(data, 0, data.length);
int maxV = easyMax(data, data.length - range, data.length);
int minV = maxV;
for (int i = data.length - range - 1; i >= 0; --i)
{
if (data[i] == data[i+range]) //nothing changed
continue;
if (data[i] > maxV)
maxV = data[i];
else if (data[i + range] == maxV)
maxV = easyMax(data, i, i + range);
if (maxV < minV)
minV = maxV;
}
return minV;
}
private static int easyMax(int[] data, int fromInclusive, int toExclusive)
{
int pos = toExclusive;
int max = data[--pos];
while (--pos >= fromInclusive)
if (data[pos] > max)
max = data[pos];
return max;
}
}
In C :
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
struct queue{
int front,rear,size;
unsigned capacity;
long *array;
int *index;
};
struct queue *create(unsigned capacity){
struct queue *q=(struct queue *)malloc(sizeof(struct queue));
q->front=0;
q->rear=capacity-1;
q->size=0;
q->capacity=capacity;
q->array=(long *)malloc(q->capacity*sizeof(long));
q->index=(int *)malloc(q->capacity*sizeof(int));
for(int i=0;i<q->capacity;i++){
q->array[i]=0;
q->index[i]=0;
}
return q;
}
int full(struct queue* q){
if(q->size==q->capacity) return 1;
else return 0;
}
int empty(struct queue* q){
if(q->size==0) return 1;
else return 0;
}
void enque(struct queue* q, long x, int i){
if(!full(q)){
q->size++;
q->rear=(q->rear+1)%(q->capacity);
q->array[q->rear]=x;
q->index[q->rear]=i;
}
}
void deque(struct queue *q){
if(!empty(q)){
q->size--;
q->front=(q->front+1)%(q->capacity);
}
}
long top_array(struct queue *q){
if(!empty(q)) return q->array[q->front];
return INT_MAX;
}
int top_index(struct queue *q){
if(!empty(q)) return q->index[q->front];
return INT_MAX;
}
void deque_back(struct queue *q){
if(!empty(q)){
q->size--;
q->rear=(q->rear-1);
if(q->rear<0) q->rear=q->capacity+q->rear;
}
}
long bottom_array(struct queue *q){
if(!empty(q)) return q->array[q->rear];
return INT_MAX;
}
int main(){
int n,Q,i,j,k;
scanf("%d%d",&n,&Q);
long *a=(long *)malloc(n*sizeof(long));
for(i=0;i<n;i++){
scanf("%li",&a[i]);
}
int d;
for(i=0;i<Q;i++){
long max,min=INT_MAX;
scanf("%d",&d);
struct queue *q=create(n);
j=0;
//printf("%d %d ",q->rear,q->size);
while(j<d){
if(empty(q)) enque(q,a[j],j);
else{
while(a[j]>=bottom_array(q)){ deque_back(q); }
enque(q,a[j],j);
}
//for(k=0;k<n;k++){ printf("%li ",q->array[k]); }
//printf("%d ",bottom_array(q));
j++;
}
min=top_array(q);
while(j<n){
while(j-top_index(q)<d && j<n){
if(top_array(q)<min) min=top_array(q);
while(a[j]>=bottom_array(q)){
deque_back(q);
}
enque(q,a[j],j);
j++;
}
if(j-top_index(q)==d && j<n){
if(top_array(q)<min) min=top_array(q);
deque(q);
while(a[j]>=bottom_array(q)){
deque_back(q);
}
enque(q,a[j],j);
j++;
}
}
if(top_array(q)<min) min=top_array(q);
printf("%li\n",min);
free(q);
}
return 0;
}
In Python3 :
from collections import deque
n, q = map(int, input().split())
a = list(map(int, input().split()))
for _ in range(q):
d = int(input())
l = deque()
m = 0
for i in range(d - 1, -1, -1):
if m < a[i]:
m = a[i]
l.appendleft(i)
for i in range(d, n):
if l[0] + d <= i:
l.popleft()
while l and a[l[-1]] < a[i]:
l.pop()
l.append(i)
m = min(m, a[l[0]])
print(m)
View More Similar Problems
Kindergarten Adventures
Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti
View Solution →Mr. X and His Shots
A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M
View Solution →Jim and the Skyscrapers
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space
View Solution →Palindromic Subsets
Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t
View Solution →Counting On a Tree
Taylor loves trees, and this new challenge has him stumped! Consider a tree, t, consisting of n nodes. Each node is numbered from 1 to n, and each node i has an integer, ci, attached to it. A query on tree t takes the form w x y z. To process a query, you must print the count of ordered pairs of integers ( i , j ) such that the following four conditions are all satisfied: the path from n
View Solution →Polynomial Division
Consider a sequence, c0, c1, . . . , cn-1 , and a polynomial of degree 1 defined as Q(x ) = a * x + b. You must perform q queries on the sequence, where each query is one of the following two types: 1 i x: Replace ci with x. 2 l r: Consider the polynomial and determine whether is divisible by over the field , where . In other words, check if there exists a polynomial with integer coefficie
View Solution →