Replace Every Element With Next Greatest
Problem: Replace Every Element With Next Greatest
Given an array of integers, replace every element with next greatest element.Since there is no element next to the last element, replace it with -1. In an array of a1 to aN, replace ai with max (ai+1 …. aN) where and aN-1 with -1
Solution
Consider the array with only positive integers. Initially set maximum element as -1. Start from the N-1th element, move to the left side one by one, and keep track of the maximum element. Replace every element with the maximum element.
What if our array contains negative integers too?
Modified solution
Initially set maximum element as Integer.MIN_VALUE. Start from the N-1th element, move to the left side one by one, and keep track of the maximum element. Replace every element with the maximum element. And Finally set the last element to -1.
Read full article from Replace Every Element With Next Greatest
No comments:
Post a Comment