ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 202 ms):
#Question :
#Link to the question: 

"""Author : Nishant Kumar"""

def solve():
    s= input().strip()
    digit = [int(ch) for ch in s]
    n = len(digit)
    for i in range(n):
        copy_i = i 
        while copy_i >0 and digit[copy_i]>0 and digit[copy_i]-1>digit[copy_i-1]:
            val = digit[copy_i]
            digit[copy_i]=digit[copy_i-1]
            digit[copy_i-1]=val-1
            if copy_i>0:
                copy_i-=1
            else:
                break
    print("".join(map(str,digit)))



for _ in range(int(input())):
    solve()

"""Author : Nishant Kumar"""

Solution 2 (runtime: 718 ms):
t=int(input())
for _ in range(t):
    s=input()
    l=list(map(int,s))
    for i in range(1,len(s)):
        while i>0 and l[i]>l[i-1]+1:
            l[i],l[i-1]=l[i-1],l[i]-1
            i-=1
    print(''.join(map(str,l)))
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 202 ms):
#Question :
#Link to the question: 

"""Author : Nishant Kumar"""

def solve():
    s= input().strip()
    digit = [int(ch) for ch in s]
    n = len(digit)
    for i in range(n):
        copy_i = i 
        while copy_i >0 and digit[copy_i]>0 and digit[copy_i]-1>digit[copy_i-1]:
            val = digit[copy_i]
            digit[copy_i]=digit[copy_i-1]
            digit[copy_i-1]=val-1
            if copy_i>0:
                copy_i-=1
            else:
                break
    print("".join(map(str,digit)))



for _ in range(int(input())):
    solve()

"""Author : Nishant Kumar"""

Solution 2 (runtime: 1202 ms):
t = int(input())



for _ in range(t):

    word = input()

    s = [int(digit) for digit in word]

    l = len(s)



    for i in range(l):

        findMax= s[i]

        maxPos = i

        for j in range(10):

            if s[i+j]>findMax+((j+i)-maxPos):

                findMax = s[i+j]

                maxPos = i+j

            

            if l-(i+j)-1==0:

                break



        for j in range(maxPos-i):

            temp = s[maxPos-1]

            s[maxPos-1] = s[maxPos]-1

            s[maxPos] = temp

            maxPos-=1



    print("".join([str(digit) for digit in s]))
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 202 ms):
#Question :
#Link to the question: 

"""Author : Nishant Kumar"""

def solve():
    s= input().strip()
    digit = [int(ch) for ch in s]
    n = len(digit)
    for i in range(n):
        copy_i = i 
        while copy_i >0 and digit[copy_i]>0 and digit[copy_i]-1>digit[copy_i-1]:
            val = digit[copy_i]
            digit[copy_i]=digit[copy_i-1]
            digit[copy_i-1]=val-1
            if copy_i>0:
                copy_i-=1
            else:
                break
    print("".join(map(str,digit)))



for _ in range(int(input())):
    solve()

"""Author : Nishant Kumar"""

Solution 2 (runtime: 203 ms):
t= int(input())
for k in range(t):    
    a=list(input())
    b= len(a)
    for i in range(b):
        t= min(i+9,b)
        m= int(a[i])
        n=i
        for j in range(i,t):
            if (int(a[j])-j+i)> m:
                m= int(a[j])-j+i
                n= j
        while n>i:
            a[n],a[n-1]= a[n-1],str(int(a[n])-1)
            n-=1
    print("".join(a))
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 202 ms):
#Question :
#Link to the question: 

"""Author : Nishant Kumar"""

def solve():
    s= input().strip()
    digit = [int(ch) for ch in s]
    n = len(digit)
    for i in range(n):
        copy_i = i 
        while copy_i >0 and digit[copy_i]>0 and digit[copy_i]-1>digit[copy_i-1]:
            val = digit[copy_i]
            digit[copy_i]=digit[copy_i-1]
            digit[copy_i-1]=val-1
            if copy_i>0:
                copy_i-=1
            else:
                break
    print("".join(map(str,digit)))



