📌  相关文章
📜  最小化数组中相邻元素之间的最大差异

📅  最后修改于: 2021-05-17 06:06:15             🧑  作者: Mango

给定一个非递减数组arr []和一个整数K ,任务是从数组中删除K个元素,以使相邻元素之间的最大差最小。
注意: K

例子:

方法1:蛮力这个想法是生成大小为N-K的数组的子集,并计算每个子序列中相邻元素的最大差值。最后,找到最大差异的最小值。

下面是上述方法的实现:

C++
// C++ implementation to find the
// minimum of the maximum difference
// of the adjacent elements after
// removing K elements from the array
 
#include 
 
using namespace std;
 
// Function to find the minimum
// of the maximum difference of the
// adjacent elements after removing
// K elements from the array
int minimumAdjacentDifference(int a[],
                        int n, int k)
{
    // Intialising the
    // minimum difference
    int minDiff = INT_MAX;
 
    // Traversing over subsets
    // in iterative manner
    for (int i = 0; i < (1 << n); i++) {
         
        // Number of elements to
        // be taken in the subset
        // ON bits of i represent
        // elements not to be removed
        int cnt = __builtin_popcount(i);
 
        // If the removed
        // set is of size k
        if (cnt == n - k) {
             
            // Creating the new array
            // after removing elements
            vector temp;
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) != 0)
                    temp.push_back(a[j]);
            }
            // Maximum difference of adjacent
            // elements of remaining array
            int maxDiff = INT_MIN;
            for (int j = 0; j < temp.size() - 1; j++) {
                maxDiff = max(maxDiff,
                   temp[j + 1] - temp[j]);
            }
            minDiff = min(minDiff, maxDiff);
        }
    }
    return minDiff;
}
 
// Driver Code
int main()
{
    int n = 5;
    int k = 2;
 
    int a[n] = { 3, 7, 8, 10, 14 };
 
    cout << minimumAdjacentDifference(a, n, k);
    return 0;
}


Java
// Java implementation to find the
// minimum of the maximum difference
// of the adjacent elements after
// removing K elements from the array
import java.util.*;
 
class GFG{
 
    // Function to find the minimum
    // of the maximum difference of the
    // adjacent elements after removing
    // K elements from the array
    static int minimumAdjacentDifference(int a[],
                            int n, int k)
    {
        // Intialising the
        // minimum difference
        int minDiff = Integer.MAX_VALUE;
     
        // Traversing over subsets
        // in iterative manner
        for (int i = 0; i < (1 << n); i++) {
             
            // Number of elements to
            // be taken in the subset
            // ON bits of i represent
            // elements not to be removed
            int cnt = Integer.bitCount(i);
     
            // If the removed
            // set is of size k
            if (cnt == n - k) {
                 
                // Creating the new array
                // after removing elements
                 Vector temp = new Vector();
                for (int j = 0; j < n; j++) {
                    if ((i & (1 << j)) != 0)
                        temp.add(a[j]);
                }
 
                // Maximum difference of adjacent
                // elements of remaining array
                int maxDiff = Integer.MIN_VALUE;
                for (int j = 0; j < temp.size() - 1; j++) {
                    maxDiff = Math.max(maxDiff,
                    temp.get(j + 1) - temp.get(j));
                }
                minDiff = Math.min(minDiff, maxDiff);
            }
        }
        return minDiff;
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int n = 5;
        int k = 2;
     
        int a[] = { 3, 7, 8, 10, 14 };
     
        System.out.println(minimumAdjacentDifference(a, n, k));
    }
}
 
 
// This code is contributed by AbhiThakur


Python3
# Python3 implementation to find the
# minimum of the maximum difference
# of the adjacent elements after
# removing K elements from the array
import sys
 
INT_MAX = sys.maxsize;
INT_MIN = -(sys.maxsize - 1)
 
