problem no 44:(sorting problem):check whether the array is sorted or not.

 QUESTIONS:

 

Given an array arr[] of size N, check if it is sorted in non-decreasing order or not. 

Example 1:

Input:
N = 5
arr[] = {10, 20, 30, 40, 50}
Output: 1
Explanation: The given array is sorted.

Example 2:

Input:
N = 6
arr[] = {90, 80, 100, 70, 40, 30}
Output: 0
Explanation: The given array is not sorted.
 
ANSWER:
 
int flagforsort=1;
for(int i=0;i<n-1;i++)
{
if(arr[i]>arr[i+1])
{
flagforsort=0;
}
}
if(flagforsort==1)
{
return 1;

}
else
{
return 0;
}

}

 

Comments

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.