for _ in range(int(input())):
    solve()

"""Author : Nishant Kumar"""

Solution 2 (runtime: 702 ms):
n = int(input())

res = []

for _ in range(n):

    s = input()

    temp = []

    for ch in s:

        temp.append(int(ch))

    for i in range(1, len(s)):

        j = i - 1

        while j >= 0:

            r = temp[j+1]

            l = temp[j]

            if r > l + 1:

                temp[j] = r - 1

                temp[j+1] = l

            else:

                break

            j -= 1

    r = ''

    for n in temp:

        r += str(n)

    res.append(r)



for val in res:

    print(val)
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 202 ms):
#Question :
#Link to the question: 

"""Author : Nishant Kumar"""

def solve():
    s= input().strip()
    digit = [int(ch) for ch in s]
    n = len(digit)
    for i in range(n):
        copy_i = i 
        while copy_i >0 and digit[copy_i]>0 and digit[copy_i]-1>digit[copy_i-1]:
            val = digit[copy_i]
            digit[copy_i]=digit[copy_i-1]
            digit[copy_i-1]=val-1
            if copy_i>0:
                copy_i-=1
            else:
                break
    print("".join(map(str,digit)))



for _ in range(int(input())):
    solve()

"""Author : Nishant Kumar"""

Solution 2 (runtime: 218 ms):
for _ in range(int(input())):

    s=list(input())

    #print(s)

    for i in range(len(s)):

        val=str(s[i])

        index=i



        for j in range(i+1,min(i+10,len(s))):

            if (int(s[j])-(j-i))>int(val):

                val=str(int(s[j])-(j-i))

                index=j 

        

        if index!=i:

            prev=s[i]

            for j in range(i+1,index+1):

                x=s[j]

                s[j]=prev

                prev=x

                

            s[i]=val

    print("".join(s))
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 202 ms):
#Question :
#Link to the question: 

"""Author : Nishant Kumar"""

def solve():
    s= input().strip()
    digit = [int(ch) for ch in s]
    n = len(digit)
    for i in range(n):
        copy_i = i 
        while copy_i >0 and digit[copy_i]>0 and digit[copy_i]-1>digit[copy_i-1]:
            val = digit[copy_i]
            digit[copy_i]=digit[copy_i-1]
            digit[copy_i-1]=val-1
            if copy_i>0:
                copy_i-=1
            else:
                break
    print("".join(map(str,digit)))



for _ in range(int(input())):
    solve()

"""Author : Nishant Kumar"""

Solution 2 (runtime: 624 ms):
def solve():

    s = list(input().strip())  # Convert string to list for mutability

    n = len(s)



    for i in range(1, n):

        j, curr = i - 1, i

        while j >= 0 and s[curr] > '0' and s[curr] > chr(ord(s[j]) + 1):

            s[curr] = chr(ord(s[curr]) - 1)  # Decrease digit

            s[curr], s[j] = s[j], s[curr]  # Swap

            curr -= 1

            j = curr - 1



    print("".join(s))



# Handling multiple test cases

t = int(input())

for _ in range(t):

    solve()
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 202 ms):
#Question :
#Link to the question: 

"""Author : Nishant Kumar"""

def solve():
    s= input().strip()
    digit = [int(ch) for ch in s]
    n = len(digit)
    for i in range(n):
        copy_i = i 
        while copy_i >0 and digit[copy_i]>0 and digit[copy_i]-1>digit[copy_i-1]:
            val = digit[copy_i]
            digit[copy_i]=digit[copy_i-1]
            digit[copy_i-1]=val-1
            if copy_i>0:
                copy_i-=1
            else:
                break
    print("".join(map(str,digit)))



for _ in range(int(input())):
    solve()

"""Author : Nishant Kumar"""

Solution 2 (runtime: 234 ms):
n = int(input())

for x in range(n):

s= list(map(int, input()))

g = len(s)

for i in range(1,g):

for j in range(0, s[i]):

