Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(JAVA) Larry has been given a permutation of a sequence of natural numbers incrementing from as an array. He must determine whether the array can

(JAVA)

Larry has been given a permutation of a sequence of natural numbers incrementing from as an array. He must determine whether the array can be sorted using the following operation any number of times:

  • Choose any consecutive indices and rotate their elements in such a way that .

For example, if :

A [1,6,5,2,4,3] rotate [6,5,2]. A [1,5,2,6,4,3] rotate [5,2,6]. A[1,2,6,5,4,3] rotate[5,4,3]. A [1,2,6,3,5,4] rotate[6,3,5]. A [1,2,3,5,6,4] rotate[5,6,4]. A [1,2,3,4,5,6]

YES

On a new line for each test case, print YES if can be fully sorted. Otherwise, print NO.

Function Description

Complete the larrysArray function in the editor below. It must return a string, either YES or NO.

larrysArray has the following parameter(s):

  • A: an array of integers

Input Format

The first line contains an integer , the number of test cases.

The next pairs of lines are as follows:

  • The first line contains an integer , the length of .
  • The next line contains space-separated integers .

Constraints

  • 1 t 10
  • 3n1000
  • 1A[i]n
  • A sorted = integers that increment by from to

Output Format

For each test case, print YES if can be fully sorted. Otherwise, print NO.

Sample Input

3 3 3 1 2 4 1 3 4 2 5 1 2 3 5 4

Sample Output

YES YES NO

Explanation

In the explanation below, the subscript of denotes the number of operations performed.

Test Case 0: A0 = { 3,1,2} rotate(3,1,2) A1 = {1,2,3}

A is now sorted, so we print YES on a new line.

Test Case 1: .Larry has been given a permutation of a sequence of natural numbers incrementing from as an array. He must determine whether the array can be sorted using the following operation any number of times:

  • Choose any consecutive indices and rotate their elements in such a way that .

For example, if :

A rotate [1,6,5,2,4,3] [6,5,2] [1,5,2,6,4,3] [5,2,6] [1,2,6,5,4,3] [5,4,3] [1,2,6,3,5,4] [6,3,5] [1,2,3,5,6,4] [5,6,4] [1,2,3,4,5,6] YES

On a new line for each test case, print YES if can be fully sorted. Otherwise, print NO.

Function Description

Complete the larrysArray function in the editor below. It must return a string, either YES or NO.

larrysArray has the following parameter(s):

  • A: an array of integers

Input Format

The first line contains an integer , the number of test cases.

The next pairs of lines are as follows:

  • The first line contains an integer , the length of .
  • The next line contains space-separated integers .

Constraints

  • 1t10
  • 3 n 1000
  • 1 A[i]n
  • Asorted = integers that increment by from to

Output Format

For each test case, print YES if can be fully sorted. Otherwise, print NO.

Sample Input

3 3 3 1 2 4 1 3 4 2 5 1 2 3 5 4

Sample Output

YES YES NO

Explanation

In the explanation below, the subscript of denotes the number of operations performed.

Test Case 0: A0 = {3,1,2} rotate(3,1,2) A1 = {1,2,3}

A is now sorted, so we print YES on a new line.

Test Case 1: .A0 = {1,3,4,2} rotate(3,4,2) A1 = {1,4,2,3}. .A0 = {1,4,2,3} rotate(4,2,3) A1 = {1,2,3,4} A is now sorted, so we print YES on a new line.

Test Case 2: No sequence of rotations will result in a sorted . Thus, we print on a new line.

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 {

/*

* Complete the 'larrysArray' function below.

*

* The function is expected to return a STRING.

* The function accepts INTEGER_ARRAY A as parameter.

*/

public static String larrysArray(List A) {

// Write your code here

}

}

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 t = Integer.parseInt(bufferedReader.readLine().trim());

IntStream.range(0, t).forEach(tItr -> {

try {

int n = Integer.parseInt(bufferedReader.readLine().trim());

List A = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))

.map(Integer::parseInt)

.collect(toList());

String result = Result.larrysArray(A);

bufferedWriter.write(result);

bufferedWriter.newLine();

} catch (IOException ex) {

throw new RuntimeException(ex);

}

});

bufferedReader.close();

bufferedWriter.close();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

2nd Edition

0471180742, 978-0471180746

More Books

Students also viewed these Databases questions