Find the next non-zero Array element to the right of each array element
Last Updated :
26 Oct, 2021
Given an array arr[] of N integers, the task is to find the next non-zero array element to the right of every array element. If there doesn’t exist any non-zero element, then print that element itself.
Examples:
Input: arr[] = {1, 2, 0}
Output: {2, 2, 0}
Explanation:
For each array element the next non-zero elements are:
arr[0] = 1 -> 2
arr[1] = 2 -> 2(as there doesn’t exist any non-zero element)
arr[2] = 0 -> 0(as there doesn’t exist any non-zero element)
Input: arr[] = {1, 0, 0, 3}
Output: {3, 3, 3, 3}
Approach: The simple approach to this problem is to traverse the given array from the end, keep track of the variable for each non-zero element obtained while traversing, and replace the integer in the array with that variable. Follow the below steps to solve the given problem:
- Create a variable, say tempValid, to keep track of the valid integer and initialize it to -1.
- Create an array result[] that stores the next non-zero array element to the right of every array element.
- Iterate the array from the end from index N – 1 to 0 and if the value of tempValid is -1, then assign the next non-zero element for the current index as the number itself i.e., arr[i]. Otherwise, update the value of result[i] as tempValid.
- After completing the above steps, print the array result[] as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void NextValidInteger( int arr[], int N)
{
int result[N];
int tempValid = -1;
for ( int i = N - 1; i >= 0; i--) {
if (tempValid == -1) {
result[i] = arr[i];
}
else {
result[i] = tempValid;
}
if (arr[i] != 0) {
tempValid = arr[i];
}
}
for ( int i = 0; i < N; i++) {
cout << result[i] << " " ;
}
}
int main()
{
int arr[] = { 1, 2, 0, 2, 4, 5, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
NextValidInteger(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static void NextValidInteger( int arr[], int N)
{
int result[] = new int [N];
int tempValid = - 1 ;
for ( int i = N - 1 ; i >= 0 ; i--) {
if (tempValid == - 1 ) {
result[i] = arr[i];
}
else {
result[i] = tempValid;
}
if (arr[i] != 0 ) {
tempValid = arr[i];
}
}
for ( int i = 0 ; i < N; i++) {
System.out.print(result[i] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 0 , 2 , 4 , 5 , 0 };
int N = 7 ;
NextValidInteger(arr, N);
}
}
|
Python3
def NextValidInteger(arr, N):
result = [ 0 for _ in range (N)]
tempValid = - 1
for i in range (N - 1 , - 1 , - 1 ):
if (tempValid = = - 1 ):
result[i] = arr[i]
else :
result[i] = tempValid
if (arr[i] ! = 0 ):
tempValid = arr[i]
for i in range ( 0 , N):
print (result[i], end = " " )
if __name__ = = "__main__" :
arr = [ 1 , 2 , 0 , 2 , 4 , 5 , 0 ]
N = len (arr)
NextValidInteger(arr, N)
|
C#
using System;
class GFG
{
static void NextValidInteger( int [] arr, int N)
{
int [] result = new int [N];
int tempValid = -1;
for ( int i = N - 1; i >= 0; i--) {
if (tempValid == -1) {
result[i] = arr[i];
}
else {
result[i] = tempValid;
}
if (arr[i] != 0) {
tempValid = arr[i];
}
}
for ( int i = 0; i < N; i++) {
Console.Write(result[i] + " " );
}
}
public static void Main()
{
int [] arr = { 1, 2, 0, 2, 4, 5, 0 };
int N = 7;
NextValidInteger(arr, N);
}
}
|
Javascript
<script>
function NextValidInteger(arr, N) {
let result = new Array(N);
let tempValid = -1;
for (let i = N - 1; i >= 0; i--) {
if (tempValid == -1) {
result[i] = arr[i];
}
else {
result[i] = tempValid;
}
if (arr[i] != 0) {
tempValid = arr[i];
}
}
for (let i = 0; i < N; i++) {
document.write(result[i] + " " );
}
}
let arr = [1, 2, 0, 2, 4, 5, 0];
let N = arr.length;
NextValidInteger(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...