Create Array by replacing any element with its immediate smaller neighbour
Last Updated :
28 Nov, 2022
Given an integer array arr[] of size N, the task is to generate a new array by replacing ith element with its immediate right smaller neighbour i.e., by (i+t)th element if it is smaller.
Examples:
Input: N = 5, Arr[] = {4, 2, 1, 5, 3}
Output: 2 1 -1 3 -1
Explanation: Array elements are 4, 2, 1, 5, 3.
Next to 4 is 2 which is smaller, so we print 2. Next of 2 is 1 which is smaller, so we print 1. Next of 1 is 5 which is greater, so we print -1. Next of 5 is 3 which is smaller, so we print 3. Note that for last element, output is always going to be -1 because there is no element on right.
Input: N = 6, Arr[] = {5, 6, 2, 3, 1, 7}
Output: -1 2 -1 1 -1 -1
Approach: To solve the problem follow the below idea:
The idea is to traverse the array and check if the next element is smaller or not. If next element is smaller then replace current element with next smaller element otherwise replace current element with -1.
Follow the steps below to implement the above idea:
- Iterate over the array i = 0 to i < size of array – 1
- Check if the current element is greater than the next element, (i.e, arr[i] > arr[i + 1])
- If true, replace arr[i] = arr[i + 1]
- Otherwise, replace arr[i] = -1
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void immediateSmaller(vector< int >& arr, int n)
{
for ( int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
arr[i] = arr[i + 1];
}
else
arr[i] = -1;
}
arr[n - 1] = -1;
}
int main()
{
vector< int > arr = { 5, 6, 2, 3, 1, 7 };
int N = arr.size();
immediateSmaller(arr, N);
for ( auto i : arr)
cout << i << " " ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static void immediateSmaller( int arr[], int n)
{
for ( int i = 0 ; i < n - 1 ; i++) {
if (arr[i] > arr[i + 1 ]) {
arr[i] = arr[i + 1 ];
}
else
arr[i] = - 1 ;
}
arr[n - 1 ] = - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 5 , 6 , 2 , 3 , 1 , 7 };
int N = arr.length;
immediateSmaller(arr, N);
for ( int i : arr)
System.out.print(i + " " );
}
}
|
Python3
def immediateSmaller(arr, n) :
for i in range (n - 1 ) :
if (arr[i] > arr[i + 1 ]) :
arr[i] = arr[i + 1 ];
else :
arr[i] = - 1 ;
arr[n - 1 ] = - 1 ;
if __name__ = = "__main__" :
arr = [ 5 , 6 , 2 , 3 , 1 , 7 ];
N = len (arr);
immediateSmaller(arr, N);
for i in arr:
print (i, end = " " );
|
C#
using System;
class GFG {
static void immediateSmaller( int [] arr, int n)
{
for ( int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
arr[i] = arr[i + 1];
}
else
arr[i] = -1;
}
arr[n - 1] = -1;
}
public static void Main()
{
int [] arr = { 5, 6, 2, 3, 1, 7 };
int N = arr.Length;
immediateSmaller(arr, N);
for ( int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " " );
}
}
|
Javascript
function immediateSmaller(arr, n)
{
for (let i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
arr[i] = arr[i + 1];
}
else
arr[i] = -1;
}
arr[n - 1] = -1;
return arr;
}
arr = [ 5, 6, 2, 3, 1, 7 ];
N = arr.length;
rslt = immediateSmaller(arr, N);
let string = "" ;
for (let i = 0; i < rslt.length ; i++)
string += rslt[i] + " " ;
console.log(string);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...