Small Triangles, Large Triangles

  • + 56 comments
    void sort_by_area(triangle *tr, int n) {
    // Sort an array a of the length n
        int *p=malloc(n*sizeof(int)); 
    //create array of size n to store "volumes"
        for(int i=0;i<n;i++)
        {
    	float a=(tr[i].a+tr[i].b+tr[i].c)/2.0;
    //use 2.0 compulsary int/int gives int, int/float gives float
           p[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 sort
        for(int i=0;i<n;i++)    
        {
            for(int j=0;j<n-i-1;j++)
            {
                if(p[j]>p[j+1])     
                {
                    int temp=p[j];
                    p[j]=p[j+1];
                    p[j+1]=temp;
    //swapping array of areas in ascending
    //and simuntaneously the structure contents
                    temp=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];

    Don't manipulate anything else

    • + 3 comments

      straight logic. NICE

      • + 2 comments
        [deleted]
        • + 2 comments

          sorry pal. Lifes a mystery, full of secrets and lies. LOL

          • + 1 comment

            Even I am a tryhard ,goodluck :)

            • + 1 comment

              well its CMR, what about you? college or any job?

              • + 1 comment

                Mine is MLR, youre roll number tells me you're 3rd year lol im 2nd :)

                • + 1 comment

                  lol 3 year... mlr full form?

                  • + 1 comment

                    see my profile ;)

                    • + 1 comment

                      lol, but i dont stalk...

                      • + 1 comment

                        Good luck sir!

                        • + 0 comments

                          anyways, happy coding bro:)

          • + 0 comments

            sheeeeeeeeeesh

        • + 0 comments

          here is problem solution in c programming. https://programs.programmingoneonone.com/2021/02/hackerrank-small-triangles-large-triangles-solution-c.html

      • + 0 comments

        solution is here

        https://www.thecscience.com/2021/05/HackerRank-small-triangles-large-triangles-in-c-problem-solution.html

      • + 1 comment

        Much Simpler

        float area(triangle tr) {
            float p = (tr.a+tr.b+tr.c)/2.0;
            return sqrt(p*(p-tr.a)*(p-tr.b)*(p-tr.c));
        }
        void sort_by_area(triangle* tr, int n) {
            triangle temp;
            for (int i=0; i<n; i++) {
                for (int j=i+1; j<n; j++) {
                    if (area(tr[i])>area(tr[j])) {
                        temp = tr[i];
                        tr[i] = tr[j];
                        tr[j] = temp;
                    }
                }
            }
        }
        
        • + 0 comments

          The same code gives error for me. Initially 6 of 11 test cases failed. Just remove sqrt() function and see the miracle :)

    • + 2 comments

      nice ,simple and clear

      • + 0 comments

        Thanks mate

      • + 1 comment

        search for "programs.programmingoneonone.com" for solutions

    • + 1 comment

      That sqrt function was giving wrong values,thanks for pointing it out.

      • + 0 comments

        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.

    • + 1 comment

      Its only working for Test case-0. What about the other ones ?

      • + 1 comment

        It passed all test cases, did you copy this exact code and telling me it is working or not?

        • + 2 comments

          Nope I alloted the array containing the volumes of triangles at compile time, else the rest of the code is same.

          • + 1 comment

            Can I do it with the help of pointers ?

            • + 0 comments

              You can or you can also declare statically

    • + 1 comment

      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

      • + 0 comments

        Yes I read about it on stack overflow I think

    • + 1 comment

      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?

      • + 1 comment

        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

        • + 1 comment

          thanks

          • + 0 comments

            you can always upvote the solution if you like it

    • + 0 comments

      Wonderfully return!!!!!!!

    • + 1 comment

      how the temp is declared ,Can you explain me?

      • + 1 comment

        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

        • + 1 comment

          ya i know that so whats wrong in that...this logic should work

          • + 0 comments

            which logic

    • + 1 comment

      will this code work if we don't swap the areas.

      • + 0 comments

        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

    • + 1 comment

      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???

      • + 0 comments

        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.

    • + 1 comment

      But why did you allocated memory to p dynamically ?

      • + 1 comment

        No problem, you can do it staticly also int p[111];

        Just wanted to know weather I remember ot or not :)

        • + 1 comment

          Yes , I have done it by taking an array only.

          • + 0 comments

            good

    • + 1 comment

      some hidden test aRE fail ,, so cheack it ,,,,,;

      • + 0 comments

        My solution passed all test cases sir

    • + 1 comment

      Ooo...i missed that 2.0 (used 2)....that changed evertything

      • + 0 comments

        any/int gives int and any/float gives float so it is better to use 2.0

    • + 1 comment

      The 2.0 and the square root thing was really helpful.

      • + 0 comments

        Happpy to help!

    • + 1 comment

      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.

      • + 0 comments

        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.

    • + 1 comment

      when i am taking square root ,i am not getting it right . can any on help me?

      • + 0 comments

        In C language the sqrt function is unstable with float values, in genral square root for float values can be a non terminating decimal

    • + 1 comment

      while declaring p you set it as int insted of float. Was that intentional?

      • + 0 comments

        Thanks for pointing it out, no it was not intentional. However, I fixed it now, you can take int also.

    • + 1 comment

      your code is good ... we can also define traingle t; varaiable n swap the entire structure at once instead of individual element;

      • + 0 comments

        Hey thanks, my idea was that beginners understand this so I didn't use that thanks for sharing though.

    • + 1 comment

      beside changing traingle element one by one why don't you make a structure temp variable and use it swap structure in three lines??

      • + 0 comments

        Then it would be somewhat complex for beginners to understand.

    • + 1 comment

      Thanks man that sqrt() was consuming my 6 test cases.......

      • + 0 comments

        haha anytime!

    • + 1 comment

      my code wrong answer for few testcases just coz i diidnt use 2.0 thanks for the suggestion mate

      • + 0 comments

        anytime!

    • + 1 comment

      great logic bro . thanks

      • + 0 comments

        Happy to help :)

    • + 0 comments

      No one can manipulate!

    • + 1 comment

      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

      int a=(tr[i].a+tr[i].b+tr[i].c);
      //This is 16 times the square area of the triangle
      p[i]=(a*(a-2*tr[i].a)*(a-2*tr[i].b)*(a-2*tr[i].c));
      
      • + 0 comments

        I wanted to solve it the way the problem statement wanted us to, your approach is pretty cool tho :)

    • + 0 comments

      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));

      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
              }
          }
      }
      

      }

    • + 1 comment

      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);
      }

      • + 0 comments

        can you tell me whats wrong in this....this one only works for test case 0 and 1

    • + 0 comments

      why do we have to use the bubble sort why cant we use the normal sorting technique

    • + 1 comment

      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;

      • + 0 comments

        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;

        for(i=0;i<n;i++)
        {
            p=(tr[i].a+tr[i].b+tr[i].c)/2;
            s=sqrtf(p*(p-tr[i].a)*(p-tr[i].b)*(p-tr[i].c));
        
            area[i]=s;
        
        
        }
          temp=area[0];
        for(i=0;i<n;i++)
        {  
        
           for(j=i+1;j<n;j++)
           {
               if(area[i]>area[j])
               {
                   temp=area[i];
                   area[i]=area[j];
                   area[j]=temp;
        
                   g=tr[i];
                    tr[i]=tr[j];
                    tr[j]=g;
        
               }
           }
        }
        

        }

    • + 1 comment

      ''formula without sqrt as areas are different guarenteed'' I have not uderstand this.

      • + 0 comments

        You don't need square root as values without square root will also work

    • + 1 comment

      is there any difference between array p being dynamic or static in this program.

      • + 0 comments

        No there is no difference, you can declare it static too.

    • + 1 comment

      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 ;)

      • + 0 comments

        Glad to help

    • + 0 comments

      Thank you, I did not know that sqrt does not work properly with float values.

    • + 1 comment

      Thanks, the sqrt makes my answers wrong.

      • + 0 comments

        Happy to help!

    • + 1 comment

      Thanks a lot for giving that formula without sqrt.

      • + 0 comments

        Happy to help

    • + 1 comment

      thanks for the explanation....so happy

      • + 0 comments

        Happy to help :

    • + 1 comment

      why can't we just swap triangle type objects tr[i] and tr[i+1].?

      • + 0 comments

        we can, my code is a beginner friendly version.

    • + 0 comments
      can you please explain.... Why j < n-i-1 ,, why not j < n-1 only??? It is working on j < n-1 also.
    • + 0 comments

      Thanks for this simple and concise explanation.

    • + 1 comment
      [deleted]
      • + 1 comment

        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...

        • + 1 comment

          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.

          • + 0 comments

            Okkay, I got it. Thanks for the clarification!

    • + 0 comments
          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;                
              }
          }
      }
      
    • + 1 comment

      why did you sort area array?

      • + 1 comment

        Small area = small Triangle Large area = large Triangle

    • + 0 comments

      *p should be float not integer. and the temp should also be float. sqrt works well.

    • + 0 comments

      You can just swap each structure mate, no need to swap each and every field between the 2 structures

    • + 0 comments

      I tried the same logic but its not working for other test cases except 1.

    • + 0 comments

      Thanks, It help me a lot.

    • + 0 comments

      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;

    • + 0 comments

      You dont need to swap element wise. You can simply swap the traiangle by creting a separate temp variable of type struct triangle

    • + 0 comments

      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??

    • + 1 comment

      Why do we have to sort areas also? Initially I was sorting only traingles based on their areas so the output was incorrect.

      • + 0 comments

        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