#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; #define MAX 1000000 int main() { int N,T,i,j; cin>>N>>T; int A[MAX]; for(i=0;i<N;i++) cin>>A[i]; while(T--) { cin>>i>>j; int max=A[i]; for(int k=i;k<=j;k++) max=max<A[k]?max:A[k]; cout<<max<<"\n"; } return 0; }
C Implementation:
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int main(){ int n; int t; scanf("%d %d",&n,&t); int width[n]; for(int width_i = 0; width_i < n; width_i++){ scanf("%d",&width[width_i]); } for(int a0 = 0; a0 < t; a0++){ int i; int j; int max; scanf("%d %d",&i,&j); max = width[i]; for(int k = i;k<=j;k++) max = max<width[k] ? max : width[k]; printf("%d\n",max); } return 0; }
Java Implementation:
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int t = in.nextInt(); int width[] = new int[n]; for(int width_i=0; width_i < n; width_i++){ width[width_i] = in.nextInt(); } for(int a0 = 0; a0 < t; a0++){ int i = in.nextInt(); int j = in.nextInt(); int max = width[i]; for(int k = i;k<=j;k++) max = max<width[k] ? max : width[k]; System.out.println(max); } } }