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

2 Comments

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

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

@na3744
na3744 Feb 23, 2024 at 2:52 AM

I bought this course, it worth it!

@mk.info.work
mk.info.work Nov 15, 2023 at 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

@sciaku1
sciaku1 Jan 11, 2024 at 3: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 About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support