Given an array A[] of size N, the task is to generate an array B[] based on the following conditions:
- For every array element A[i], find the most frequent element greater than A[i] present on the right of A[i]. Insert that element into B[].
- If more than one such element is present on the right, choose the element having the smallest value.
- If no element greater than A[i] is present on the right of A[i], then insert -1 into B[].
Finally, print the array B[] that is obtained.
Examples:
Input: A[] = {4, 5, 2, 25, 10, 5, 10, 3, 10, 5}
Output: 5, 10, 10, -1, -1, 10, -1, 5, -1, -1
Explanation:
A[0] (= 4): Array elements greater than 4 are {5, 25, 10}. Since 5 occurs maximum number of times, set B[0] = 5.
A[1] (= 5): Array elements greater than 5 are {25, 10}. Since 10 occurs maximum number of times, set B[1] = 10.
A[2] (= 2): Array elements greater than 2 are {5, 25, 10}. Since 10 occurs maximum number of times, set B[2] = 10.
A[3] (= 25): No element greater than A[3] found on its right. Therefore, set B[3] = -1.
A[4] (= 10): No element greater than A[4] found on its right. Therefore, set B[4] = -1.
Similarly, set B[5] = 10, B[6] = -1, B[7] = 5, B[8] = -1, B[9] = -1.
Therefore, the obtained array is B[] = {5, 10, 10, -1, -1, 10, -1, 5, -1, -1}.
Input: A[] = {1, 1, 3, 3, 2, 2}
Output: 2, 2, -1, -1, -1, -1
Naive Approach: Follow the steps below to solve the problem:
- Initialize an array, say V, to store the resultant array elements.
- Traverse the array A[] using a variable, say i, and perform the following operations:
- Initialize variables ans as -1 and freq as 0, to store the result for the current index and its frequency.
- Iterate over the range [i+1, N-1] using a variable, say j, and perform the following operations:
- If the frequency of A[j] ≤ A[i], then continue.
- Otherwise, check if the frequency of A[j] > freq. If found to be true, then update ans to A[j] and freq to the frequency of A[j].
- Otherwise, if the frequency of A[j] is equal to freq, then update ans to a smaller value among A[j] and ans.
- Insert the value of ans to the array, V.
- Print the array, V as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findArray( int arr[], int n)
{
vector< int > v;
for ( int i = 0; i < n; i++) {
int ans = -1, old_c = 0;
for ( int j = i + 1; j < n; j++) {
if (arr[j] > arr[i]) {
int curr_c
= count(&arr[j], &arr[n], arr[j]);
if (curr_c == old_c) {
if (arr[j] < ans)
ans = arr[j];
}
if (curr_c > old_c) {
ans = arr[j];
old_c = curr_c;
}
}
}
v.push_back(ans);
}
for ( int i = 0; i < v.size(); i++)
cout << v[i] << " " ;
}
int main()
{
int arr[] = { 4, 5, 2, 25, 10, 5,
10, 3, 10, 5 };
int size = sizeof (arr)
/ sizeof (arr[0]);
findArray(arr, size);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void findArray( int arr[], int n)
{
Vector<Integer> v = new Vector<Integer>();
for ( int i = 0 ; i < n; i++)
{
int ans = - 1 , old_c = 0 ;
for ( int j = i + 1 ; j < n; j++)
{
if (arr[j] > arr[i])
{
int curr_c = 0 ;
for ( int k = j; k < n; k++)
{
if (arr[k] == arr[j])
{
curr_c++;
}
};
if (curr_c == old_c)
{
if (arr[j] < ans)
ans = arr[j];
}
if (curr_c > old_c)
{
ans = arr[j];
old_c = curr_c;
}
}
}
v.add(ans);
}
for ( int i = 0 ; i < v.size(); i++)
System.out.print(v.get(i) + " " );
}
public static void main (String[] args)
{
int arr[] = { 4 , 5 , 2 , 25 , 10 ,
5 , 10 , 3 , 10 , 5 };
int size = arr.length;
findArray(arr, size);
}
}
|
Python3
def findArray(arr, n):
v = []
for i in range (n):
ans = - 1
old_c = 0
for j in range (i + 1 , n):
if (arr[j] > arr[i]):
curr_c = arr[j : n + 1 ].count(arr[j])
if (curr_c = = old_c):
if (arr[j] < ans):
ans = arr[j]
if (curr_c > old_c):
ans = arr[j]
old_c = curr_c
v.append(ans)
for i in range ( len (v)):
print (v[i], end = " " )
if __name__ = = '__main__' :
arr = [ 4 , 5 , 2 , 25 , 10 ,
5 , 10 , 3 , 10 , 5 ]
size = len (arr)
findArray(arr, size)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findArray( int [] arr, int n)
{
List< int > v = new List< int >();
for ( int i = 0; i < n; i++)
{
int ans = -1, old_c = 0;
for ( int j = i + 1; j < n; j++)
{
if (arr[j] > arr[i])
{
int curr_c = 0;
for ( int k = j; k < n; k++)
{
if (arr[k] == arr[j])
{
curr_c++;
}
};
if (curr_c == old_c)
{
if (arr[j] < ans)
ans = arr[j];
}
if (curr_c > old_c)
{
ans = arr[j];
old_c = curr_c;
}
}
}
v.Add(ans);
}
for ( int i = 0; i < v.Count; i++)
Console.Write(v[i] + " " );
}
public static void Main()
{
int [] arr= { 4, 5, 2, 25, 10,
5, 10, 3, 10, 5 };
int size = arr.Length;
findArray(arr, size);
}
}
|
Javascript
<script>
function findArray(arr, n) {
let v = new Array();
for (let i = 0; i < n; i++) {
let ans = -1, old_c = 0;
for (let j = i + 1; j < n; j++) {
if (arr[j] > arr[i]) {
let curr_c = 0;
arr.slice(j, n).forEach((item) => { if (item == arr[j]) { curr_c++ } })
if (curr_c == old_c) {
if (arr[j] < ans)
ans = arr[j];
}
if (curr_c > old_c) {
ans = arr[j];
old_c = curr_c;
}
}
}
v.push(ans);
}
for (let i = 0; i < v.length; i++)
document.write(v[i] + " " );
}
let arr = [4, 5, 2, 25, 10, 5,
10, 3, 10, 5];
let size = arr.length
findArray(arr, size);
</script>
|
Output:
5 10 10 -1 -1 10 -1 5 -1 -1
Time Complexity: O(N2), as we are using nested loops for traversing N*N times.
Auxiliary Space: O(N), as we are using extra space for storing generated array