-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
42 lines (32 loc) · 990 Bytes
/
Main.java
File metadata and controls
42 lines (32 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Scanner;
class Solution {
public boolean check(int[] nums) {
int n = nums.length;
for (int i = 0; i < n - 1; i++) {
if (nums[i] > nums[i + 1]) {
return false; // Not sorted
}
}
return true; // All pairs were sorted
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
int[] nums = new int[n];
System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
Solution sol = new Solution();
boolean ans = sol.check(nums);
if (ans) {
System.out.println("The array is sorted in non-decreasing order.");
} else {
System.out.println("The array is NOT sorted.");
}
sc.close();
}
}