Computer scienceProgramming languagesJavaInterview preparationAlgorithms and problem solving techniques

Understand the problem

Beautiful array

Report a typo

For some fixed n, an array nums is beautiful if it is a permutation of the integers 1, 2, ..., n, such that for every i < j, there is no k with i < k < j such that nums[k] * 2 = nums[i] + nums[j]. Given n, return any beautiful array nums. (It is guaranteed that one exists.)

Sample Input 1:

4

Sample Output 1:

[1, 3, 2, 4]
Write a program in Java 17
import java.util.Arrays;
import java.util.Scanner;

class BeautifulArray{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();

//write your code here

}
}
___

Create a free account to access the full topic