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.
The other big problem is that you have to print the original positions of the two ice creams you find, and sorting destroys this information.
There are a couple of ways to solve this problem. One way to do it is to make a copy of the original array before sorting it. You can then look through the old array for the prices you found.
The other way is to make an ice_cream class that holds the price and original index of an ice cream. You can then sort the ice creams, find a pair with the required combined price and then you will also know their original indices. To do this you will need to tell the sort function how to compare ice creams, which is done by implementing the Comparable interface and overriding compareTo(). Here's the code you need to do this:
/* most guides just write "implements Comparable",but then you have to write code to handle any kindof Object. The <ice_cream> bit means you are onlyshowing how to compare with other ice creams. */classice_creamimplementsComparable<ice_cream>{publicintprice;publicintoriginal_index;// constructorpublicice_cream(intp,intoi){price=p;original_index=oi;}/* CompareTo should return -1,0 or 1 depending on whether the object is less than, equal or greater than the other object by whatever criteria you have defined@Override generates a compile time error if the method does not override another method. It is not necessary but is good style. */@OverridepublicintcompareTo(ice_creamother){if(this.price<other.price){return-1;}elseif(this.price>other.price){return1;}elsereturn0;}// Lets you print it out@OverridepublicStringtoString(){return"("+price+", "+original_index+") ";}};
Technically, I think it is actually better to make a copy before sorting. You add a couple of O(n) operations by doing this, while using objects instead of plain integers makes the O(nlogn) sorting operation slower. However, I think you learn more with the second approach, so I encourage you to try it out.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Ice Cream Parlor
You are viewing a single comment's thread. Return to all comments →
The other big problem is that you have to print the original positions of the two ice creams you find, and sorting destroys this information.
There are a couple of ways to solve this problem. One way to do it is to make a copy of the original array before sorting it. You can then look through the old array for the prices you found.
The other way is to make an ice_cream class that holds the price and original index of an ice cream. You can then sort the ice creams, find a pair with the required combined price and then you will also know their original indices. To do this you will need to tell the sort function how to compare ice creams, which is done by implementing the Comparable interface and overriding compareTo(). Here's the code you need to do this:
Technically, I think it is actually better to make a copy before sorting. You add a couple of O(n) operations by doing this, while using objects instead of plain integers makes the O(nlogn) sorting operation slower. However, I think you learn more with the second approach, so I encourage you to try it out.