We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
voidsort_by_area(triangle*tr,intn){// Sort an array a of the length nint*p=malloc(n*sizeof(int));//create array of size n to store "volumes"for(inti=0;i<n;i++){floata=(tr[i].a+tr[i].b+tr[i].c)/2.0;//use 2.0 compulsary int/int gives int, int/float gives floatp[i]=(a*(a-tr[i].a)*(a-tr[i].b)*(a-tr[i].c));//formula without sqrt as areas are different guarenteed //because sqrt dosent work well with float values}//bubble sortfor(inti=0;i<n;i++){for(intj=0;j<n-i-1;j++){if(p[j]>p[j+1]){inttemp=p[j];p[j]=p[j+1];p[j+1]=temp;//swapping array of areas in ascending//and simuntaneously the structure contentstemp=tr[j].a;tr[j].a=tr[j+1].a;tr[j+1].a=temp;temp=tr[j].b;tr[j].b=tr[j+1].b;tr[j+1].b=temp;temp=tr[j].c;tr[j].c=tr[j+1].c;tr[j+1].c=temp;}}}}
Simple straight logic
array p is declared dynamically also can use //int p[1000];
I am appreciative of you pointing out the swap related problem. I hadn't noticed it myself within my test cases. However, the reason it fails is because pairs is not declared as an array of floats. If this is suitably modified, the function runs without any errors.
if your sum is odd you dont get accurate value as 3/2=1 and also 2/2=1 as per interger division which ocurs when you use int. C language integer division floors the actual division value and dosen't consider the floating point/decimal value.
Hope this helps
temp is a local variable it is assigned with the greater value among 2 areas while comparing them element 1 is greater than element 2 it is basically swapping logiv\c tmp=1 a=b b=tmp now a value is in b and b value is in a
Hello anand,
It will NOT work
because the whole comcept of sorting is to swap the position of greater elements before smaller elements if we don't sort first time
as we do not get the greatest element in its perfect place in sorted array if we dont swap elementsthe soting logic does many swaps to get final positions in sorted array
Hey,
Here it dosen't work coz we look up to the p array to keep track of areas and sort the triangle contents
If you dont sort p array,
I think u r assuming that when one swap occurs the element which has swapped takes its final position and is not further swapper, that is wrong there are many swaps taken in order to ensure a fully sorted array.
In C language int/int gives you result in int only
int/float gives you result in float (or) double which is more accurate for comparision, because of it's precision.
Nice one. I just realized sqrt can be avoided. Thanks.
You can also avoid the float altogether by changing the area formula to avoid dividing by 2; the result will be proportional to the area.
The formula will then be
inta=(tr[i].a+tr[i].b+tr[i].c);//This is 16 times the square area of the trianglep[i]=(a*(a-2*tr[i].a)*(a-2*tr[i].b)*(a-2*tr[i].c));
for(int i=0;i<n-1;i++){
for(int j=0;j<(n-1-i);j++){
if(sq(tr,j)>sq(tr,(j+1))){
temp[j]=tr[j];
tr[j]=tr[j+1];
tr[j+1]=temp[j];
}
}
}
}
First two passes for working good. Others did compile successfully, but returning wrongs answers at some points only. Where might be the error occured.
Is sqrt() is returning wrong values?? please do help
}
}
}
yes you are right but i m still not able to solve test case 0 and 1 are getting executed
oid sort_by_area(triangle* tr, int n) {
int i,j,p,s,temp,area[n],t1,t2,t3;
I was trying to get correct output for 2-3 days but that 2 instead of 2.0 held me back. After reading your comment, as I changed it to 2.0, the exact output came. Now I understood the value of precise and accurate commenting. A smallest mistake, like I did, can hold you back getting the perfect results.
Thanks for teaching me that ;)
This is how I did it by sorting array of areas ;)
float area[n];
float s,temp;
int i,j;
for(int i=0;i<n;i++){
s=(tr[i].a+tr[i].b+tr[i].c)/2.00;
area[i]=sqrt(s*(s-tr[i].a)*(s-tr[i].b)*(s-tr[i].c));
// printf("%f",area[i]);
}
for(i=0;i<n;i++){
for(j=0;j<n-1-i;j++){
if(area[j]>area[j+1]){
temp=area[j+1];
area[j+1]=area[j];
area[j]=temp;
//swapping elements of sides
temp=tr[j+1].a;
tr[j+1].a=tr[j].a;
tr[j].a=temp;
temp=tr[j+1].b;
tr[j+1].b=tr[j].b;
tr[j].b=temp;
temp=tr[j+1].c;
tr[j+1].c=tr[j].c;
tr[j].c=temp;
}
}
}
instead of swapping each element of structure, you can swap only respective structure pointers:
triangle temp;
temp1=tr[j];
tr[j]=tr[j+1];
tr[j+1]=temp1;
During the sorting logic why can't we directly sort the length breadth and height without sorting the area array??It's showing test cases failed can you explain please??
if you sort only triangles, then their area will change! old position and new position of traingles will be swapped but areas will not be swapped which leads to incorrect mapping of triangle areas which leads to incorrect output, thats why you seedn to sort them both to keep them together
Small Triangles, Large Triangles
You are viewing a single comment's thread. Return to all comments →
Simple straight logic
array p is declared dynamically also can use //int p[1000];
Don't manipulate anything else
straight logic. NICE
sorry pal. Lifes a mystery, full of secrets and lies. LOL
Even I am a tryhard ,goodluck :)
well its CMR, what about you? college or any job?
Mine is MLR, youre roll number tells me you're 3rd year lol im 2nd :)
lol 3 year... mlr full form?
see my profile ;)
lol, but i dont stalk...
Good luck sir!
anyways, happy coding bro:)
sheeeeeeeeeesh
here is problem solution in c programming. https://programs.programmingoneonone.com/2021/02/hackerrank-small-triangles-large-triangles-solution-c.html
solution is here
https://www.thecscience.com/2021/05/HackerRank-small-triangles-large-triangles-in-c-problem-solution.html
Much Simpler
The same code gives error for me. Initially 6 of 11 test cases failed. Just remove sqrt() function and see the miracle :)
nice ,simple and clear
Thanks mate
search for "programs.programmingoneonone.com" for solutions
That sqrt function was giving wrong values,thanks for pointing it out.
I am appreciative of you pointing out the swap related problem. I hadn't noticed it myself within my test cases. However, the reason it fails is because pairs is not declared as an array of floats. If this is suitably modified, the function runs without any errors.
Its only working for Test case-0. What about the other ones ?
It passed all test cases, did you copy this exact code and telling me it is working or not?
Nope I alloted the array containing the volumes of triangles at compile time, else the rest of the code is same.
Can I do it with the help of pointers ?
You can or you can also declare statically
i had the exactly same logic but i had divided the sum of sides by 2 instead of 2.0 which resulted in only 2 right test cases
Yes I read about it on stack overflow I think
doubt here( float a=(tr[i].a+tr[i].b+tr[i].c)/2.0;) i tried taking a an int and dividing by 2 instead ........and it doesnt work why?
if your sum is odd you dont get accurate value as 3/2=1 and also 2/2=1 as per interger division which ocurs when you use int. C language integer division floors the actual division value and dosen't consider the floating point/decimal value. Hope this helps
thanks
you can always upvote the solution if you like it
Wonderfully return!!!!!!!
how the temp is declared ,Can you explain me?
temp is a local variable it is assigned with the greater value among 2 areas while comparing them element 1 is greater than element 2 it is basically swapping logiv\c tmp=1 a=b b=tmp now a value is in b and b value is in a
ya i know that so whats wrong in that...this logic should work
which logic
will this code work if we don't swap the areas.
Hello anand, It will NOT work because the whole comcept of sorting is to swap the position of greater elements before smaller elements if we don't sort first time as we do not get the greatest element in its perfect place in sorted array if we dont swap elementsthe soting logic does many swaps to get final positions in sorted array
why is it necessary to sort areas int temp=p[j]; p[j]=p[j+1]; p[j+1]=temp; if i remove these 3 lines from your prg it doesnt work...why???
Hey, Here it dosen't work coz we look up to the p array to keep track of areas and sort the triangle contents If you dont sort p array, I think u r assuming that when one swap occurs the element which has swapped takes its final position and is not further swapper, that is wrong there are many swaps taken in order to ensure a fully sorted array.
But why did you allocated memory to p dynamically ?
No problem, you can do it staticly also int p[111];
Just wanted to know weather I remember ot or not :)
Yes , I have done it by taking an array only.
good
some hidden test aRE fail ,, so cheack it ,,,,,;
My solution passed all test cases sir
Ooo...i missed that 2.0 (used 2)....that changed evertything
any/int gives int and any/float gives float so it is better to use 2.0
The
2.0
and the square root thing was really helpful.Happpy to help!
I have a question! y u take float a,why it can't be int ? all the triange sides in structure triangle in int only.
In C language int/int gives you result in int only int/float gives you result in float (or) double which is more accurate for comparision, because of it's precision.
when i am taking square root ,i am not getting it right . can any on help me?
In C language the sqrt function is unstable with float values, in genral square root for float values can be a non terminating decimal
while declaring p you set it as int insted of float. Was that intentional?
Thanks for pointing it out, no it was not intentional. However, I fixed it now, you can take int also.
your code is good ... we can also define traingle t; varaiable n swap the entire structure at once instead of individual element;
Hey thanks, my idea was that beginners understand this so I didn't use that thanks for sharing though.
beside changing traingle element one by one why don't you make a structure temp variable and use it swap structure in three lines??
Then it would be somewhat complex for beginners to understand.
Thanks man that sqrt() was consuming my 6 test cases.......
haha anytime!
my code wrong answer for few testcases just coz i diidnt use 2.0 thanks for the suggestion mate
anytime!
great logic bro . thanks
Happy to help :)
No one can manipulate!
Nice one. I just realized sqrt can be avoided. Thanks. You can also avoid the float altogether by changing the area formula to avoid dividing by 2; the result will be proportional to the area.
The formula will then be
I wanted to solve it the way the problem statement wanted us to, your approach is pretty cool tho :)
double sq(triangle * tr,int j){ float p=(tr[j].a+tr[j].b+tr[j].c)/2; return sqrt(p*(p-tr[j].a)(p-tr[j].b)(p-tr[j].c)); } void sort_by_area(triangle* tr, int n) { triangle *temp=malloc(n * sizeof(triangle));
}
include
include
include
typedef struct rahul { int a; int b; int c;
} rahul; void bhai(rahul *t,int n) { float arr[n]; for(int i=0;i } for(int k=0;k } for(int i=0;iarr[j])
{ char temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; rahul tenp=t[i]; t[i]=t[j]; t[j]=tenp; }
}
} for(int k=0;k }
} int main() { int n; scanf("%d",&n); rahul r; r=(rahul)malloc(n*sizeof(rahul)); for(int i=0;i
} bhai(r,n);
}
can you tell me whats wrong in this....this one only works for test case 0 and 1
why do we have to use the bubble sort why cant we use the normal sorting technique
While sorting Structure you don't need to sort every thing individually,just take 'temp' variable of structure type.
triangle temp; temp = tr[j]; tr[j] = tr[j+1]; tr[j+1] = temp;
yes you are right but i m still not able to solve test case 0 and 1 are getting executed oid sort_by_area(triangle* tr, int n) { int i,j,p,s,temp,area[n],t1,t2,t3;
}
''formula without sqrt as areas are different guarenteed'' I have not uderstand this.
You don't need square root as values without square root will also work
is there any difference between array p being dynamic or static in this program.
No there is no difference, you can declare it static too.
I was trying to get correct output for 2-3 days but that 2 instead of 2.0 held me back. After reading your comment, as I changed it to 2.0, the exact output came. Now I understood the value of precise and accurate commenting. A smallest mistake, like I did, can hold you back getting the perfect results. Thanks for teaching me that ;)
Glad to help
Thank you, I did not know that sqrt does not work properly with float values.
Thanks, the sqrt makes my answers wrong.
Happy to help!
Thanks a lot for giving that formula without sqrt.
Happy to help
thanks for the explanation....so happy
Happy to help :
why can't we just swap triangle type objects tr[i] and tr[i+1].?
we can, my code is a beginner friendly version.
Thanks for this simple and concise explanation.
Why do we have to take semi perimeter a as float? I mean, all the inputs are int type, right? So why will a be a decimal? Please clarify...
For precision, you cannot sort 31.44 and 31.27 if both are taken as its they will be 31 only no difference but actually there is a difference.
Okkay, I got it. Thanks for the clarification!
why did you sort area array?
Small area = small Triangle Large area = large Triangle
*p should be float not integer. and the temp should also be float. sqrt works well.
You can just swap each structure mate, no need to swap each and every field between the 2 structures
I tried the same logic but its not working for other test cases except 1.
Thanks, It help me a lot.
instead of swapping each element of structure, you can swap only respective structure pointers: triangle temp; temp1=tr[j]; tr[j]=tr[j+1]; tr[j+1]=temp1;
You dont need to swap element wise. You can simply swap the traiangle by creting a separate temp variable of type struct triangle
During the sorting logic why can't we directly sort the length breadth and height without sorting the area array??It's showing test cases failed can you explain please??
Why do we have to sort areas also? Initially I was sorting only traingles based on their areas so the output was incorrect.
if you sort only triangles, then their area will change! old position and new position of traingles will be swapped but areas will not be swapped which leads to incorrect mapping of triangle areas which leads to incorrect output, thats why you seedn to sort them both to keep them together