📜  大小为K的最大连续子数组

📅  最后修改于: 2021-04-22 08:05:22             🧑  作者: Mango

给定一个整数数组arr []和一个整数K ,任务是找到大小为K的最大连续子数组。如果两个子数组中的第一个不匹配元素在X中的值都大于Y中的值,则称子数组X大于子数组Y。

例子:

方法:生成所有大小为K的子数组并将其存储在任何数据结构中。对所有子数组进行排序,答案将是排序后的数据结构中的最后一个子数组。在C++中,我们可以使用向量的向量来存储大小为K的子数组。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that returns the sub-array
vector findSubarray(int a[], int k, int n)
{
 
    // Data-structure to store all
    // the sub-arrays of size K
    vector > vec;
 
    // Iterate to find all the sub-arrays
    for (int i = 0; i < n - k + 1; i++) {
        vector temp;
 
        // Store the sub-array elements in the array
        for (int j = i; j < i + k; j++) {
            temp.push_back(a[j]);
        }
 
        // Push the vector in the container
        vec.push_back(temp);
    }
 
    // Sort the vector of elements
    sort(vec.begin(), vec.end());
 
    // The last sub-array in the sorted order
    // will be the answer
    return vec[vec.size() - 1];
}
 
// Driver code
int main()
{
    int a[] = { 1, 4, 3, 2, 5 };
    int k = 4;
    int n = sizeof(a) / sizeof(a[0]);
 
    // Get the sub-array
    vector ans = findSubarray(a, k, n);
 
    for (auto it : ans)
        cout << it << " ";
}


Java
// Java implementation of the approach
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function that returns the sub-array
static ArrayList findSubarray(int a[],
                                       int k, int n)
{
     
    // Data-structure to store all
    // the sub-arrays of size K
    ArrayList<
    ArrayList> vec = new ArrayList<
                                  ArrayList>();
                                   
    // Iterate to find all the sub-arrays
    for(int i = 0; i < n - k + 1; i++)
    {
        ArrayList temp = new ArrayList();
         
        // Store the sub-array elements in the array
        for(int j = i; j < i + k; j++)
        {
            temp.add(a[j]);
        }
         
        // Push the vector in the container
        vec.add(temp);
    }
     
     // Sort the vector of elements
    Collections.sort(vec, new Comparator>()
    {   
        @Override
        public int compare(ArrayList o1,
                           ArrayList o2)
        {
            return o1.get(0).compareTo(o2.get(0));
        }              
    });
     
    // The last sub-array in the sorted order
    // will be the answer
    return vec.get(vec.size() - 1);
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 1, 4, 3, 2, 5 };
    int k = 4;
    int n = a.length;
     
    // Get the sub-array
    ArrayList ans = findSubarray(a, k, n);
     
    for(int it: ans)
    {
        System.out.print(it + " ");
    }
}
}
 
// This code is contributed by avanitrachhadiya2155


Python3
# Python3 implementation of the approach
 
# Function that returns the sub-array
def findSubarray(a, k, n):
 
    # Data-structure to store all
    # the sub-arrays of size K
    vec=[]
 
    # Iterate to find all the sub-arrays
    for i in range(n-k+1):
        temp=[]
 
        # Store the sub-array elements in the array
        for j in range(i,i+k):
            temp.append(a[j])
 
        # Push the vector in the container
        vec.append(temp)
 
    # Sort the vector of elements
    vec=sorted(vec)
 
    # The last sub-array in the sorted order
    # will be the answer
    return vec[len(vec) - 1]
 
# Driver code
 
a =[ 1, 4, 3, 2, 5 ]
k = 4
n = len(a)
 
# Get the sub-array
ans = findSubarray(a, k, n)
 
for it in ans:
    print(it,end=" ")
     
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
using System.Linq;
 
public class GFG
{
 
  // Function that returns the sub-array
  static List findSubarray(int[] a, int k, int n)
  {
    // Data-structure to store all
    // the sub-arrays of size K
    List> vec = new List>();
 
    // Iterate to find all the sub-arrays
    for(int i = 0; i < n - k + 1; i++)
    {
      List temp = new List();
 
      // Store the sub-array elements in the array
      for(int j = i; j < i + k; j++)
      {
        temp.Add(a[j]);
      }
 
      // Push the vector in the container
      vec.Add(temp);
    }
 
    // Sort the vector of elements
    vec.OrderBy( l => l[0]);
 
    // The last sub-array in the sorted order
    // will be the answer
    return vec[vec.Count - 1];
  }
 
  // Driver code
  static public void Main (){
    int[] a = { 1, 4, 3, 2, 5 };
    int k = 4;
    int n = a.Length;
 
    // Get the sub-array
    List ans = findSubarray(a, k, n);
    foreach(int it in ans)
    {
      Console.Write(it + " ");
    }
  }
}
 
// This code is contributed by rag2127


输出:
4 3 2 5