An Array of positive elements. Deepu Wants to reduce the elements of the array.
He calls a function Hit(X) which reduces the all the elements in the array which are greater than X by 1.
he will call this array many times . Print the array after many calls to Hit(X).
Input:
n----- no of elements in array 10^5.
n elements ----- 1<=element<=10^9.
x----- no of calls to Hit(X)
x elements----- 1<=element<=10^9.
output:
Print The array after call to Hit(X) x times.
Time limit–5 secs.
My solution gave Time Limit Exceeded.
My approach:
keep an Original Array
Create a vector of pairs of array elements and their index in the array
Sort the vector elements [ ascending ]
Do LowerBound() of C++ STL to get the position of element in the vector where elements are equal to give element.
From this element decrease the elements which are greater than x by 1 till end in the original array from the index in the pair.
Why are you doing so much,its a simple problem,just traverse the array whenever Hit(X) is called and if greater than X,update the value by decrementing it by 1.The time complexity turns out to be O(noofshots*sizeofarray)
Here’s my code:
Why are you doing so much,its a simple problem,just traverse the array whenever Hit(X) is called and if greater than X,update the value by decrementing it by 1.The time complexity turns out to be O(no_of_shots*size_of_array)
Here’s my code: #include <bits/stdc++.h>
using namespace std; #define ll long long
int main()
{
ll n;
cin>>n;
vector v(n);
for(int i=0;i<n;i++)
cin>>v[i];
ll shot;
cin>>shot;
while(shot–)
{
ll val;
cin>>val;
for(int i=0;i<n;i++)
{
if(v[i]>val)
v[i]–;
}
}
for(int i=0;i<n;i++)
{
cout<<v[i]<<" ";
}
return 0;
}