0

Here is my problem: Given a set of arrays of size N and an integer K, you have to find the maximum integer for each and every contiguous subarray of size K for each of the given arrays. example: Sample Input

5 2
3 4 6 3 4

Sample Output

4 6 6 4

Here is my code.please help me.code is right but whenever I submit it,it shows me time limit exceed.

#include<bits/stdc++.h>
    #include<deque>
    #include<algorithm>
    using namespace std;
    int print(deque<int>arr,int n,int k)
    {
        int i,j,max;

        for(i=0;i<=n-k;i++)
        {
            max=arr[i];
            for(j=1;j<k;j++)
            {
                if(arr[i+j]>max)
                max=arr[i+j];
            }
                printf("%d ",max);
        }
        return 0;
    }
    int main()
    {
        ios_base::sync_with_stdio(false); 
        cin.tie(NULL); 
        int t,n,k;
        cin>>t;

        while(t--)
        {
            cin>>n>>k;
            deque<int>arr;

            for(int i=0;i<n;++i)
            {
                int value;
                scanf("%d",&value);
                arr.push_back(value);
            }
            print(arr,n,k);
            printf("\n");
        }
        return 0;
    }
PaulMcKenzie
  • 31,493
  • 4
  • 19
  • 38
developer
  • 11
  • 4
  • You [should not `#include `](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). Also you don't seem to know what it actually does, so just throw it away and forget it ever existed. That would be the first optimization ;) – churill Jun 04 '20 at 20:06
  • [Why should I not #include ?](https://stackoverflow.com/q/31816095/5910058) – Jesper Juhl Jun 04 '20 at 20:06
  • *It shows me time limit exceed.* -- More than likely it is because you wrote a nested `for` loop, making the function `O(n^2)` time complexity. It looks like you got this question from one of those "online judge" websites. If you did, those sites ask questions that always have naive solutions that will fail due to time limit exceeded. The goal is to see if you can come up with a better solution by using some usage of a data structure, algorithm, etc. or some math trick. Naive brute force `for` loops almost never pass these tests. – PaulMcKenzie Jun 04 '20 at 21:06
  • @churill --after removing #include doesn't affect at all. Still It shows me TLE(Time limit exceed) – developer Jun 05 '20 at 08:11
  • You never mentioned what are the constraints of `N` and `K`. If `N` can be 10000 elements, and `K` is 1000, how many total iterations would you be executing in your nested `for` loop? Maybe that is the reason for the time-limit exceeded (don't use abbreviations, as many do not know what "TLE" means). Again, that sample input is there to fool you into thinking that naive solutions will work -- but they only work for small data. – PaulMcKenzie Jun 05 '20 at 13:16
  • @PaulMcKenzie Limit is as follow: 1<=T<=1000 1<=N<=10000 1<=K<=N 1<=(ith element in A)<=10000 and I know that there is nested loop,,which is O(n^2) ,it will take more time,,,but what's an alternative?? – developer Jun 08 '20 at 12:00
  • You can try building a max-heap of K items and figure out how and when to get/replace the top item in the heap. The problem with your solution is simple -- you process K items, but then you throw away all of the information about those K items except which one was largest. You now go back and reprocess those very same K items (actually K-1) items because you lost the information on them. That's the trick -- is there a way to go through K items, somehow "remember" them, so that when you move the window up by one item, you're not doing practically the same work. – PaulMcKenzie Jun 08 '20 at 12:57

0 Answers0