Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerRank Bigger is Greater problem solution

YASH PAL, 31 July 2024

In this HackerRank Bigger is a Greater problem you have Given a word, create a new word by swapping some or all of its characters. This new word must be greater than the original word and also It must be the smallest word that meets the first condition.

HackerRank Bigger is Greater problem solution

Topics we are covering

Toggle
  • Problem solution in Python programming.
  • Problem solution in Java Programming.
    • Problem solution in C++ programming.
    • Problem solution in C programming.
    • Problem solution in JavaScript programming.

Problem solution in Python programming.

for _ in range(int(input())):
    s = input()
    s = list(s[::-1])
    done = 0
    for i in range(1,len(s)):
        if s[i-1] > s[i]:
            for j in range(i):
                if s[j] > s[i]:
                    s[j],s[i] = s[i],s[j]
                    s = sorted(s[:i])[::-1] + s[i:]
                    print("".join(s[::-1]))
                    break
            break
    else:
        print("no answer")

Problem solution in Java Programming.

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

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        sc.nextLine();
        if(t>=1&&t<=100000){
            for(int i=0;i<t;i++){
                String str = sc.nextLine();
                System.out.println(process(str));
            }
        }
    }
    
    private static String process(String str){
        char[] chars = str.toCharArray();
        int i= chars.length-1;
        while(i>0){
            if(chars[i-1]>=chars[i]){
                i--;
            }else{
                int j=i;
                while(j<chars.length&&chars[j]>chars[i-1]){
                    j++;
                }
                char temp = chars[i-1];
                chars[i-1]=chars[j-1];
                chars[j-1]=temp;
                
                char[] newChar = new char[chars.length];
                for(int k=0;k<i;k++){
                    newChar[k]=chars[k];
                }
                int end = chars.length-1;
                for(int k=i;k<chars.length;k++){
                    newChar[k]=chars[end--];
                }
                return new String(newChar);
            }
        }
        return "no answer";
    }
    
}

Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

 string s;

int main() {
    int tc;
    scanf("%d", &tc);
    while (tc--) {
        cin >> s;
        if (next_permutation(s.begin(), s.end())) printf("%sn", s.c_str());
        else printf("no answern");
    }
    return 0;
}

Problem solution in C programming.

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>

void quick_sort (char *a, int n) {
    if (n < 2)
        return;
    char p = a[n / 2];
    char *l = a;
    char *r = a + n - 1;
    while (l <= r) {
        if (*l < p) {
            l++;
        }
        else if (*r > p) {
            r--;
        }
        else {
            char t = *l;
            *l = *r;
            *r = t;
            l++;
            r--;
        }
    }
    quick_sort(a, r - a + 1);
    quick_sort(l, a + n - l);
}

int main(int argc, char *argv[])
{
    int T;
    scanf("%u", &T);
    for (int t = 0; t < T; t++)
    {
        char s[101];
        scanf("%s", s);
        int sl = strlen(s);
        for (int i = sl - 2; i >= 0; i--)
        {
            for (int j = sl - 1; j > i; j--)
            {
                if (s[i] < s[j])
                {
                    char c = s[i];
                    s[i] = s[j];
                    s[j] = c;
                    quick_sort(s + i + 1, sl - i - 1);
                    goto found;
                }
            }
        }
        strcpy(s, "no answer");
found:
        printf("%sn", s);
    }
    return 0;
}

Problem solution in JavaScript programming.

function processData(input) {
    
    
    var lines = input.split('n');
    solutions = '';
    
    for (var i=1; i<lines.length; i++) {
        var w = lines[i];
        var numChars = w.length;
        if (numChars < 2) {
            solutions += 'no answern';
            continue;
        }
        
        // two characters or more:
        var wordBranchPos = -1;
        for (var n=w.length; n>1; n--) {
            var pair = w.substring(n-2,n);
            var pairAr = pair.split('');
            var pairArSorted = pairAr.sort();
            var pairSorted = pairArSorted.join('');
            if (pairSorted == pair) {
                var pairArReversed = pairArSorted.reverse();
                var pairReversed = pairArReversed.join('');
                if (pair == pairReversed) {
                    continue;
                }
                // The letter at n-2 is lower than n-1
                //var tail = w.substring(n-2);
                //w = w.substring(0,n-2) + pairReversed + w.substring(n);
                wordBranchPos = n;
                break;
            }
        }
        
        if (wordBranchPos != -1) {
            var n = wordBranchPos;
            var wStem = w.substring(0,n-2);
            var wBranch = w.substring(n-2);
            var wBranchAr = wBranch.split('');
            var wBranchArSorted = wBranchAr.sort();
            
            var breakChar = w.substring(n-2,n-1);
            var breakCharPos = wBranchArSorted.lastIndexOf(breakChar);
            var nextChar = wBranchArSorted[breakCharPos+1];
            wBranchArSorted.splice(breakCharPos+1,1);
            var newBranch = wBranchArSorted.join('');
            
            w = wStem + nextChar + newBranch;
            solutions += w + 'n';
        } else {
            solutions += 'no answern';
        }
        
    }
    
    console.log(solutions);
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes