import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
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 in=new Scanner(System.in);
int t=in.nextInt();
for(int k=0;k<t;k++)
{
String s=in.nextLine();
char a[]=s.toCharArray();
int i=a.length-1;
while(a[i-1]>=a[i]&&i>0)
i--;
if(i>0)
{int w=a.length-1;
int pivot=i-1;
while(a[pivot]>=a[w])
w--;
char temp=a[w];
a[w]=a[pivot];
a[pivot]=temp;
Arrays.sort(a,i,a.length-1);
String sb=new String(a);
System.out.println(sb);
}
else System.out.println("no answer");
}
}
}https://www.hackerrank.com/challenges/bigger-is-greater
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
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 in=new Scanner(System.in);
int t=in.nextInt();
for(int k=0;k<t;k++)
{
String s=in.nextLine();
char a[]=s.toCharArray();
int i=a.length-1;
while(a[i-1]>=a[i]&&i>0)
i--;
if(i>0)
{int w=a.length-1;
int pivot=i-1;
while(a[pivot]>=a[w])
w--;
char temp=a[w];
a[w]=a[pivot];
a[pivot]=temp;
Arrays.sort(a,i,a.length-1);
String sb=new String(a);
System.out.println(sb);
}
else System.out.println("no answer");
}
}
}https://www.hackerrank.com/challenges/bigger-is-greater
I am unable to know why it is coming ArrayIndexOutOfBound:-2.
ReplyDeleteCan anyone help me