Hackearth's Data Structures solutions( Long ATM Queue, Polygon Possibility, Micro and Array Update ) :
Problem 7: Long ATM Queue
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n,i,ct=1;
cin>>n;
vector<long long>v(n);
for(i=0;i<n;i++)
{
cin>>v[i];
}
for(i=0;i<n-1;i++)
{
if(v[i]>v[i+1])
{
ct++;
}
}
cout<<ct;
}
This code is simple. There's no need for any explanation.
Problem 8: Polygon Possibility
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long t,n,i,sum,maxi,s; //s=side length
cin>>t; //n=no. of sides, t=test cases
while(t--)
{
sum=0;
maxi=0;
cin>>n;
for(i=0;i<n;i++)
{
cin>>s;
sum+=s; //sum=sum of all sides
if(s>maxi)
{
maxi=s; //therefore, maxi=maximum side
}
}
if(maxi<(sum-maxi)) //see note below
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}
}
If you google , you will find out for a polygon to be valid , the length of any given side must be less than the sum of the remaining sides. But you don't have to check all the sides, just check the maximum side and compare it with the sum of remaining sides and answer accordingly.
Problem 9: Micro and Array Update
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long t,n,k,i,diff;
cin>>t;
while(t--)
{
cin>>n>>k;
vector<long long>a(n,0),t(n,0); //two vectors v and t
for(i=0;i<n;i++) //initializing with zeroes
{
cin>>a[i];
if(a[i]<k)
{
diff=k-a[i];
t[i]=diff;
diff=0;
}
else
{
t[i]=0;
}
}
cout<<*max_element(t.begin(),t.end())<<endl; //max element of vector t
}
}
This code is simple. There's no need for any explanation.
Guys , if you have any queries related to a question or need more explanation for a question , comment below!
Comments
Post a Comment