# Function to find the minimum
# of the maximum difference of the
# adjacent elements after removing
# K elements from the array
def minimumAdjacentDifference(a, n, k) :
 
    # Intialising the
    # minimum difference
    minDiff = INT_MAX;
 
    # Traversing over subsets
    # in iterative manner
    for i in range( 1<


C#
// C# implementation to find the
// minimum of the maximum difference
// of the adjacent elements after
// removing K elements from the array
using System;
using System.Collections.Generic;
 
class GFG{
  
    // Function to find the minimum
    // of the maximum difference of the
    // adjacent elements after removing
    // K elements from the array
    static int minimumAdjacentDifference(int []a,
                            int n, int k)
    {
        // Intialising the
        // minimum difference
        int minDiff = int.MaxValue;
      
        // Traversing over subsets
        // in iterative manner
        for (int i = 0; i < (1 << n); i++) {
              
            // Number of elements to
            // be taken in the subset
            // ON bits of i represent
            // elements not to be removed
            int cnt = countSetBits(i);
      
            // If the removed
            // set is of size k
            if (cnt == n - k) {
                  
                // Creating the new array
                // after removing elements
                 List temp = new List();
                for (int j = 0; j < n; j++) {
                    if ((i & (1 << j)) != 0)
                        temp.Add(a[j]);
                }
  
                // Maximum difference of adjacent
                // elements of remaining array
                int maxDiff = int.MinValue;
                for (int j = 0; j < temp.Count - 1; j++) {
                    maxDiff = Math.Max(maxDiff,
                    temp[j + 1] - temp[j]);
                }
                minDiff = Math.Min(minDiff, maxDiff);
            }
        }
        return minDiff;
    }
     static int countSetBits(int x)
     {
         int setBits = 0;
         while (x != 0) {
             x = x & (x - 1);
             setBits++;
         }
         return setBits;
     }
    // Driver Code
    public static void Main(String []args)
    {
        int n = 5;
        int k = 2;
      
        int []a = { 3, 7, 8, 10, 14 };
      
        Console.WriteLine(minimumAdjacentDifference(a, n, k));
    }
}
  
// This code is contributed by sapnasingh4991


C++
// C++ implementation to find the
// minimum of the maximum difference
// of the adjacent elements after
// removing K elements from the array
 
#include 
 
using namespace std;
 
// Function to find the minimum
// of the maximum difference of the
// adjacent elements after removing
// K elements from the array
int minimumAdjacentDifference(int a[],
                        int n, int k)
{
    // Intialising the
    // minimum difference
    int minDiff = INT_MAX;
 
    // Iterating over all
    // subarrays of size n-k
    for (int i = 0; i <= k; i++) {
         
        // Maximum difference after
        // removing elements
        int maxDiff = INT_MIN;
        for (int j = 0; j < n - k - 1; j++) {
            for (int p = i; p <= i + j; p++) {
                maxDiff = max(maxDiff,
                     a[p + 1] - a[p]);
            }
        }
        // Minimum Adjacent Difference
        minDiff = min(minDiff, maxDiff);
    }
    return minDiff;
}
 
// Driver Code
int main()
{
    int n = 5;
    int k = 2;
 
    int a[n] = { 3, 7, 8, 10, 14 };
 
    cout << minimumAdjacentDifference(a, n, k);
    return 0;
}


Java
// Java implementation to find the
// minimum of the maximum difference
// of the adjacent elements after
// removing K elements from the array
class GFG {
     
    // Function to find the minimum
    // of the maximum difference of the
    // adjacent elements after removing
    // K elements from the array
    static int minimumAdjacentDifference(int a[],
                            int n, int k)
    {
        // Intialising the
        // minimum difference
        int minDiff = Integer.MAX_VALUE;
     
        // Iterating over all
        // subarrays of size n-k
        for (int i = 0; i <= k; i++) {
             
            // Maximum difference after
            // removing elements
            int maxDiff = Integer.MIN_VALUE;
            for (int j = 0; j < n - k - 1; j++) {
                for (int p = i; p <= i + j; p++) {
                    maxDiff = Math.max(maxDiff,
                        a[p + 1] - a[p]);
                }
            }
  
            // Minimum Adjacent Difference
            minDiff = Math.min(minDiff, maxDiff);
        }
        return minDiff;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int n = 5;
        int k = 2;
     
        int []a = { 3, 7, 8, 10, 14 };
     
        System.out.println(minimumAdjacentDifference(a, n, k));
    }
}
 
// This code is contributed by Yash_R


Python3
# Python3 implementation to find the
# minimum of the maximum difference
# of the adjacent elements after
# removing K elements from the array
import sys
 
INT_MAX = sys.maxsize;
INT_MIN = -(sys.maxsize - 1);
 
# Function to find the minimum
# of the maximum difference of the
# adjacent elements after removing
# K elements from the array
def minimumAdjacentDifference(a, n, k) :
     
    # Intialising the
    # minimum difference
    minDiff = INT_MAX;
 
    # Iterating over all
    # subarrays of size n-k
    for i in range(k + 1) :
         
        # Maximum difference after
        # removing elements
        maxDiff = INT_MIN;
        for j in range( n - k - 1) :
            for p in range(i, i + j + 1) :
                maxDiff = max(maxDiff, a[p + 1] - a[p]);
     
        # Minimum Adjacent Difference
        minDiff = min(minDiff, maxDiff);
         
    return minDiff;
 
# Driver Code
if __name__ == "__main__" :
 
    n = 5;
    k = 2;
 
    a = [ 3, 7, 8, 10, 14 ];
 
    print(minimumAdjacentDifference(a, n, k));
 
# This code is contributed by AnkitRai01


C#
// C# implementation to find the
// minimum of the maximum difference
// of the adjacent elements after
// removing K elements from the array
using System;
 
class GFG {
     
    // Function to find the minimum
    // of the maximum difference of the
    // adjacent elements after removing
    // K elements from the array
    static int minimumAdjacentDifference(int []a,
                            int n, int k)
    {
        // Intialising the
        // minimum difference
        int minDiff = int.MaxValue;
     
        // Iterating over all
        // subarrays of size n-k
        for (int i = 0; i <= k; i++) {
             
            // Maximum difference after
            // removing elements
            int maxDiff = int.MinValue;
            for (int j = 0; j < n - k - 1; j++) {
                for (int p = i; p <= i + j; p++) {
                    maxDiff = Math.Max(maxDiff,
                        a[p + 1] - a[p]);
                }
            }
  
            // Minimum Adjacent Difference
            minDiff = Math.Min(minDiff, maxDiff);
        }
        return minDiff;
    }
     
    // Driver Code
    public static void Main (string[] args)
    {
        int n = 5;
        int k = 2;
     
        int []a = { 3, 7, 8, 10, 14 };
     
        Console.WriteLine(minimumAdjacentDifference(a, n, k));
    }
}
 
// This code is contributed by Yash_R


C++
// C++ implementation to find the
// minimum of the maximum difference
// of the adjacent elements after
// removing K elements from the array
 
#include 
 
using namespace std;
 
// Function to find the minimum
// different in the subarrays
// of size K in the array
int findKMin(int arr[], int n, int k)
{
    // Create a Double Ended Queue, Qi
    // that will store indexes
    // of array elements, queue will
    // store indexes of useful elements
    // in every window
    std::deque Qi(k);
 
    // Process first k (or first window)
    // elements of array
    int i;
    for (i = 0; i < k; ++i) {
        // For every element,
        // the previous smaller elements
        // are useless so remove them from Qi
        while ((!Qi.empty()) &&
               arr[i] >= arr[Qi.back()])
            Qi.pop_back(); // Remove from rear
 
        // Add new element at rear of queue
        Qi.push_back(i);
    }
     
    int minDiff = INT_MAX;
     
    // Process rest of the elements,
    // i.e., from arr[k] to arr[n-1]
    for (; i < n; ++i) {
        // The element at the front
        // of the queue is the largest
        //  element of previous window
        minDiff = min(minDiff, arr[Qi.front()]);
 
        // Remove the elements
        // which are out of this window
        while ((!Qi.empty()) && Qi.front() <= i - k)
            Qi.pop_front();
 
        // Remove all elements smaller
        // than the currently being
        // added element (remove useless elements)
        while ((!Qi.empty()) &&
                arr[i] >= arr[Qi.back()])
            Qi.pop_back();
 
        // Add current element
        // at the rear of Qi
        Qi.push_back(i);
    }
 
    // compare the maximum
    // element of last window
    minDiff = min(minDiff, arr[Qi.front()]);
    return minDiff;
}
 
// Function to find the minimum
// of the maximum difference of the
// adjacent elements after removing
// K elements from the array
int minimumAdjacentDifference(int a[],
                          int n, int k)
{
 
    // Create the difference array
    int diff[n - 1];
    for (int i = 0; i < n - 1; i++) {
        diff[i] = a[i + 1] - a[i];
    }
 
    // find minimum of all maximum
    // of subarray sizes n - k - 1
    int answer = findKMin(diff,
                  n - 1, n - k - 1);
    return answer;
}
 
// Driver Code
int main()
{
    int n = 5;
    int k = 2;
 
    int a[n] = { 3, 7, 8, 10, 14 };
 
    cout << minimumAdjacentDifference(a, n, k);
    return 0;
}


Java
// Java implementation to find the
// minimum of the maximum difference
// of the adjacent elements after
// removing K elements from the array
import java.util.*;
import java.lang.*;
 
class GFG{
     
// Function to find the minimum
// different in the subarrays
// of size K in the array
static int findKMin(int arr[], int n, int k)
{
     
    // Create a Double Ended Queue, Qi
    // that will store indexes
    // of array elements, queue will
    // store indexes of useful elements
    // in every window
    Deque Qi = new LinkedList<>();
     
    // Process first k (or first window)
    // elements of array
    int i;
    for(i = 0; i < k; ++i)
    {
         
        // For every element,
        // the previous smaller elements
        // are useless so remove them from Qi
        while ((!Qi.isEmpty()) &&
               arr[i] >= arr[Qi.peekLast()])
                
            // Remove from rear
            Qi.pollLast();
  
        // Add new element at rear of queue
        Qi.addLast(i);
    }
      
    int minDiff = Integer.MAX_VALUE;
      
    // Process rest of the elements,
    // i.e., from arr[k] to arr[n-1]
    for(; i < n; ++i)
    {
         
        // The element at the front
        // of the queue is the largest
        //  element of previous window
        minDiff = Math.min(minDiff,
                           arr[Qi.peekFirst()]);
  
        // Remove the elements
        // which are out of this window
        while ((!Qi.isEmpty()) &&
                 Qi.peekFirst() <= i - k)
            Qi.pollFirst();
  
        // Remove all elements smaller
        // than the currently being
        // added element (remove useless elements)
        while ((!Qi.isEmpty()) &&
                arr[i] >= arr[Qi.peekLast()])
            Qi.pollLast();
  
        // Add current element
        // at the rear of Qi
        Qi.addLast(i);
    }
  
    // Compare the maximum
    // element of last window
    minDiff = Math.min(minDiff,
                       arr[Qi.peekFirst()]);
    return minDiff;
}
  
// Function to find the minimum
// of the maximum difference of the
// adjacent elements after removing
// K elements from the array
static int minimumAdjacentDifference(int a[],
                                     int n, int k)
{
     
    // Create the difference array
    int[] diff = new int[n - 1];
    for(int i = 0; i < n - 1; i++)
    {
        diff[i] = a[i + 1] - a[i];
    }
  
    // find minimum of all maximum
    // of subarray sizes n - k - 1
    int answer = findKMin(diff,
                          n - 1,
                          n - k - 1);
    return answer;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5;
    int k = 2;
     
    int a[] = { 3, 7, 8, 10, 14 };
     
    System.out.println(minimumAdjacentDifference(a, n, k));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 implementation to find the
# minimum of the maximum difference
# of the adjacent elements after
# removing K elements from the array
import sys
 
# Function to find the minimum
# different in the subarrays
# of size K in the array
def findKMin(arr, n, k):
     
    # Create a Double Ended Queue, Qi
    # that will store indexes
    # of array elements, queue will
    # store indexes of useful elements
    # in every window
    Qi = []
  
    # Process first k (or first window)
    # elements of array
    i = 0
     
    for j in range(k):
         
        # For every element,
        # the previous smaller elements
        # are useless so remove them from Qi
        while ((len(Qi) != 0) and
                 arr[i] >= arr[Qi[-1]]):
            Qi.pop() # Remove from rear
  
        # Add new element at rear of queue
        Qi.append(i)
        i += 1
         
    minDiff = sys.maxsize;
      
    # Process rest of the elements,
    # i.e., from arr[k] to arr[n-1]
    for j in range(i, n):
 
        # The element at the front
        # of the queue is the largest
        #  element of previous window
        minDiff = min(minDiff, arr[Qi[0]])
  
        # Remove the elements
        # which are out of this window
        while ((len(Qi) != 0) and
                  Qi[0] <= i - k):
            Qi.pop(0)
  
        # Remove all elements smaller
        # than the currently being
        # added element (remove
        # useless elements)
        while ((len(Qi) != 0) and
                 arr[i] >= arr[Qi[-1]]):
            Qi.pop()
  
        # Add current element
        # at the rear of Qi
        Qi.append(i)
        i += 1
         
    # Compare the maximum
    # element of last window
    minDiff = min(minDiff, arr[Qi[0]])
     
    return minDiff
 
# Function to find the minimum
# of the maximum difference of the
# adjacent elements after removing
# K elements from the array
def minimumAdjacentDifference(a, n, k):
  
    # Create the difference array
    diff = [0 for i in range(n - 1)]
     
    for i in range(n - 1):
        diff[i] = a[i + 1] - a[i]
  
    # Find minimum of all maximum
    # of subarray sizes n - k - 1
    answer = findKMin(diff, n - 1,
                        n - k - 1)
    return answer
 
# Driver code   
if __name__=="__main__":
     
    n = 5
    k = 2
  
    a = [ 3, 7, 8, 10, 14 ]
  
    print(minimumAdjacentDifference(a, n, k))
 
# This code is contributed by rutvik_56


输出:
2






时间复杂度: O(2 N * N)
方法2:最佳方法

  • 经过仔细观察,可以注意到,如果从数组之间的某个位置(即不是结束元素)移除元素,则其余元素的最大差异只能增加或保持不变。
    例如:
Let the given array be {1, 5, 6},

If we remove the element 5(not the end element), 
then the maximum difference will always increase.

Therefore, It is always better to remove end elements.

  • 这意味着删除K个元素后得到的数组将是大小为N – K的原始数组的子数组。
  • 因此,我们可以遍历大小为N – K的所有子数组,并为每个子数组找到相邻元素之间的最大差。最后,找到所有相邻元素的最大差异中的最小值。

下面是上述方法的实现:

C++

// C++ implementation to find the
// minimum of the maximum difference
// of the adjacent elements after
// removing K elements from the array
 
#include 
 
using namespace std;
 
// Function to find the minimum
// of the maximum difference of the
// adjacent elements after removing
// K elements from the array
int minimumAdjacentDifference(int a[],
                        int n, int k)
{
    // Intialising the
    // minimum difference
    int minDiff = INT_MAX;
 
    // Iterating over all
    // subarrays of size n-k
    for (int i = 0; i <= k; i++) {
         
        // Maximum difference after
        // removing elements
        int maxDiff = INT_MIN;
        for (int j = 0; j < n - k - 1; j++) {
            for (int p = i; p <= i + j; p++) {
                maxDiff = max(maxDiff,
                     a[p + 1] - a[p]);
            }
        }
        // Minimum Adjacent Difference
        minDiff = min(minDiff, maxDiff);
    }
    return minDiff;
}
 
// Driver Code
int main()
{
    int n = 5;
    int k = 2;
 
    int a[n] = { 3, 7, 8, 10, 14 };
 
    cout << minimumAdjacentDifference(a, n, k);
    return 0;
}

Java

// Java implementation to find the
// minimum of the maximum difference
// of the adjacent elements after
// removing K elements from the array
class GFG {
     
    // Function to find the minimum
    // of the maximum difference of the
    // adjacent elements after removing
    // K elements from the array
    static int minimumAdjacentDifference(int a[],
                            int n, int k)
    {
        // Intialising the
        // minimum difference
        int minDiff = Integer.MAX_VALUE;
     
        // Iterating over all
        // subarrays of size n-k
        for (int i = 0; i <= k; i++) {
             
            // Maximum difference after
            // removing elements
            int maxDiff = Integer.MIN_VALUE;
            for (int j = 0; j < n - k - 1; j++) {
                for (int p = i; p <= i + j; p++) {
                    maxDiff = Math.max(maxDiff,
                        a[p + 1] - a[p]);
                }
            }
  
            // Minimum Adjacent Difference
            minDiff = Math.min(minDiff, maxDiff);
        }
        return minDiff;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int n = 5;
        int k = 2;
     
        int []a = { 3, 7, 8, 10, 14 };
     
        System.out.println(minimumAdjacentDifference(a, n, k));
    }
}
 
// This code is contributed by Yash_R

Python3

# Python3 implementation to find the
# minimum of the maximum difference
# of the adjacent elements after
# removing K elements from the array
import sys
 
INT_MAX = sys.maxsize;
INT_MIN = -(sys.maxsize - 1);
 
# Function to find the minimum
# of the maximum difference of the
# adjacent elements after removing
# K elements from the array
def minimumAdjacentDifference(a, n, k) :
     
    # Intialising the
    # minimum difference
    minDiff = INT_MAX;
 
    # Iterating over all
    # subarrays of size n-k
    for i in range(k + 1) :
         
        # Maximum difference after
        # removing elements
        maxDiff = INT_MIN;
        for j in range( n - k - 1) :
            for p in range(i, i + j + 1) :
                maxDiff = max(maxDiff, a[p + 1] - a[p]);
     
        # Minimum Adjacent Difference
        minDiff = min(minDiff, maxDiff);
         
    return minDiff;
 
# Driver Code
if __name__ == "__main__" :
 
    n = 5;
    k = 2;
 
    a = [ 3, 7, 8, 10, 14 ];
 
    print(minimumAdjacentDifference(a, n, k));
 
# This code is contributed by AnkitRai01

C#

// C# implementation to find the
// minimum of the maximum difference
// of the adjacent elements after
// removing K elements from the array
using System;
 
class GFG {
     
    // Function to find the minimum
    // of the maximum difference of the
    // adjacent elements after removing
    // K elements from the array
    static int minimumAdjacentDifference(int []a,
                            int n, int k)
    {
        // Intialising the
        // minimum difference
        int minDiff = int.MaxValue;
     
        // Iterating over all
        // subarrays of size n-k
        for (int i = 0; i <= k; i++) {
             
            // Maximum difference after
            // removing elements
            int maxDiff = int.MinValue;
            for (int j = 0; j < n - k - 1; j++) {
                for (int p = i; p <= i + j; p++) {
                    maxDiff = Math.Max(maxDiff,
                        a[p + 1] - a[p]);
                }
            }
  
            // Minimum Adjacent Difference
            minDiff = Math.Min(minDiff, maxDiff);
        }
        return minDiff;
    }
     
    // Driver Code
    public static void Main (string[] args)
    {
        int n = 5;
        int k = 2;
     
        int []a = { 3, 7, 8, 10, 14 };
     
        Console.WriteLine(minimumAdjacentDifference(a, n, k));
    }
}
 
// This code is contributed by Yash_R
输出:
2






时间复杂度: O(N * K 2 )

方法3:高效方法

  • 使用方法2的思想,我们需要找到大小为N – K的所有子数组的最大相邻元素差的最小值。如果我们创建一个差数组,即初始数组的相邻元素差的数组,那么我们需要做的是找到此差异数组大小为N – K – 1的所有子数组的最大值的最小元素(因为该最大值代表原始数组大小为N – K的子数组的最大相邻差异)。
  • 为了执行此操作,我们可以使用带有双端队列的滑动窗口方法。有关此方法,请参考最大滑动窗口(大小为K的所有子阵列的最大值)。

下面是上述方法的实现:

C++

// C++ implementation to find the
// minimum of the maximum difference
// of the adjacent elements after
// removing K elements from the array
 
#include 
 
using namespace std;
 
// Function to find the minimum
// different in the subarrays
// of size K in the array
int findKMin(int arr[], int n, int k)
{
    // Create a Double Ended Queue, Qi
    // that will store indexes
    // of array elements, queue will
    // store indexes of useful elements
    // in every window
    std::deque Qi(k);
 
    // Process first k (or first window)
    // elements of array
    int i;
    for (i = 0; i < k; ++i) {
        // For every element,
        // the previous smaller elements
        // are useless so remove them from Qi
        while ((!Qi.empty()) &&
               arr[i] >= arr[Qi.back()])
            Qi.pop_back(); // Remove from rear
 
        // Add new element at rear of queue
        Qi.push_back(i);
    }
     
    int minDiff = INT_MAX;
     
    // Process rest of the elements,
    // i.e., from arr[k] to arr[n-1]
    for (; i < n; ++i) {
        // The element at the front
        // of the queue is the largest
        //  element of previous window
        minDiff = min(minDiff, arr[Qi.front()]);
 
        // Remove the elements
        // which are out of this window
        while ((!Qi.empty()) && Qi.front() <= i - k)
            Qi.pop_front();
 
        // Remove all elements smaller
        // than the currently being
        // added element (remove useless elements)
        while ((!Qi.empty()) &&
                arr[i] >= arr[Qi.back()])
            Qi.pop_back();
 
        // Add current element
        // at the rear of Qi
        Qi.push_back(i);
    }
 
    // compare the maximum
    // element of last window
    minDiff = min(minDiff, arr[Qi.front()]);
    return minDiff;
}
 
// Function to find the minimum
// of the maximum difference of the
// adjacent elements after removing
// K elements from the array
int minimumAdjacentDifference(int a[],
                          int n, int k)
{
 
    // Create the difference array
    int diff[n - 1];
    for (int i = 0; i < n - 1; i++) {
        diff[i] = a[i + 1] - a[i];
    }
 
    // find minimum of all maximum
    // of subarray sizes n - k - 1
    int answer = findKMin(diff,
                  n - 1, n - k - 1);
    return answer;
}
 
// Driver Code
int main()
{
    int n = 5;
    int k = 2;
 
    int a[n] = { 3, 7, 8, 10, 14 };
 
    cout << minimumAdjacentDifference(a, n, k);
    return 0;
}

Java

// Java implementation to find the
// minimum of the maximum difference
// of the adjacent elements after
// removing K elements from the array
import java.util.*;
import java.lang.*;
 
class GFG{
     
// Function to find the minimum
// different in the subarrays
// of size K in the array
static int findKMin(int arr[], int n, int k)
{
     
    // Create a Double Ended Queue, Qi
    // that will store indexes
    // of array elements, queue will
    // store indexes of useful elements
    // in every window
    Deque Qi = new LinkedList<>();
     
    // Process first k (or first window)
    // elements of array
    int i;
    for(i = 0; i < k; ++i)
    {
         
        // For every element,
        // the previous smaller elements
        // are useless so remove them from Qi
        while ((!Qi.isEmpty()) &&
               arr[i] >= arr[Qi.peekLast()])
                
            // Remove from rear
            Qi.pollLast();
  
        // Add new element at rear of queue
        Qi.addLast(i);
    }
      
    int minDiff = Integer.MAX_VALUE;
      
    // Process rest of the elements,
    // i.e., from arr[k] to arr[n-1]
    for(; i < n; ++i)
    {
         
        // The element at the front
        // of the queue is the largest
        //  element of previous window
        minDiff = Math.min(minDiff,
                           arr[Qi.peekFirst()]);
  
        // Remove the elements
        // which are out of this window
        while ((!Qi.isEmpty()) &&
                 Qi.peekFirst() <= i - k)
            Qi.pollFirst();
  
        // Remove all elements smaller
        // than the currently being
        // added element (remove useless elements)
        while ((!Qi.isEmpty()) &&
                arr[i] >= arr[Qi.peekLast()])
            Qi.pollLast();
  
        // Add current element
        // at the rear of Qi
        Qi.addLast(i);
    }
  
    // Compare the maximum
    // element of last window
    minDiff = Math.min(minDiff,
                       arr[Qi.peekFirst()]);
    return minDiff;
}
  
// Function to find the minimum
// of the maximum difference of the
// adjacent elements after removing
// K elements from the array
static int minimumAdjacentDifference(int a[],
                                     int n, int k)
{
     
    // Create the difference array
    int[] diff = new int[n - 1];
    for(int i = 0; i < n - 1; i++)
    {
        diff[i] = a[i + 1] - a[i];
    }
  
    // find minimum of all maximum
    // of subarray sizes n - k - 1
    int answer = findKMin(diff,
                          n - 1,
                          n - k - 1);
    return answer;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5;
    int k = 2;
     
    int a[] = { 3, 7, 8, 10, 14 };
     
    System.out.println(minimumAdjacentDifference(a, n, k));
}
}
 
// This code is contributed by offbeat

Python3

# Python3 implementation to find the
# minimum of the maximum difference
# of the adjacent elements after
# removing K elements from the array
import sys
 
# Function to find the minimum
# different in the subarrays
# of size K in the array
def findKMin(arr, n, k):
     
    # Create a Double Ended Queue, Qi
    # that will store indexes
    # of array elements, queue will
    # store indexes of useful elements
    # in every window
    Qi = []
  
    # Process first k (or first window)
    # elements of array
    i = 0
     
    for j in range(k):
         
        # For every element,
        # the previous smaller elements
        # are useless so remove them from Qi
        while ((len(Qi) != 0) and
                 arr[i] >= arr[Qi[-1]]):
            Qi.pop() # Remove from rear
  
        # Add new element at rear of queue
        Qi.append(i)
        i += 1
         
    minDiff = sys.maxsize;
      
    # Process rest of the elements,
    # i.e., from arr[k] to arr[n-1]
    for j in range(i, n):
 
        # The element at the front
        # of the queue is the largest
        #  element of previous window
        minDiff = min(minDiff, arr[Qi[0]])
  
        # Remove the elements
        # which are out of this window
        while ((len(Qi) != 0) and
                  Qi[0] <= i - k):
            Qi.pop(0)
  
        # Remove all elements smaller
        # than the currently being
        # added element (remove
        # useless elements)
        while ((len(Qi) != 0) and
                 arr[i] >= arr[Qi[-1]]):
            Qi.pop()
  
        # Add current element
        # at the rear of Qi
        Qi.append(i)
        i += 1
         
    # Compare the maximum
    # element of last window
    minDiff = min(minDiff, arr[Qi[0]])
     
    return minDiff
 
# Function to find the minimum
# of the maximum difference of the
# adjacent elements after removing
# K elements from the array
def minimumAdjacentDifference(a, n, k):
  
    # Create the difference array
    diff = [0 for i in range(n - 1)]
     
    for i in range(n - 1):
        diff[i] = a[i + 1] - a[i]
  
    # Find minimum of all maximum
    # of subarray sizes n - k - 1
    answer = findKMin(diff, n - 1,
                        n - k - 1)
    return answer
 
# Driver code   
if __name__=="__main__":
     
    n = 5
    k = 2
  
    a = [ 3, 7, 8, 10, 14 ]
  
    print(minimumAdjacentDifference(a, n, k))
 
# This code is contributed by rutvik_56
输出:
2






时间复杂度: O(N)

如果您希望与行业专家一起参加实时课程,请参阅Geeks在线课程