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
Components in a graph
There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu
View Solution →Kundu and Tree
Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that
View Solution →Super Maximum Cost Queries
Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and
View Solution →Contacts
We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co
View Solution →No Prefix Set
There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio
View Solution →Cube Summation
You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor
View Solution →