I'm doing this question on hackerrank: https://www.hackerrank.com/challenges/ctci-bubble-sort/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=sorting
I wrote the solution in intellij, and it gives me the correct output there, but when I copied it over to the hackerrank ide, it gave me an error.
This is the code I'm talking about:
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Results {
/*
* Complete the 'countSwaps' function below.
*
* The function accepts INTEGER_ARRAY a as parameter.
*/
public static void countSwaps(List<Integer> a) {
int count = 0;
boolean flag = false;
while (!flag) {
flag = true;
for (int i = 0; i < a.size() - 1; i++) {
if (a.get(i) > a.get(i + 1)) {
int temp = a.get(i);
a.set(i, a.get(i + 1));
a.set(i + 1, temp);
flag = false;
count++;
}
}
}
System.out.println(String.format(
"Array is sorted in %d swaps.%n" +
"First Element: %d%n" +
"Last Element: %d%n",
count,
a.get(0),
a.get(a.size() - 1)));
}
public static class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> a = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
Results.countSwaps(a);
bufferedReader.close();
}
}
}
This is the error: could not find or load main class solution.
Do you have any idea why I'm getting this error here? How could I fix it.
Online Coding contest platforms generally require you to adhere to certain rules for submissions.
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
public class Solution
{
/*
* Complete the 'countSwaps' function below.
*
* The function accepts INTEGER_ARRAY a as parameter.
*/
public static void countSwaps (List < Integer > a)
{
int count = 0;
boolean flag = false;
while (!flag)
{
flag = true;
for (int i = 0; i < a.size () - 1; i++)
{
if (a.get (i) > a.get (i + 1))
{
int temp = a.get (i);
a.set (i, a.get (i + 1));
a.set (i + 1, temp);
flag = false;
count++;
}
}
}
System.out.println (String.format ("Array is sorted in %d swaps.%n" +
"First Element: %d%n" +
"Last Element: %d%n",
count,
a.get (0), a.get (a.size () - 1)));
}
public static void main (String[]args) throws IOException
{
BufferedReader bufferedReader =
new BufferedReader (new InputStreamReader (System.in));
int n = Integer.parseInt (bufferedReader.readLine ().trim ());
List < Integer > a =
Stream.of (bufferedReader.readLine ().replaceAll ("\\s+$",
"").split (" ")).
map (Integer::parseInt).collect (toList ());
countSwaps (a);
bufferedReader.close ();
}
}
The error: could not find or load main class solution.
means your main()
method in the Solution
class could not be accessed.