Sort by

recency

|

1000 Discussions

|

  • + 0 comments

    c++ solution, disliking the fact that I spent a bit too long thinking about how to do this, no cause the problem explenation sucks.

    string biggerIsGreater(string w) {
        int n = w.length();
        int i = n - 2;
    
        while (i >= 0 && w[i] >= w[i + 1]) {
            i--;
        }
    
        if (i < 0) return "no answer"; 
    
        int j = n - 1;
        while (w[j] <= w[i]) {
            j--;
        }
    
        swap(w[i], w[j]);
    
        reverse(w.begin() + i + 1, w.end());
    
        return w;
    }
    
  • + 0 comments

    Composite Door Locks deliver strength, style, and advanced security, making them an ideal choice for modern homes. Built to withstand forced entry, they combine multi-point locking systems with durable materials, ensuring long-lasting protection. Easy to operate and designed to fit seamlessly with composite doors, they enhance both safety and appearance. Investing in secure locking solutions reflects the idea that Bigger is Greater, giving homeowners confidence in superior protection and peace of mind.

  • + 0 comments

    import java.io.; import java.util.; import java.util.stream.*;

    class Result {

    public static String biggerIsGreater(String w) {
        char[] chars = w.toCharArray();
        int i = chars.length - 2;
    
        // Step 1: Find the first character that is smaller than its next character from the end
        while (i >= 0 && chars[i] >= chars[i + 1]) {
            i--;
        }
    
        if (i < 0) {
            return "no answer";
        }
    
        // Step 2: Find the smallest character on the right of i that is greater than chars[i]
        int j = chars.length - 1;
        while (chars[j] <= chars[i]) {
            j--;
        }
    
        // Step 3: Swap them
        char temp = chars[i];
        chars[i] = chars[j];
        chars[j] = temp;
    
        // Step 4: Reverse the suffix starting at i + 1
        int left = i + 1, right = chars.length - 1;
        while (left < right) {
            temp = chars[left];
            chars[left] = chars[right];
            chars[right] = temp;
            left++;
            right--;
        }
    
        return new String(chars);
    }
    

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int T = Integer.parseInt(bufferedReader.readLine().trim());
    
        IntStream.range(0, T).forEach(TItr -> {
            try {
                String w = bufferedReader.readLine();
                String result = Result.biggerIsGreater(w);
                bufferedWriter.write(result);
                bufferedWriter.newLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });
    
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    }

  • + 0 comments
    solution in Javascript:
    function biggerIsGreater(w) {
        let s = w.split("");
        let index = -1;
        let n = w.length;
        
        for(let i = n-2; i>=0;i--){
            if(s[i] < s[i+1]){
                index = i;
                break;
            }
        }
        
        if(index === -1){
            return "no answer"
        }else{
            for(let i = n-1; i> index; i--){
                if(s[i] > s[index]){
                    swap(index, i, s);
                    break;
                }
            }
            reverse(index+1, n-1, s)
        }
        
        return s.join("");
        
        function swap(w1, w2, s){
            let temp = s[w1];
            s[w1] = s[w2];
            s[w2] = temp;
        }
    
        function reverse(start, end, s){
            while(start <= end){
                swap(start, end, s);
                start++;
                end--;
            }
        }
    }
    
  • + 0 comments

    `string biggerIsGreater(string w) { int index = 0; int decStart = -1; int swapIndex = -1; while (index < w.length()) { // Increasing if (index + 1 < w.length() && w[index] < w[index + 1]) { decStart = index + 1; swapIndex = index; } // Decreasing, we found a new minimum that is smaller else if (decStart != -1 && w[index] <= w[decStart] && w[index] > w[swapIndex]) { // If the new character is greater than the character to be swapping decStart = index; } index++; }

    if (decStart == -1) {
        return "no answer";
    }
    
    string descending = w.substr(decStart);
    reverse(descending.begin() + 1, descending.end());
    if (swapIndex + 1 < decStart) {
        reverse(w.begin() + swapIndex + 1, w.begin() + decStart);
    }
    w.erase(decStart);
    w.insert(swapIndex, descending);
    return w;`
    

    }