array manipulation
largest number
coding techniques
programming challenges
algorithm strategies

How can I manipulate an array to make the largest number?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Manipulating an array to form the largest possible number is a common problem that arises in various computational tasks and coding interviews. The challenge is to reorder the elements of the array such that when concatenated, they form the maximum numerical value possible. This problem requires not only knowledge of sorting algorithms but also a deep understanding of string manipulation and comparison criteria. Let's delve into the intricacies of this problem and examine how it can be solved effectively.

Understanding the Problem

Given an array of non-negative integers, the goal is to arrange them such that they form the largest number possible when concatenated. For example, for the array `[3, 30, 34, 5, 9]`, the largest number that can be formed is `9534330`.

Key Considerations

  1. String Comparison: Direct comparison based on numbers won't work because of varying lengths and the concatenation effect.
  2. Custom Sorting: A custom sorting mechanism is essential to determine the right order.
  3. Edge Cases: Handling leading zeros and arrays with identical numbers are crucial.

Approach

Custom Comparator Function

The primary trick in solving this problem is to determine the correct order of concatenation. A common approach is to define a comparator function based on string concatenation:

  • For any two numbers `x` and `y`, compare the concatenated strings `x+y` and `y+x`.
  • If `x+y` is greater than `y+x`, then `x` should come before `y` in order.

This custom comparator can be integrated into a sorting routine, such as Python's `sorted()` function or Java's `Arrays.sort()` with a comparator.

Algorithm

Here’s a step-by-step outline of how to address the problem:

  1. Convert Integers to Strings: Since the main operation involves string concatenation, first convert all numbers to strings.
  2. Sort with Custom Comparator: Implement and use the comparator function described above.
  3. Concatenate Results: Once sorted, concatenate the array into a single string.
  4. Handle Edge Cases: If the result is of the form '000...0', convert it to '0'.

Example

Consider the array `[30, 3, 34, 5, 9]`. Let's break down the steps:

  • Convert to strings: `["30", "3", "34", "5", "9"]`
  • Sort using the custom comparator:
    • Compare `"3"+"30"` and `"30"+"3"`: Since `330` > `303`, `"3"` precedes `"30"`.
    • Compare `"34"+"3"` and `"3"+"34"`: Since `343` > `334`, `"34"` precedes `"3"`.
    • Following this logic, you arrive at the sorted array `["9", "5", "34", "3", "30"]`.
  • Concatenate: The result is the string "9534330".

Code Example

Here's a Python implementation of the described algorithm:


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.