6. Strongest Neighbour 
Basic Accuracy: 50.02% Submissions: 15515 Points: 1

Given an array arr[] of n positive integers. The task is to find the maximum for every adjacent pairs in the array.

Example 1:

Input:
n = 6
arr[] = {1,2,2,3,4,5}
Output: 2 2 3 4 5
Explanation: Maximum of arr[0] and arr[1]
is 2, that of arr[1] and arr[2] is 2, ...
and so on. For last two elements, maximum 
is 5.

Example 2:

Input:
n = 2
arr[] = {5, 5}
Output: 5
Explanation: We only have two elements 
so max of 5 and 5 is 5 only.

Your Task:
The task is to complete the function maximumAdjacent(), which takes sizeOfArray (n) and array(arr) as parameters and prints the maximum of all adjacent pairs (space separated).

Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).

Constraints:
2 <= n <= 106
1 <= arr[i] <= 106


void strongest(int *arr,int n){

    if(n%2==0){

    for(int i=0;i<n-1;i++){

        cout<<max(arr[i],arr[i+1])<<" ";

    }

    }

    else{

        for(int i=0;i<n;i++){

        cout<<max(arr[i],arr[i+1])<<" ";

    }

        

    }

}

int main() {

    int cap=5;

    int x;

    //cin>>x;

    int index;

    //cin>>index;

    int arr[50]={1,2,2,3,4,5};

    //del(arr,9,43);

    int arr2[3]={5,5};

    strongest(arr2,2);

    

    

    

    


    return 0;

}

Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings