int isSorted(int n, vector<int> arr) {
// Write your code here.
for(int i=1;i<n;i++)
{
if(arr[i]>=arr[i-1])
{
}
else
{
return 0;
}
}
return 1;
}
Problem of the day
You have been given an array ‘a’ of ‘n’ non-negative integers.You have to check whether the given array is sorted in the non-decreasing order or not.
Your task is to return 1 if the given array is sorted. Else, return 0.
Input: ‘n’ = 5, ‘a’ = [1, 2, 3, 4, 5]
Output: 1
The given array is sorted in non-decreasing order; hence the answer will be 1.
The first line will contain the integer ‘n’, the number of elements in the array ‘a’, and the next line will contain the ‘n’ spaced integers in the array elements.
Output Format:
Print 1 if the given array is sorted. Else, print 0.
Output for each test case will be printed on a separate line.
Note:
You are not required to print anything; it has already been taken care of. Just implement the function.
4
0 0 0 1
1
The given array is sorted in non-decreasing order; hence the answer will be 1.
5
4 5 4 4 4
0
O(n), Where ‘n’ is the size of an input array ‘a’.
1 ≤ 'n' ≤ 5*10^6
0 ≤ 'a'[i] ≤ 10^9
Time limit: 1 sec
Can you think of comparing the adjacent elements here?
Approach:
We will simply iterate over the array and if for every element (except the end) the element after is less than the current element then we will return 0 immediately. After exiting from the loop we will return 1.
Algorithm:
O(n), Where ‘n’ is the size of the array ‘a’.
We are simply iterating over the array ‘a’.
Hence, the time complexity is O(n).
O(1).
We are using constant extra space.
Hence, the space complexity is O(1).
Interview problems
Simple C++ Solution
int isSorted(int n, vector<int> arr) {
// Write your code here.
for(int i=1;i<n;i++)
{
if(arr[i]>=arr[i-1])
{
}
else
{
return 0;
}
}
return 1;
}
Interview problems
solution for c++ bruteforce approch
int isSorted(int n, vector<int> a) {
for(int i=0;i<n-1;i++)
{
if(a[i+1]<a[i])
{
return 0;
}
}
return 1;
}
Interview problems
JAVA || Easy Code || O(n)
public class Solution {
public static int isSorted(int n, int []a) {
// Write your code here.
for(int i=0;i<n-1;i++){
if(a[i]>a[i+1]){
return 0;
}
}
return 1;
}
}
Interview problems
JAVA
public class Solution {
public static int checkSorted(int currentIndex, int[] arr, int n){
if(currentIndex == 0){
return checkSorted(currentIndex+1, arr, n);
}
if(arr[currentIndex] < arr[currentIndex - 1]){
return 0;
}
if(currentIndex == n){
return 1;
}
return checkSorted(currentIndex+1, arr, n);
}
public static int isSorted(int n, int []a) {
// Write your code here.
return checkSorted(0, a, n-1);
}
}
Interview problems
Java Solution
public class Solution {
public static int isSorted(int n, int []a) {
int max = a[n-1];
for(int i=n-2;i>=0;i--){
if(a[i]>max){
return 0;
}
}
return 1;
}
}
Interview problems
SORTED ARRAY OR NOT (JAVA OPTIMAL SOLLUTION)
public class Solution {
public static int isSorted(int n, int []a) {
// Write your code here.
for(int i=1; i<n; i++){
if (a[i] >= a[i-1]) {
} else {
return 0;
}
}
return 1;
}
}
Interview problems
JAVA SOLUTION
public class Solution {
public static int isSorted(int n, int []a) {
// Write your code here.
for(int i =1 ;i< n ;i++){
if(a[i] >= a[i-1]){
}else{
return 0;
}
}
return 1;
}
}
Interview problems
Check the array is sorted or not
//Asked in tcs company
//HOW TO CHECK WHETHER UR ARRAY IS SORTED?
int isSorted(int n, vector<int> a) {
// Write your code here.
for (int i = 0; i < n-1; i++) {
if (a[i] >a[i+1]) {
return 0;
}
}
return 1;
}
//eg: 1,2,8,15
//1>2
//2>8
//8>15
Interview problems
c++ approch
int isSorted(int n, vector<int> a) {
// Write your code here.
for(int i=0;i<n-1;i++)
{
if(a[i+1]<a[i])
{
return 0;
}
}
return 1;
}
//Follow me on Linkedin :www.linkedin.com/in/gschandrasekhar
Interview problems
C++ Solution
int isSorted(int n, vector<int> a) {
// Write your code here.
for (int i = 1; i < n; i++) {
if(a[i-1]<=a[i] && a[i+1]>=a[i]){
return 1;
}
else{
return 0;
}
}
}