tutorialcup@gmail.com

Replace every element with the greatest on the right side in an array

Arrays
Reverse Array
Pair Sum Equals Number
Floor and Ceil
Largest Sum Subarray
Missing Number
Odd Occuring Number
Sort by Frequency
First and Second Smallest
Majority Element
Difference between elements
Arrange Even and Odd
Closest Sum Elements
Distinct Elements
First Repeating Number
Common Elements in Arrays
Smallest Missing Number
Max Adjacent Sum
Find Occurrences of Number
Rotate Image
Distance Between Numbers
Pair with given difference
Product Array Puzzle
Replace Elements
First Repeating Element
Move Zeroes to End
Pythagorean Triplets
Maximum Average Subarray
Subarray Sum Equal to X
Leaders in Array
Largest Pair Sum
Segregate 0s 1s and 2s
Find Duplicates
Consecutive Elements Check
Triplet with given Sum
Binary Search in Sorted Array
Fixed point in sorted array
Reorder Elements by Indexes
Merging two sorted arrays
Find next greater number
Reorder array using given indexes
Count of triplets with sum less than given value
Merge two sorted arrays
Subarray and Subsequence
Rearrange array maximum minimum form
Find the lost element from a duplicated array
Count minimum steps to get the given array
Maximum element in an array which is increasing and then decreasing
Minimum number of jumps to reach the end of an array
Subarray with given sum
Length of Longest Increasing Subsequence
Smallest positive number missing unsorted array
The Celebrity Problem
Sorted subsequence of size 3
Partition Problem
Find pair with given difference
Maximum length of chain pairs
Four elements that sum to given
Maximum circular subarray sum
Count possible triangles
Longest Increasing Subsequence
Petrol Bunks Tour
Tug of War
Counting Sort
Maximum Repeating Number
Positive Negative Arrangement
Find a Peak element
Elements More Than n/k
Max Product Subsequence
Longest Bitonic subarray in an array
Number of smaller elements on right side
Implement two stacks in an array
Maximum sum increasing subsequence
Find the two numbers with odd occurrences in an unsorted array
Largest subarray with equal number of 0's and 1's
Maximum product subarray
Replace every element with the greatest on the right side in an array
Sorting a k sorted array
Find the row with maximum number of 1's
Shuffle a given array
Iterative Implementation of quick sort
Arrange given numbers to form the biggest number
Pancake sorting
Pancake sorting Problem
Maximum Subarray Sum using Divide and Conquer
Merge Overlapping Intervals
Stock Buy Sell to Maximize Profit
Sort Elements by frequency
Print all possible combinations of r elements in a given array of size n
Monotonically increasing function
Minimum element in a sorted and rotated array
Merge k Sorted Arrays
Flip Zeroes for Consecutive 1's
Least Average Subarray
Longest span with same sum in two binary arrays
Form minimum number from given sequence of D's and I's
Number of strictly increasing subarrays
Minimum difference between any two elements in an array
Number of pairs with given sum
Make Array Palindrome
Dynamic Programming, Longest Bitonic SubSequence

Given an array, this function will replace every element in the array with the greatest element in the right side in an array. Since we have no element next to the last element we have to replace it with -1.

Here greatest element is the element that is greatest of all the elements on the right side of the current element

Lets take an example

INPUT:
arr[] = {3, 9, 6, 8, 5}

OUTPUT:
Output array is {9, 8, 8, 5, -1}

In the above example, the greatest element on the right side of 3 is 9, on the right side of 9 is 8 and so on.

Time Complexity: O()

Algorithm

1. simply run two loops

2. In the outer loop, pick an each element in the array ie, i = 0 to n, where n is the size

3. In the inner loop, search for the greatest element after the picked element. ie, j= i+1 to n

4. Replace the picked element with the greatest element and make the last element to -1.

C++ Program

#include <bits/stdc++.h>
using namespace std;

int greatestElement(int arr[], int n)
{
	int greatest;
	for (int i = 0; i < n; ++i)
	{
		greatest = 0;
		for (int j = i +1 ; j < n; ++j)
		{
			if (arr[j]> greatest) //if element is greater than greatest
			{
				greatest = arr[j]; //replace greatest
			}	
		}
		arr[i] = greatest; //replace the element with the greatest of all elements on right
	}
	arr[n-1] = -1;
	cout<<"Output array is "<<"{"; 
	for (int i = 0; i < n; ++i)
	{
		cout<<arr[i]<<" ";
	}
	cout<<"}";
}


int main()
{
	
	int arr[]= {3, 9, 6, 8, 5};  //creating an array
    int n = sizeof(arr)/sizeof(arr[0]); //size of the array
    greatestElement(arr, n);
    return 0;
}
Try It

Time Complexity: O(n)

Algorithm

1. Traverse the array from end to start

2. Initialize greatest element to the last element.

3. While moving left one by one, update the greatest element and replace every element with the greatest element.

C++ Program

#include <bits/stdc++.h>
using namespace std;

int greatestElement(int arr[], int n)
{
	int greatest = arr[n-1]; //initialize greatest to last element
	for (int i = n-2; i >= 0; --i)
	{
		int temp = arr[i]; //check eack element i greater than greatest or not
		arr[i] = greatest; //replace element with greatest.
		if (temp>greatest)
		{
			greatest = temp; //update greatest 
		}
	}
	arr[n-1] = -1;
	cout<<"Output array is "<<"{"; 
	for (int i = 0; i < n; ++i)
	{
		cout<<arr[i]<<" ";
	}
	cout<<"}";
}


int main()
{
	
	int arr[]= {3, 9, 6, 8, 5};  //creating an array
    int n = sizeof(arr)/sizeof(arr[0]); //size of the array
    greatestElement(arr, n);
    return 0;
}
Try It


C++ Online Test - Check Your Rank across the World


Next > < Prev
Scroll to Top