Largest Number

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 83 :-  Largest Number

To find the largest number possible by arranging a given array of non-negative integers, you need to perform a custom sort on the numbers. The key to getting the largest number is to compare two numbers as strings rather than as integers. The comparison should consider the concatenation of the numbers in different orders to see which one gives a larger result.

Here's a Python function to find the largest number:

pythonCopy code

def largest_number(nums):    # Convert numbers to strings and sort based on custom comparison    nums = [str(num) for num in nums]    nums.sort(key=lambda x: x * 10, reverse=True)    return str(int(''.join(nums))) # Example usage: numbers = [3, 30, 34, 5, 9] result = largest_number(numbers) print(result)

Output:

arduinoCopy code

"9534330"

In this example, the function largest_number takes an array of non-negative integers nums. It first converts each number to a string using a list comprehension. Then, it sorts the strings using the custom comparison key, which is defined using a lambda function. The lambda function takes each string x and multiplies it by 10 to ensure that the comparison considers different concatenations of the strings. Finally, it joins the sorted strings and converts the result back to an integer and then to a string to handle cases where the largest number is 0.

The function returns the largest number as a string.

Keep in mind that this approach works correctly for non-negative integers. If the array contains negative numbers, you need to handle them differently or clarify the requirements for the problem.

11. Week5 - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?