Skip to content

Instantly share code, notes, and snippets.

@MukulLatiyan
Last active September 18, 2020 03:11
Show Gist options
  • Save MukulLatiyan/a8998b88ea28eb7cdc8e929280d07007 to your computer and use it in GitHub Desktop.
Save MukulLatiyan/a8998b88ea28eb7cdc8e929280d07007 to your computer and use it in GitHub Desktop.
Check if an array is sorted(GeeksForGeeks)
//Java Code for array checking problem given on school level on geeksforgeeks array school section.
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
Scanner s = new Scanner(System.in);
int ntest = s.nextInt();
while (ntest-- > 0){
int size = s.nextInt();
int[] arr = new int[size];
for (int i = 0; i < size; i++){
arr[i] = s.nextInt();
}
//here using the clone object method,which is basically creating a replica of an array.
int[] ch = arr.clone();
Arrays.sort(arr);
//now comparing them.
System.out.println(Arrays.equals(ch, arr)? 1: 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment