Given an array arr[] of size N. The task is to find maximum element among N – 1 elements other than arr[i] for each i from 1 to N.
Examples:
Input: arr[] = {2, 5, 6, 1, 3}
Output: 6 6 5 6 6
Input: arr[] = {1, 2, 3}
Output: 3 3 2
Approach: An efficient approach is to make prefix and suffix array of maximum elements and find maximum element among N – 1 elements other than arr[i].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int max_element( int a[], int n)
{
int pre[n];
pre[0] = a[0];
for ( int i = 1; i < n; i++)
pre[i] = max(pre[i - 1], a[i]);
int suf[n];
suf[n - 1] = a[n - 1];
for ( int i = n - 2; i >= 0; i--)
suf[i] = max(suf[i + 1], a[i]);
for ( int i = 0; i < n; i++) {
if (i == 0)
cout << suf[i + 1] << " " ;
else if (i == n - 1)
cout << pre[i - 1] << " " ;
else
cout << max(pre[i - 1], suf[i + 1]) << " " ;
}
}
int main()
{
int a[] = { 2, 5, 6, 1, 3 };
int n = sizeof (a) / sizeof (a[0]);
max_element(a, n);
return 0;
}
|
Java
class GFG
{
static void max_element( int a[], int n)
{
int []pre = new int [n];
pre[ 0 ] = a[ 0 ];
for ( int i = 1 ; i < n; i++)
pre[i] = Math.max(pre[i - 1 ], a[i]);
int []suf = new int [n];
suf[n - 1 ] = a[n - 1 ];
for ( int i = n - 2 ; i >= 0 ; i--)
suf[i] = Math.max(suf[i + 1 ], a[i]);
for ( int i = 0 ; i < n; i++)
{
if (i == 0 )
System.out.print(suf[i + 1 ] + " " );
else if (i == n - 1 )
System.out.print(pre[i - 1 ] + " " );
else
System.out.print(Math.max(pre[i - 1 ],
suf[i + 1 ]) + " " );
}
}
public static void main(String []args)
{
int a[] = { 2 , 5 , 6 , 1 , 3 };
int n = a.length;
max_element(a, n);
}
}
|
Python3
def max_element(a, n) :
pre = [ 0 ] * n;
pre[ 0 ] = a[ 0 ];
for i in range ( 1 , n) :
pre[i] = max (pre[i - 1 ], a[i]);
suf = [ 0 ] * n;
suf[n - 1 ] = a[n - 1 ];
for i in range (n - 2 , - 1 , - 1 ) :
suf[i] = max (suf[i + 1 ], a[i]);
for i in range (n) :
if (i = = 0 ) :
print (suf[i + 1 ], end = " " );
elif (i = = n - 1 ) :
print (pre[i - 1 ], end = " " );
else :
print ( max (pre[i - 1 ],
suf[i + 1 ]), end = " " );
if __name__ = = "__main__" :
a = [ 2 , 5 , 6 , 1 , 3 ];
n = len (a);
max_element(a, n);
|
C#
using System;
class GFG
{
static void max_element( int []a, int n)
{
int []pre = new int [n];
pre[0] = a[0];
for ( int i = 1; i < n; i++)
pre[i] = Math.Max(pre[i - 1], a[i]);
int []suf = new int [n];
suf[n - 1] = a[n - 1];
for ( int i = n - 2; i >= 0; i--)
suf[i] = Math.Max(suf[i + 1], a[i]);
for ( int i = 0; i < n; i++)
{
if (i == 0)
Console.Write(suf[i + 1] + " " );
else if (i == n - 1)
Console.Write(pre[i - 1] + " " );
else
Console.Write(Math.Max(pre[i - 1],
suf[i + 1]) + " " );
}
}
public static void Main(String []args)
{
int []a = { 2, 5, 6, 1, 3 };
int n = a.Length;
max_element(a, n);
}
}
|
Javascript
<script>
function max_element(a, n)
{
let pre = new Array(n);
pre[0] = a[0];
for (let i = 1; i < n; i++)
pre[i] = Math.max(pre[i - 1], a[i]);
let suf = new Array(n);
suf[n - 1] = a[n - 1];
for (let i = n - 2; i >= 0; i--)
suf[i] = Math.max(suf[i + 1], a[i]);
for (let i = 0; i < n; i++) {
if (i == 0)
document.write(suf[i + 1] + " " );
else if (i == n - 1)
document.write(pre[i - 1] + " " );
else
document.write(Math.max(pre[i - 1], suf[i + 1]) + " " );
}
}
let a = [2, 5, 6, 1, 3];
let n = a.length;
max_element(a, n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)