if(i-j-1 < 0):

break

if(s[i-j]-1 > s[i-j-1]):

temp = s[i-j-1]

s[i-j-1] = s[i-j]-1

s[i-j] = temp

else:

break

result = ''.join(map(str,s))

print(result)
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 202 ms):
#Question :
#Link to the question: 

"""Author : Nishant Kumar"""

def solve():
    s= input().strip()
    digit = [int(ch) for ch in s]
    n = len(digit)
    for i in range(n):
        copy_i = i 
        while copy_i >0 and digit[copy_i]>0 and digit[copy_i]-1>digit[copy_i-1]:
            val = digit[copy_i]
            digit[copy_i]=digit[copy_i-1]
            digit[copy_i-1]=val-1
            if copy_i>0:
                copy_i-=1
            else:
                break
    print("".join(map(str,digit)))



for _ in range(int(input())):
    solve()

"""Author : Nishant Kumar"""

Solution 2 (runtime: 156 ms):
for _ in range(int(input())):

a = input()

s = [int(digit) for digit in a]

n = len(s)

for i in range(n):

best_value = s[i]

best_idx = -1

for j in range(i + 1, min(i + 9, n)):

score = s[j]-(j - i)

if score>best_value:

best_value=score

best_idx=j



if best_idx != -1:

# print(best_value,s[best_idx],i,best_idx)

s[i], s[best_idx] = best_value, s[i]

for j in range(best_idx, i+1, -1):

s[j], s[j - 1] = s[j - 1], s[j]

# print(s)



print("".join(map(str, s)))
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 202 ms):
#Question :
#Link to the question: 

"""Author : Nishant Kumar"""

def solve():
    s= input().strip()
    digit = [int(ch) for ch in s]
    n = len(digit)
    for i in range(n):
        copy_i = i 
        while copy_i >0 and digit[copy_i]>0 and digit[copy_i]-1>digit[copy_i-1]:
            val = digit[copy_i]
            digit[copy_i]=digit[copy_i-1]
            digit[copy_i-1]=val-1
            if copy_i>0:
                copy_i-=1
            else:
                break
    print("".join(map(str,digit)))



for _ in range(int(input())):
    solve()

"""Author : Nishant Kumar"""

Solution 2 (runtime: 171 ms):
mod = 1e9 + 7

for _ in range(int(input())):

s = list(map(int,list(input())))

for i in range(len(s)-1):

mxi = -1

mx = 0

for j in range(1,10):

if i + j >= len(s):

break

if int(s[i+j]) - j - int(s[i]) > mx:

mx = int(s[i+j]) - j - int(s[i])

mxi = j

if mxi == -1:

continue



for jj in range(i+mxi,i,-1):

s[jj],s[jj-1] = s[jj-1],s[jj] - 1



print(''.join(list(map(str,s))))
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 202 ms):
#Question :
#Link to the question: 

"""Author : Nishant Kumar"""

def solve():
    s= input().strip()
    digit = [int(ch) for ch in s]
    n = len(digit)
    for i in range(n):
        copy_i = i 
        while copy_i >0 and digit[copy_i]>0 and digit[copy_i]-1>digit[copy_i-1]:
            val = digit[copy_i]
            digit[copy_i]=digit[copy_i-1]
            digit[copy_i-1]=val-1
            if copy_i>0:
                copy_i-=1
            else:
                break
    print("".join(map(str,digit)))



for _ in range(int(input())):
    solve()

"""Author : Nishant Kumar"""

Solution 2 (runtime: 218 ms):
t =  int(input())

def solve():

s = [int(i) for i in list(input())]

n = len(s)

for i in range(1,n,1):

for j in range(max(i-10, 0), i):

if s[i] - (i-j) > s[j]:

tmp = s[j:i]

s[j] = s[i] - (i-j)

s[j+1:i+1] = tmp

break

print("".join([str(i) for i in s]))

return

for i in range(t):

solve()
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 202 ms):
#Question :
#Link to the question: 

