Answers for "first negative number in window"

C++
0

first negative number in window

//complexity O(n)
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cout<<"Enter the size of the array:"<<endl;
    cin>>n;
    int arr[n];
    cout<<"enter the elements of the array:"<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    int k;
    cout<<"enter the size of the window:"<<endl;
    cin>>k;
    list<int>l;
    vector<int>vec;
    int i=0,j=0;
    while(j<n)
    {
        if(arr[j]<0)
        {
            l.push_back(arr[j]);
        }
        if(j-i+1<k)
        {
            j++;
        }
        else if(j-i+1==k)
        {
            if(l.size()==0)
            {
                vec.push_back(0);
            }
            else
            {
                vec.push_back(l.front());
                if(arr[i]==l.front())
                {
                    l.pop_front();
                }
            }
            i++;
            j++;
        }
    }
    for(auto it=vec.begin();it!=vec.end();it++)
    {
        cout<<*it<<" ";
    }


    return 0;
}
Posted by: Guest on June-21-2021

Code answers related to "first negative number in window"

Browse Popular Code Answers by Language