> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/string/949.-largest-time-for-given-digits.md).

# 949. Largest Time for Given Digits

![](/files/UOhuK0P7GTpEE92nc6xK)

![](/files/wtfXTQN2vHPOxVduilWF)

still need this , when \[2,0, 6, 6], the first one cant get right ans, should consider another case start from 1

```
        res = res || 
            findMaxAndRearrange(0, 1, arr) && 
            findMaxAndRearrange(1, 9, arr) && 
            findMaxAndRearrange(2, 5, arr) && 
            findMaxAndRearrange(3, 9, arr);
```

![](/files/CZqj3UeUUF7Z35RPjfES)

time: O(8\*4) = O(32) = O(1), worst time

space: O(1)

```java
class Solution {
    public String largestTimeFromDigits(int[] arr) {
        boolean res = 
            findMaxAndRearrange(0, 2, arr) &&
            (arr[0] == 2 ? findMaxAndRearrange(1, 3, arr) : findMaxAndRearrange(1, 9, arr)) && 
            findMaxAndRearrange(2, 5, arr) && 
            findMaxAndRearrange(3, 9, arr);
        
        res = res || 
            findMaxAndRearrange(0, 1, arr) && 
            findMaxAndRearrange(1, 9, arr) && 
            findMaxAndRearrange(2, 5, arr) && 
            findMaxAndRearrange(3, 9, arr);
        
        if (!res) {
            return "";
        }
        
        StringBuilder sb = new StringBuilder();
        return sb.append(arr[0])
            .append(arr[1])
            .append(":")
            .append(arr[2])
            .append(arr[3]).toString();
    }
    
    private boolean findMaxAndRearrange(int index, int upper, int[] arr) {
        int maxIndex = -1;
        
        for (int i = index; i < arr.length; i++) {
            if (arr[i] <= upper && (maxIndex == -1 || arr[maxIndex] < arr[i])) {
                maxIndex = i;
            }
        }
        if (maxIndex == -1) {
            return false;
        }
        int temp = arr[maxIndex];
        arr[maxIndex] = arr[index];
        arr[index] = temp;
        return true;
    }
}

/*
[1,2,3,4]


rule 

23 59

??:??

__:__

19:59 1-case

23:59 2-case

if 2 in array
use in first, then 1
then if no 0, return ""
*/
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/string/949.-largest-time-for-given-digits.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