"""Author : Nishant Kumar"""

def solve():
    s= input().strip()
    digit = [int(ch) for ch in s]
    n = len(digit)
    for i in range(n):
        copy_i = i 
        while copy_i >0 and digit[copy_i]>0 and digit[copy_i]-1>digit[copy_i-1]:
            val = digit[copy_i]
            digit[copy_i]=digit[copy_i-1]
            digit[copy_i-1]=val-1
            if copy_i>0:
                copy_i-=1
            else:
                break
    print("".join(map(str,digit)))



for _ in range(int(input())):
    solve()

"""Author : Nishant Kumar"""

Solution 2 (runtime: 906 ms):
def solve():

s = list(input())

n = len(s)

while True:

changed = False

for i in range(1, n):

if s[i] != '0' and int(s[i]) - 1 > int(s[i-1]):

num = int(s[i])

s[i] = str(int(s[i-1]))

s[i-1] = str(num - 1)

changed = True

if not changed:

break

print("".join(s))



t = int(input())

for _ in range(t):

solve()
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 202 ms):
#Question :
#Link to the question: 

"""Author : Nishant Kumar"""

def solve():
    s= input().strip()
    digit = [int(ch) for ch in s]
    n = len(digit)
    for i in range(n):
        copy_i = i 
        while copy_i >0 and digit[copy_i]>0 and digit[copy_i]-1>digit[copy_i-1]:
            val = digit[copy_i]
            digit[copy_i]=digit[copy_i-1]
            digit[copy_i-1]=val-1
            if copy_i>0:
                copy_i-=1
            else:
                break
    print("".join(map(str,digit)))



for _ in range(int(input())):
    solve()

"""Author : Nishant Kumar"""

Solution 2 (runtime: 265 ms):
for _ in range(int(input())):

s = list(map(int,input()))

for i in range(9):

for j in range(len(s)-1):

if s[j] < s[j+1] - 1:

s[j],s[j+1] = s[j+1]-1,s[j]

print(*s,sep= '')
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 717 ms):
# cook your dish here
testcases = int(input())
for testcase in range(testcases):
    s = str(input())
    digits = []
    for d in s:
        digits.append(int(d))
    for i in range(len(s)):
        curr = digits[i]
        idx = -1
        maxx = curr
        limit = min(len(s), i + 9 - curr)
        for j in range(i + 1, limit):
            if maxx < digits[j] - (j - i):
                idx = j 
                maxx = digits[j] - (j - i)
        if idx != -1:
            for k in range(idx, i, -1):
                digits[k] = digits[k - 1]
        digits[i] = maxx
    ans = [str(d) for d in digits]
    print("".join(ans))

Solution 2 (runtime: 1108 ms):
def solve():
    number = list(input())
    for i in range(len(number)):
        count = 0
        for i in range(0,len(number)-1):
            if(int(number[i])+1<int(number[i+1])):
                count +=1
                temp = number[i]
                number[i]=str(int(number[i+1])-1)
                number[i+1] = temp
        if count==0:
            return ''.join(number)
        

t = int(input())
for _ in range(t):
    print(solve())
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 717 ms):
# cook your dish here
testcases = int(input())
for testcase in range(testcases):
    s = str(input())
    digits = []
    for d in s:
        digits.append(int(d))
    for i in range(len(s)):
        curr = digits[i]
        idx = -1
        maxx = curr
        limit = min(len(s), i + 9 - curr)
        for j in range(i + 1, limit):
            if maxx < digits[j] - (j - i):
                idx = j 
                maxx = digits[j] - (j - i)
        if idx != -1:
            for k in range(idx, i, -1):
                digits[k] = digits[k - 1]
        digits[i] = maxx
    ans = [str(d) for d in digits]
    print("".join(ans))

Solution 2 (runtime: 1077 ms):
import sys

data = sys.stdin.read().strip().splitlines()
t = int(data[0])
results = []

