Strongest Neighbour

PHOTO EMBED

Tue Feb 08 2022 06:22:48 GMT+0000 (Coordinated Universal Time)

Saved by @Uttam #java #gfg #geeksforgeeks #arrays #practice #strongestneighbour

class StrongestNeighbour{
    
    // Function to find maximum for every adjacent pairs in the array.
    static void maximumAdjacent(int sizeOfArray, int arr[])
    {
        int sum=0;
        for(int i=0; i<sizeOfArray-1; i++)
        {
            sum = Math.max(arr[i], arr[i+1]);  
            System.out.print(sum+" ");
            
        }
    }
}
content_copyCOPY

Strongest Neighbour 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 <= 10^6 1 <= arr[i] <= 10^6

https://practice.geeksforgeeks.org/problems/strongest-neighbour/1/?track=DSASP-Arrays&batchId=190