Play with an array
Given an unsorted array arr of size N, rearrange the elements of array such that number at the odd index is greater than the number at the previous even index. The task is to complete the function formatArray() which will return formatted array.
NOTE:
If the array formed is according to rules print 1, else 0.
Example 1:
Input: n = 5 arr[] = {5, 4, 3, 2, 1} Output: 1 Explanation: The given array after modification will be as such: 4 5 2 3 1.
Example 2:
Input: n = 4 arr[] = {4, 3, 2, 1} Output: ​1
User task:
Since this is a functional problem you don't have to worry about the input, you just have to complete the function formatArray(), which accepts array arr and its size
def formatArray(a,n):
for i in range(n-1):
if i%2==0:
if a[i]>a[i+1]:
a[i],a[i+1]=a[i+1],a[i]
return a
Comments
Post a Comment