for case in range(1, t + 1):
    s = list(data[case])  # Convert string to a list for mutability
    n = len(s)
    
    for i in range(n):
        b = int(s[i])
        p = i
        
        for j in range(i, min(i + 10, n)):
            if int(s[j]) - (j - i) > b:
                b = int(s[j]) - (j - i)
                p = j
        
        while p > i:
            s[p], s[p - 1] = s[p - 1], s[p]  # Swap elements in the list
            p -= 1
        
        s[i] = str(b)  # Update the current position with the best value
    
    results.append(''.join(s))  # Join the list back into a string

for result in results:
    print(result)
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 717 ms):
# cook your dish here
testcases = int(input())
for testcase in range(testcases):
    s = str(input())
    digits = []
    for d in s:
        digits.append(int(d))
    for i in range(len(s)):
        curr = digits[i]
        idx = -1
        maxx = curr
        limit = min(len(s), i + 9 - curr)
        for j in range(i + 1, limit):
            if maxx < digits[j] - (j - i):
                idx = j 
                maxx = digits[j] - (j - i)
        if idx != -1:
            for k in range(idx, i, -1):
                digits[k] = digits[k - 1]
        digits[i] = maxx
    ans = [str(d) for d in digits]
    print("".join(ans))

Solution 2 (runtime: 281 ms):
def f(n):
    # for i in range(1,len(n)):
    i = 0
    while i < (len(n) - 1):
        i += 1
        #print(n, i)
        while i > 0 and n[i] != '0' and int(str(int(n[i]) - 1) + n[i-1]) > int(n[i-1] + n[i]):
            old = n[i - 1]
            n[i-1] = str(int(n[i]) - 1)
            n[i] = old
            i -= 1
    return ''.join(n)

for i in range(int(input())):
    print(f(list(input())))
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 717 ms):
# cook your dish here
testcases = int(input())
for testcase in range(testcases):
    s = str(input())
    digits = []
    for d in s:
        digits.append(int(d))
    for i in range(len(s)):
        curr = digits[i]
        idx = -1
        maxx = curr
        limit = min(len(s), i + 9 - curr)
        for j in range(i + 1, limit):
            if maxx < digits[j] - (j - i):
                idx = j 
                maxx = digits[j] - (j - i)
        if idx != -1:
            for k in range(idx, i, -1):
                digits[k] = digits[k - 1]
        digits[i] = maxx
    ans = [str(d) for d in digits]
    print("".join(ans))

Solution 2 (runtime: 155 ms):
from sys import stdin
from typing import Any
import math
input = stdin.readline
def ri(): return int(input())
def rl(v_type=int,output=list): return output(map(v_type, input().split()))
def rs(): return input().rstrip()
def out(x, sep='\n'): print(sep.join(x))


def solve():
    s = rs()
    n = len(s)
    a = list(map(int,s))
    for i in range(1,n):
        for j in range(i -1,max(-1,i - 11),-1):
            if a[j] + 1 < a[j + 1]:
                a[j],a[j+1] = a[j+1] - 1, a[j]
            else:
                break 

        
    return "".join(map(str,a))



output = []
for _ in range(ri()):
    output.append(str(solve()))
out(output)
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 717 ms):
# cook your dish here
testcases = int(input())
for testcase in range(testcases):
    s = str(input())
    digits = []
    for d in s:
        digits.append(int(d))
    for i in range(len(s)):
        curr = digits[i]
        idx = -1
        maxx = curr
        limit = min(len(s), i + 9 - curr)
        for j in range(i + 1, limit):
            if maxx < digits[j] - (j - i):
                idx = j 
                maxx = digits[j] - (j - i)
        if idx != -1:
            for k in range(idx, i, -1):
                digits[k] = digits[k - 1]
        digits[i] = maxx
    ans = [str(d) for d in digits]
    print("".join(ans))

