All test cases are coming wrong but the one given is right can you have a look at my code please
Can you please look in to my code showing all test cases wrong
Hey @bhavik911
Try for this input
1
2
98 9
correct output:
998
For example, 98 is greater than 9, but in output 9 comes before 98.
The idea is to use any comparison based sorting algorithm. In the used sorting algorithm, instead of using the default comparison, write a comparison function myCompare() and use it to sort numbers. Given two numbers X and Y, how should myCompare() decide which number to put first – we compare two numbers XY (Y appended at the end of X) and YX (X appended at the end of Y). If XY is larger, then X should come before Y in output, else Y should come before. For example, let X and Y be 98 and 9. To compare X and Y, we compare 989 and 998. Since 998is greater than 989, we put Y first.
Hey I have done exactly that now still not working
It is showing run error and one test case is failing
debug your code
for this input
1
2
98 9
correct output:
998
your code gives
989
package Arrays;
import java.util.Scanner;
public class kk {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
int size = scanner.nextInt();
int[] arr = new int[size];
boolean flag = false;
for (int j = 0; j < size; j++) {
arr[j] = scanner.nextInt();
}
print(arr);
}
}
public static int combine(int n1, int n2) {
String s1 = Integer.toString(n1);
String s2 = Integer.toString(n2);
String s3 = s1 + s2;
return Integer.parseInt(s3);
}
public static boolean compare(int n1, int n2) {
int i = 0;
int xy = combine(n1,n2);
int yx = combine(n2,n1);
if(xy>yx){
return true;
}else {
return false;
}
}
public static void sort(int[] arr) {
int n = arr.length;
int k = 1;
while (k <= n - 1) {
for (int i = 0; i<arr.length-k; i++) {
if (!compare(arr[i], arr[i+1])) {
swap(arr,i,i+1);
}
}
k++;
}
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void print(int[] arr) {
sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
}
}
This is my updated code it gives 998 only
Code Mast h
Just a change
public static long combine(int n1, int n2) {
String s1 = Integer.toString(n1);
String s2 = Integer.toString(n2);
String s3 = s1 + s2;
return Long.parseLong(s3);
}
let n1 and n1 both range of 10^5, then
s3 > 10^9 out of int range
public static boolean compare(int n1, int n2) {
int i = 0;
long xy = combine(n1, n2);
long yx = combine(n2, n1);
if (xy > yx) {
return true;
} else {
return false;
}
}
and add new line after each and every TestCase
Correct code :
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
int size = scanner.nextInt();
int[] arr = new int[size];
boolean flag = false;
for (int j = 0; j < size; j++) {
arr[j] = scanner.nextInt();
}
print(arr);
}
}
public static long combine(int n1, int n2) {
String s1 = Integer.toString(n1);
String s2 = Integer.toString(n2);
String s3 = s1 + s2;
return Long.parseLong(s3);
}
public static boolean compare(int n1, int n2) {
int i = 0;
long xy = combine(n1, n2);
long yx = combine(n2, n1);
if (xy > yx) {
return true;
} else {
return false;
}
}
public static void sort(int[] arr) {
int n = arr.length;
int k = 1;
while (k <= n - 1) {
for (int i = 0; i < arr.length - k; i++) {
if (!compare(arr[i], arr[i + 1])) {
swap(arr, i, i + 1);
}
}
k++;
}
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void print(int[] arr) {
sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
System.out.println();
}
}
Thanks , got it now , totally forgot to take long and print new line
