We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
/*
* Complete the 'lilysHomework' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY arr as parameter.
*/
public static int lilysHomework(List<Integer> arr) {
// Write your code here
List<Integer> arr2 = new ArrayList<>(arr); // it is used for decreasing order
List<Integer> sort_arr = new ArrayList<>(arr);
List<Integer> sort_arr_rev = new ArrayList<>(arr);
Collections.sort(sort_arr);
Collections.sort(sort_arr_rev, Collections.reverseOrder());
// System.out.println(sort_arr);
// System.out.println(arr);
int count = 0;
int rev = 0;
for(int i=0; i<arr.size(); i++){ // It is used to count the no. of swap for increasing order
List<Integer> li = findMinMax_Index(arr, arr2, i, arr.size()-1);
// System.out.println(li);
// System.out.println("arr= "+arr);
// System.out.println("arr2= "+arr2);
int min_idx = li.get(0);
if(arr.equals(sort_arr)==false && i != min_idx){
Collections.swap(arr, i, min_idx);
count++;
}
int max_idx = li.get(1); // It is used to count the no. of swap for decreasing order
if(arr2.equals(sort_arr_rev)==false && i != max_idx){
Collections.swap(arr2, max_idx, i);
rev++;
}
if(arr.equals(sort_arr) && rev>count){
break;
}
if(arr2.equals(sort_arr_rev) && count>rev){
break;
}
}
if(count < rev){
return count;
}
else{
return rev;
}
}
public static List<Integer> findMinMax_Index(List<Integer> arr, List<Integer> arr2, int start, int end){
int min = Integer.MAX_VALUE;
int min_idx = 0;
int max = 0;
int max_idx = 0;
for(int i=start; i<=end; i++){
if(arr.get(i) < min){
min = arr.get(i);
min_idx = i;
}
if(arr2.get(i) > max){
max = arr2.get(i);
max_idx = i;
}
}
// System.out.println("arr= "+arr);
// System.out.println("arr2= "+arr2);
// System.out.println("min= "+min_idx+" max= "+max_idx);
List<Integer> li_ans = new ArrayList<>();
li_ans.add(min_idx);
li_ans.add(max_idx);
return li_ans;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
int result = Result.lilysHomework(arr);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Lily's Homework
You are viewing a single comment's thread. Return to all comments →
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 Result {
}
public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
}