Solution 2 (runtime: 171 ms):
def solve():
    s = input()
    s = list(s)  # Преобразуем строку в список символов для удобства работы
    for i in range(len(s)):
        best = int(s[i])
        pos = i
        for j in range(i, min(i + 10, len(s))):
            current_score = int(s[j]) - (j - i)
            if current_score > best:
                best = current_score
                pos = j
        while pos > i:
            s[pos], s[pos - 1] = s[pos - 1], s[pos]
            pos -= 1
        s[i] = str(best)
    print("".join(s))

if __name__ == "__main__":
    tests = int(input())
    for _ in range(tests):
        solve()
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 717 ms):
# cook your dish here
testcases = int(input())
for testcase in range(testcases):
    s = str(input())
    digits = []
    for d in s:
        digits.append(int(d))
    for i in range(len(s)):
        curr = digits[i]
        idx = -1
        maxx = curr
        limit = min(len(s), i + 9 - curr)
        for j in range(i + 1, limit):
            if maxx < digits[j] - (j - i):
                idx = j 
                maxx = digits[j] - (j - i)
        if idx != -1:
            for k in range(idx, i, -1):
                digits[k] = digits[k - 1]
        digits[i] = maxx
    ans = [str(d) for d in digits]
    print("".join(ans))

Solution 2 (runtime: 218 ms):
t = int(input())
for _ in range(t):
    num = list(input())  # Convert string to list for mutable operations
    n = len(num)
    for i in range(n - 1):
        j = i + 1
        while j < n and (int(num[j]) - 1 > int(num[j - 1])):
            temp = str(int(num[j]) - 1)
            num[j] = num[j - 1]
            num[j - 1] = temp
            j -= 1  # Move backward to recheck the order
            if j <= 0:  # Prevent going out of bounds
                break
    print(''.join(num))  # Convert list back to string for output
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 717 ms):
# cook your dish here
testcases = int(input())
for testcase in range(testcases):
    s = str(input())
    digits = []
    for d in s:
        digits.append(int(d))
    for i in range(len(s)):
        curr = digits[i]
        idx = -1
        maxx = curr
        limit = min(len(s), i + 9 - curr)
        for j in range(i + 1, limit):
            if maxx < digits[j] - (j - i):
                idx = j 
                maxx = digits[j] - (j - i)
        if idx != -1:
            for k in range(idx, i, -1):
                digits[k] = digits[k - 1]
        digits[i] = maxx
    ans = [str(d) for d in digits]
    print("".join(ans))

Solution 2 (runtime: 187 ms):
t = int(input())
for _ in range(t):
    s = input().strip()
    l = [int(c) for c in s]
    i = 1
    while i < len(l):
        if l[i] > l[i - 1]+1 and l[i] != 0:
            l[i] -= 1
            l[i - 1], l[i] = l[i], l[i - 1]
            if i > 1:
                i -= 1
        else:
            i += 1
    print("".join(map(str, l)))
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 717 ms):
# cook your dish here
testcases = int(input())
for testcase in range(testcases):
    s = str(input())
    digits = []
    for d in s:
        digits.append(int(d))
    for i in range(len(s)):
        curr = digits[i]
        idx = -1
        maxx = curr
        limit = min(len(s), i + 9 - curr)
        for j in range(i + 1, limit):
            if maxx < digits[j] - (j - i):
                idx = j 
                maxx = digits[j] - (j - i)
        if idx != -1:
            for k in range(idx, i, -1):
                digits[k] = digits[k - 1]
        digits[i] = maxx
    ans = [str(d) for d in digits]
    print("".join(ans))

Solution 2 (runtime: 358 ms):
n = int(input())
for _ in range(n):
    n1 = list(input())  # Convert input to a list for mutability
    length = len(n1)

    for i in range(length):
        best = int(n1[i])
        pos = i

        # Optimize the inner loop to minimize recalculations
        for j in range(i, min(i + 9, length)):
            value = int(n1[j]) - (j - i)
            if value > best:
                best = value
                pos = j

        # Perform swaps to bring the best element to the correct position
        while pos > i:
            n1[pos], n1[pos - 1] = n1[pos - 1], n1[pos]
            pos -= 1

        n1[i] = str(best)

    print("".join(n1))