查询以查找给定范围内相邻数组元素之间的最小绝对差
给定一个由N个整数组成的数组arr[]和一个由{L, R}形式的查询组成的数组query[] ,每个查询的任务是在[L]范围内找到相邻元素之间绝对差的最小值, R] 。
例子:
Input: arr[] = {2, 6, 1, 8, 3, 4}, query[] = {{0, 3}, {1, 5}, {4, 5}}
Output:
4
1
1
Explanation:
Following are the values of queries performed:
- The minimum absolute difference between adjacent element over the range [0, 3] is min(|2 – 6|, |6 – 1|, |1 – 8|) = 4.
- The minimum absolute difference between adjacent element over the range [1, 5] is min(|6 – 1|, |1 – 8|, |8 – 3|, |3 – 4| ) = 1.
- The minimum absolute difference between adjacent element over the range [4, 5] is min(|3 – 4|) = 1.
Therefore, print 4, 1, 1 as the results of the given queries.
Input: arr[] = [10, 20, 1, 1, 5 ], query[] = [0, 1], [1, 4], [2, 3]
Output:
10
0
0
朴素方法:解决给定问题的最简单方法是创建一个数组diff[] ,该数组存储每个数组元素的相邻元素之间的绝对差。现在对于每个查询,遍历范围[L, R – 1]上的数组diff[ ] 并打印范围[ L, R – 1] 中所有值的最小值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Structure for query range
struct Query {
int L, R;
};
int MAX = 5000;
// Function to find the minimum difference
// between adjacent array element over the
// given range [L, R] for Q Queries
void minDifference(int arr[], int n,
Query q[], int m)
{
// Find the sum of all queries
for (int i = 0; i < m; i++) {
// Left and right boundaries
// of current range
int L = q[i].L, R = q[i].R;
int ans = MAX;
for (int i = L; i < R; i++) {
ans = min(ans, arr[i]);
}
// Print the sum of the
// current query range
cout << ans << '\n';
}
}
// Function to find the minimum absolute
// difference of adjacent array elements
// for the given range
void minimumDifference(int arr[], Query q[],
int N, int m)
{
// Stores the absolute difference of
// adjacent elements
int diff[N];
for (int i = 0; i < N - 1; i++)
diff[i] = abs(arr[i] - arr[i + 1]);
// Find the minimum difference of
// adjacent elements
minDifference(diff, N - 1, q, m);
}
// Driver Code
int main()
{
int arr[] = { 2, 6, 1, 8, 3, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
Query Q[] = { { 0, 3 }, { 1, 5 }, { 4, 5 } };
int M = sizeof(Q) / sizeof(Q[0]);
minimumDifference(arr, Q, N, M);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Structure for query range
static class Query {
int L, R;
public Query(int l, int r) {
super();
L = l;
R = r;
}
};
static int MAX = 5000;
// Function to find the minimum difference
// between adjacent array element over the
// given range [L, R] for Q Queries
static void minDifference(int arr[], int n,
Query q[], int m)
{
// Find the sum of all queries
for (int i = 0; i < m; i++) {
// Left and right boundaries
// of current range
int L = q[i].L, R = q[i].R;
int ans = MAX;
for (int j = L; j < R; j++) {
ans = Math.min(ans, arr[j]);
}
// Print the sum of the
// current query range
System.out.println(ans);
}
}
// Function to find the minimum absolute
// difference of adjacent array elements
// for the given range
static void minimumDifference(int arr[], Query q[],
int N, int m)
{
// Stores the absolute difference of
// adjacent elements
int []diff = new int[N];
for (int i = 0; i < N - 1; i++)
diff[i] = Math.abs(arr[i] - arr[i + 1]);
// Find the minimum difference of
// adjacent elements
minDifference(diff, N - 1, q, m);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, 6, 1, 8, 3, 4 };
int N = arr.length;
Query Q[] = {new Query( 0, 3 ),new Query( 1, 5 ),new Query( 4, 5 ) };
int M = Q.length;
minimumDifference(arr, Q, N, M);
}
}
// This code is contributed by Princi Singh
Python3
# Python3 program for the above approach
MAX = 5000;
# Function to find the minimum difference
# between adjacent array element over the
# given range [L, R] for Q Queries
def minDifference(arr, n, q, m) :
# Find the sum of all queries
for i in range(m) :
# Left and right boundaries
# of current range
L = q[i][0]; R = q[i][1];
ans = MAX;
for i in range(L, R) :
ans = min(ans, arr[i]);
# Print the sum of the
# current query range
print(ans);
# Function to find the minimum absolute
# difference of adjacent array elements
# for the given range
def minimumDifference(arr, q, N, m) :
# Stores the absolute difference of
# adjacent elements
diff = [0]*N;
for i in range(N - 1) :
diff[i] = abs(arr[i] - arr[i + 1]);
# Find the minimum difference of
# adjacent elements
minDifference(diff, N - 1, q, m);
# Driver Code
if __name__ == "__main__" :
arr = [ 2, 6, 1, 8, 3, 4 ];
N = len(arr);
Q = [ [ 0, 3 ], [ 1, 5 ], [ 4, 5 ] ];
M = len(Q);
minimumDifference(arr, Q, N, M);
# This code is contributed by AnkThon
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Structure for query range
class Query {
public int L, R;
public Query(int l, int r) {
this.L = l;
this.R = r;
}
};
static int MAX = 5000;
// Function to find the minimum difference
// between adjacent array element over the
// given range [L, R] for Q Queries
static void minDifference(int []arr, int n,
Query []q, int m)
{
// Find the sum of all queries
for (int i = 0; i < m; i++) {
// Left and right boundaries
// of current range
int L = q[i].L, R = q[i].R;
int ans = MAX;
for (int j = L; j < R; j++) {
ans = Math.Min(ans, arr[j]);
}
// Print the sum of the
// current query range
Console.WriteLine(ans);
}
}
// Function to find the minimum absolute
// difference of adjacent array elements
// for the given range
static void minimumDifference(int []arr, Query []q,
int N, int m)
{
// Stores the absolute difference of
// adjacent elements
int []diff = new int[N];
for (int i = 0; i < N - 1; i++)
diff[i] = Math.Abs(arr[i] - arr[i + 1]);
// Find the minimum difference of
// adjacent elements
minDifference(diff, N - 1, q, m);
}
// Driver Code
public static void Main()
{
int []arr = { 2, 6, 1, 8, 3, 4 };
int N = arr.Length;
Query []Q = {new Query( 0, 3 ),new Query( 1, 5 ),new Query( 4, 5 ) };
int M = Q.Length;
minimumDifference(arr, Q, N, M);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
#define MAX 500
// Stores the index for the minimum
// value in the subarray arr[i, j]
int lookup[MAX][MAX];
// Structure for query range
struct Query {
int L, R;
};
// Function to fill the lookup array
// lookup[][] in the bottom up manner
void preprocess(int arr[], int n)
{
// Initialize M for the intervals
// with length 1
for (int i = 0; i < n; i++)
lookup[i][0] = i;
// Find the values from smaller
// to bigger intervals
for (int j = 1; (1 << j) <= n; j++) {
// Compute minimum value for
// all intervals with size 2^j
for (int i = 0; (i + (1 << j) - 1) < n; i++) {
// For arr[2][10], compare
// arr[lookup[0][3]] and
// arr[lookup[3][3]]
if (arr[lookup[i][j - 1]]
< arr[lookup[i + (1 << (j - 1))][j - 1]])
lookup[i][j] = lookup[i][j - 1];
// Otherwise
else
lookup[i][j]
= lookup[i + (1 << (j - 1))][j - 1];
}
}
}
// Function find minimum of absolute
// difference of all adjacent element
// in subarray arr[L..R]
int query(int arr[], int L, int R)
{
// For [2, 10], j = 3
int j = (int)log2(R - L + 1);
// For [2, 10], compare arr[lookup[0][3]]
// and arr[lookup[3][3]],
if (arr[lookup[L][j]]
<= arr[lookup[R - (1 << j) + 1][j]])
return arr[lookup[L][j]];
else
return arr[lookup[R - (1 << j) + 1][j]];
}
// Function to find the minimum of the
// ranges for M queries
void Min_difference(int arr[], int n,
Query q[], int m)
{
// Fills table lookup[n][Log n]
preprocess(arr, n);
// Compute sum of all queries
for (int i = 0; i < m; i++) {
// Left and right boundaries
// of current range
int L = q[i].L, R = q[i].R;
// Print sum of current query range
cout << query(arr, L, R - 1) << '\n';
}
}
// Function to find the minimum absolute
// difference in a range
void minimumDifference(int arr[], Query q[],
int N, int m)
{
// diff[] is to stores the absolute
// difference of adjacent elements
int diff[N];
for (int i = 0; i < N - 1; i++)
diff[i] = abs(arr[i] - arr[i + 1]);
// Call Min_difference to get minimum
// difference of adjacent elements
Min_difference(diff, N - 1, q, m);
}
// Driver Code
int main()
{
int arr[] = { 2, 6, 1, 8, 3, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
Query Q[] = { { 0, 3 }, { 1, 5 }, { 4, 5 } };
int M = sizeof(Q) / sizeof(Q[0]);
minimumDifference(arr, Q, N, M);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
import javax.management.Query;
class GFG{
static final int MAX = 500;
// Stores the index for the minimum
// value in the subarray arr[i, j]
static int [][]lookup = new int[MAX][MAX];
// Structure for query range
static class Query {
int L, R;
public Query(int l, int r) {
super();
L = l;
R = r;
}
};
// Function to fill the lookup array
// lookup[][] in the bottom up manner
static void preprocess(int arr[], int n)
{
// Initialize M for the intervals
// with length 1
for (int i = 0; i < n; i++)
lookup[i][0] = i;
// Find the values from smaller
// to bigger intervals
for (int j = 1; (1 << j) <= n; j++) {
// Compute minimum value for
// all intervals with size 2^j
for (int i = 0; (i + (1 << j) - 1) < n; i++) {
// For arr[2][10], compare
// arr[lookup[0][3]] and
// arr[lookup[3][3]]
if (arr[lookup[i][j - 1]]
< arr[lookup[i + (1 << (j - 1))][j - 1]])
lookup[i][j] = lookup[i][j - 1];
// Otherwise
else
lookup[i][j]
= lookup[i + (1 << (j - 1))][j - 1];
}
}
}
// Function find minimum of absolute
// difference of all adjacent element
// in subarray arr[L..R]
static int query(int arr[], int L, int R)
{
// For [2, 10], j = 3
int j = (int)Math.log(R - L + 1);
// For [2, 10], compare arr[lookup[0][3]]
// and arr[lookup[3][3]],
if (arr[lookup[L][j]]
<= arr[lookup[R - (1 << j) + 1][j]])
return arr[lookup[L][j]];
else
return arr[lookup[R - (1 << j) + 1][j]];
}
// Function to find the minimum of the
// ranges for M queries
static void Min_difference(int arr[], int n,
Query q[], int m)
{
// Fills table lookup[n][Log n]
preprocess(arr, n);
// Compute sum of all queries
for (int i = 0; i < m; i++) {
// Left and right boundaries
// of current range
int L = q[i].L, R = q[i].R;
// Print sum of current query range
System.out.println(query(arr, L, R - 1));
}
}
// Function to find the minimum absolute
// difference in a range
static void minimumDifference(int arr[], Query q[],
int N, int m)
{
// diff[] is to stores the absolute
// difference of adjacent elements
int []diff = new int[N];
for (int i = 0; i < N - 1; i++)
diff[i] = Math.abs(arr[i] - arr[i + 1]);
// Call Min_difference to get minimum
// difference of adjacent elements
Min_difference(diff, N - 1, q, m);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, 6, 1, 8, 3, 4 };
int N = arr.length;
Query Q[] = { new Query( 0, 3 ), new Query( 1, 5 ), new Query( 4, 5 ) };
int M = Q.length;
minimumDifference(arr, Q, N, M);
}
}
// This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
public class GFG{
static readonly int MAX = 500;
// Stores the index for the minimum
// value in the subarray arr[i, j]
static int [,]lookup = new int[MAX, MAX];
// Structure for query range
class Query {
public int L, R;
public Query(int l, int r) {
L = l;
R = r;
}
};
// Function to fill the lookup array
// lookup[,] in the bottom up manner
static void preprocess(int []arr, int n)
{
// Initialize M for the intervals
// with length 1
for (int i = 0; i < n; i++)
lookup[i,0] = i;
// Find the values from smaller
// to bigger intervals
for (int j = 1; (1 << j) <= n; j++) {
// Compute minimum value for
// all intervals with size 2^j
for (int i = 0; (i + (1 << j) - 1) < n; i++) {
// For arr[2,10], compare
// arr[lookup[0,3]] and
// arr[lookup[3,3]]
if (arr[lookup[i,j - 1]]
< arr[lookup[i + (1 << (j - 1)),j - 1]])
lookup[i,j] = lookup[i,j - 1];
// Otherwise
else
lookup[i,j]
= lookup[i + (1 << (j - 1)),j - 1];
}
}
}
// Function find minimum of absolute
// difference of all adjacent element
// in subarray arr[L..R]
static int query(int []arr, int L, int R)
{
// For [2, 10], j = 3
int j = (int)Math.Log(R - L + 1);
// For [2, 10], compare arr[lookup[0,3]]
// and arr[lookup[3,3]],
if (arr[lookup[L,j]]
<= arr[lookup[R - (1 << j) + 1,j]])
return arr[lookup[L,j]];
else
return arr[lookup[R - (1 << j) + 1,j]];
}
// Function to find the minimum of the
// ranges for M queries
static void Min_difference(int []arr, int n,
Query []q, int m)
{
// Fills table lookup[n,Log n]
preprocess(arr, n);
// Compute sum of all queries
for (int i = 0; i < m; i++) {
// Left and right boundaries
// of current range
int L = q[i].L, R = q[i].R;
// Print sum of current query range
Console.WriteLine(query(arr, L, R - 1));
}
}
// Function to find the minimum absolute
// difference in a range
static void minimumDifference(int []arr, Query []q,
int N, int m)
{
// diff[] is to stores the absolute
// difference of adjacent elements
int []diff = new int[N];
for (int i = 0; i < N - 1; i++)
diff[i] = Math.Abs(arr[i] - arr[i + 1]);
// Call Min_difference to get minimum
// difference of adjacent elements
Min_difference(diff, N - 1, q, m);
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 2, 6, 1, 8, 3, 4 };
int N = arr.Length;
Query []Q = { new Query( 0, 3 ), new Query( 1, 5 ), new Query( 4, 5 ) };
int M = Q.Length;
minimumDifference(arr, Q, N, M);
}
}
// This code is contributed by shikhasingrajput
Javascript
4
1
1
时间复杂度: O(N 2 )
辅助空间: O(N)
高效的方法:上述方法也可以通过使用支持在恒定时间O(1)和额外空间O(N log N)中查询的稀疏表进行优化。而不是通过原始arr[]通过diff[]来获得所需的答案。请按照以下步骤解决问题:
- 为稀疏数组初始化一个全局数组lookup[][] 。
- 定义一个函数preprocess(arr, N)并执行以下操作:
- 使用变量i遍历范围[0, N)并将lookup[i][0]的值设置为i 。
- 如果arr[lookup[i][j-1]]小于arr[lookup[i + (1 << (j-1))]使用变量j和i迭代范围[1, N) [j-1] ,然后将lookup[i][j]设置为lookup[i][j-1] ,否则将lookup[i][j]设置为lookup[i + (1 << (j – 1)) ][j - 1] 。
- 定义一个函数query(int arr[], int L, int M)并执行以下操作:
- 将变量j初始化为(int)log2(R – L + 1) 。
- 如果arr[lookup[L][j]]小于等于arr[lookup[R – (1 << j) + 1][j]],则返回arr[lookup[L][j]],否则返回arr[lookup[R – (1 << j) + 1][j]] 。
- 定义一个函数Min_difference(arr, n, q, m)并执行以下操作:
- 调用函数preprocess(arr, n)对稀疏数组进行预处理。
- 遍历给定的查询数组Q[] ,函数query(arr, L, R – 1)返回的值给出当前查询的结果。
- 初始化一个大小为N的数组diff[]并为i的每个值存储arr[i]-arr[i+1]的绝对差。
- 调用函数Min_difference(diff, N-1, q, m)来找到每个查询的最小绝对差。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
#define MAX 500
// Stores the index for the minimum
// value in the subarray arr[i, j]
int lookup[MAX][MAX];
// Structure for query range
struct Query {
int L, R;
};
// Function to fill the lookup array
// lookup[][] in the bottom up manner
void preprocess(int arr[], int n)
{
// Initialize M for the intervals
// with length 1
for (int i = 0; i < n; i++)
lookup[i][0] = i;
// Find the values from smaller
// to bigger intervals
for (int j = 1; (1 << j) <= n; j++) {
// Compute minimum value for
// all intervals with size 2^j
for (int i = 0; (i + (1 << j) - 1) < n; i++) {
// For arr[2][10], compare
// arr[lookup[0][3]] and
// arr[lookup[3][3]]
if (arr[lookup[i][j - 1]]
< arr[lookup[i + (1 << (j - 1))][j - 1]])
lookup[i][j] = lookup[i][j - 1];
// Otherwise
else
lookup[i][j]
= lookup[i + (1 << (j - 1))][j - 1];
}
}
}
// Function find minimum of absolute
// difference of all adjacent element
// in subarray arr[L..R]
int query(int arr[], int L, int R)
{
// For [2, 10], j = 3
int j = (int)log2(R - L + 1);
// For [2, 10], compare arr[lookup[0][3]]
// and arr[lookup[3][3]],
if (arr[lookup[L][j]]
<= arr[lookup[R - (1 << j) + 1][j]])
return arr[lookup[L][j]];
else
return arr[lookup[R - (1 << j) + 1][j]];
}
// Function to find the minimum of the
// ranges for M queries
void Min_difference(int arr[], int n,
Query q[], int m)
{
// Fills table lookup[n][Log n]
preprocess(arr, n);
// Compute sum of all queries
for (int i = 0; i < m; i++) {
// Left and right boundaries
// of current range
int L = q[i].L, R = q[i].R;
// Print sum of current query range
cout << query(arr, L, R - 1) << '\n';
}
}
// Function to find the minimum absolute
// difference in a range
void minimumDifference(int arr[], Query q[],
int N, int m)
{
// diff[] is to stores the absolute
// difference of adjacent elements
int diff[N];
for (int i = 0; i < N - 1; i++)
diff[i] = abs(arr[i] - arr[i + 1]);
// Call Min_difference to get minimum
// difference of adjacent elements
Min_difference(diff, N - 1, q, m);
}
// Driver Code
int main()
{
int arr[] = { 2, 6, 1, 8, 3, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
Query Q[] = { { 0, 3 }, { 1, 5 }, { 4, 5 } };
int M = sizeof(Q) / sizeof(Q[0]);
minimumDifference(arr, Q, N, M);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
import javax.management.Query;
class GFG{
static final int MAX = 500;
// Stores the index for the minimum
// value in the subarray arr[i, j]
static int [][]lookup = new int[MAX][MAX];
// Structure for query range
static class Query {
int L, R;
public Query(int l, int r) {
super();
L = l;
R = r;
}
};
// Function to fill the lookup array
// lookup[][] in the bottom up manner
static void preprocess(int arr[], int n)
{
// Initialize M for the intervals
// with length 1
for (int i = 0; i < n; i++)
lookup[i][0] = i;
// Find the values from smaller
// to bigger intervals
for (int j = 1; (1 << j) <= n; j++) {
// Compute minimum value for
// all intervals with size 2^j
for (int i = 0; (i + (1 << j) - 1) < n; i++) {
// For arr[2][10], compare
// arr[lookup[0][3]] and
// arr[lookup[3][3]]
if (arr[lookup[i][j - 1]]
< arr[lookup[i + (1 << (j - 1))][j - 1]])
lookup[i][j] = lookup[i][j - 1];
// Otherwise
else
lookup[i][j]
= lookup[i + (1 << (j - 1))][j - 1];
}
}
}
// Function find minimum of absolute
// difference of all adjacent element
// in subarray arr[L..R]
static int query(int arr[], int L, int R)
{
// For [2, 10], j = 3
int j = (int)Math.log(R - L + 1);
// For [2, 10], compare arr[lookup[0][3]]
// and arr[lookup[3][3]],
if (arr[lookup[L][j]]
<= arr[lookup[R - (1 << j) + 1][j]])
return arr[lookup[L][j]];
else
return arr[lookup[R - (1 << j) + 1][j]];
}
// Function to find the minimum of the
// ranges for M queries
static void Min_difference(int arr[], int n,
Query q[], int m)
{
// Fills table lookup[n][Log n]
preprocess(arr, n);
// Compute sum of all queries
for (int i = 0; i < m; i++) {
// Left and right boundaries
// of current range
int L = q[i].L, R = q[i].R;
// Print sum of current query range
System.out.println(query(arr, L, R - 1));
}
}
// Function to find the minimum absolute
// difference in a range
static void minimumDifference(int arr[], Query q[],
int N, int m)
{
// diff[] is to stores the absolute
// difference of adjacent elements
int []diff = new int[N];
for (int i = 0; i < N - 1; i++)
diff[i] = Math.abs(arr[i] - arr[i + 1]);
// Call Min_difference to get minimum
// difference of adjacent elements
Min_difference(diff, N - 1, q, m);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, 6, 1, 8, 3, 4 };
int N = arr.length;
Query Q[] = { new Query( 0, 3 ), new Query( 1, 5 ), new Query( 4, 5 ) };
int M = Q.length;
minimumDifference(arr, Q, N, M);
}
}
// This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
public class GFG{
static readonly int MAX = 500;
// Stores the index for the minimum
// value in the subarray arr[i, j]
static int [,]lookup = new int[MAX, MAX];
// Structure for query range
class Query {
public int L, R;
public Query(int l, int r) {
L = l;
R = r;
}
};
// Function to fill the lookup array
// lookup[,] in the bottom up manner
static void preprocess(int []arr, int n)
{
// Initialize M for the intervals
// with length 1
for (int i = 0; i < n; i++)
lookup[i,0] = i;
// Find the values from smaller
// to bigger intervals
for (int j = 1; (1 << j) <= n; j++) {
// Compute minimum value for
// all intervals with size 2^j
for (int i = 0; (i + (1 << j) - 1) < n; i++) {
// For arr[2,10], compare
// arr[lookup[0,3]] and
// arr[lookup[3,3]]
if (arr[lookup[i,j - 1]]
< arr[lookup[i + (1 << (j - 1)),j - 1]])
lookup[i,j] = lookup[i,j - 1];
// Otherwise
else
lookup[i,j]
= lookup[i + (1 << (j - 1)),j - 1];
}
}
}
// Function find minimum of absolute
// difference of all adjacent element
// in subarray arr[L..R]
static int query(int []arr, int L, int R)
{
// For [2, 10], j = 3
int j = (int)Math.Log(R - L + 1);
// For [2, 10], compare arr[lookup[0,3]]
// and arr[lookup[3,3]],
if (arr[lookup[L,j]]
<= arr[lookup[R - (1 << j) + 1,j]])
return arr[lookup[L,j]];
else
return arr[lookup[R - (1 << j) + 1,j]];
}
// Function to find the minimum of the
// ranges for M queries
static void Min_difference(int []arr, int n,
Query []q, int m)
{
// Fills table lookup[n,Log n]
preprocess(arr, n);
// Compute sum of all queries
for (int i = 0; i < m; i++) {
// Left and right boundaries
// of current range
int L = q[i].L, R = q[i].R;
// Print sum of current query range
Console.WriteLine(query(arr, L, R - 1));
}
}
// Function to find the minimum absolute
// difference in a range
static void minimumDifference(int []arr, Query []q,
int N, int m)
{
// diff[] is to stores the absolute
// difference of adjacent elements
int []diff = new int[N];
for (int i = 0; i < N - 1; i++)
diff[i] = Math.Abs(arr[i] - arr[i + 1]);
// Call Min_difference to get minimum
// difference of adjacent elements
Min_difference(diff, N - 1, q, m);
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 2, 6, 1, 8, 3, 4 };
int N = arr.Length;
Query []Q = { new Query( 0, 3 ), new Query( 1, 5 ), new Query( 4, 5 ) };
int M = Q.Length;
minimumDifference(arr, Q, N, M);
}
}
// This code is contributed by shikhasingrajput
Javascript
4
1
1
时间复杂度: O(N*log(N))
辅助空间: O(N*N)