then please explain ur code wrt to problem how it works. mathematically
here is another question in which i got 91 marks. HELP!
https://www.hackerrank.com/contests/procode-final-round-avengers-theme/challenges/tony-stark-and-perfect-equipment
We had to find all numbers of form (a,b) which were having same ratio as (x,y).
so first we reduced (x,y) to the simplest ratio by dividing them by their gcd.
Now, simply we pick the min of w/x and h/y.
even I was wondering what’s the difference between the two codes
@ssrivastava990
Hi, This codes passes all cases
#include<bits/stdc++.h>
using namespace std;
signed main()
{
int n,o=0,e=0,c=0,k=0;
cin>>n;
int a[n+1];
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(i%2)
o+=a[i];
else
e+=a[i];
}
int abo=0,abe=0,aao,aae,es,os;
for(int i=1;i<=n;i++)
{
aao=e-abe-(i%2?0:a[i]);
aae=o-abo-(i%2?a[i]:0);
es=abe+aae;
os=abo+aao;
//cout<<i<<" "<<es<<" "<<os<<endl;
if(os==es)
c++;
if(i%2)
abo+=a[i];
else
abe+=a[i];
}
cout<<c<<endl;
return 0;
}
Explain Logic plz
I calculated the following things :
- sum of odd positions before i’th position
- sum of even positions before i’th position
- sum of odd positions after i’th position
- sum of even positions before i’th position
Now, calculate total odd sum and total even sum (excluding i’th position) using above and compare both.
width=int(input("width: "))
height=int(input("height: "))
x=int(input(“x:”))
y=int(input(‘y:’))
no_of_shields=0
ratio=x/y
for width in range(1,width+1):
for height in range(1,height+1):
if width/height == ratio:
no_of_shields+=1
print(f’no of shields is { no_of_shields }’)
This code will give TLE.