查找每个数组元素右侧的下一个非零数组元素
给定一个包含N个整数的数组arr[] ,任务是找到每个数组元素右侧的下一个非零数组元素。如果不存在任何非零元素,则打印该元素本身。
例子:
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}
方法:解决这个问题的简单方法是从末尾遍历给定数组,跟踪遍历时获得的每个非零元素的变量,并用该变量替换数组中的整数。请按照以下步骤解决给定的问题:
- 创建一个变量,例如tempValid ,以跟踪有效整数并将其初始化为-1 。
- 创建一个数组result[] ,它将下一个非零数组元素存储在每个数组元素的右侧。
- 从索引N – 1到0迭代数组,如果tempValid的值为-1 ,则将当前索引的下一个非零元素分配为数字本身,即arr[i] 。否则,将result[i]的值更新为tempValid 。
- 完成上述步骤后,打印数组result[]作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the next non-zero
// element to the right of each array
// elements
void NextValidInteger(int arr[], int N)
{
// Stores the resultant array
int result[N];
// Keeps the track of next non-zero
// element for each array element
int tempValid = -1;
// Iterate the array from right to left
// and update tempValid
for (int i = N - 1; i >= 0; i--) {
// If tempValid is -1, the valid
// number at current index is the
// number itself
if (tempValid == -1) {
result[i] = arr[i];
}
else {
result[i] = tempValid;
}
// Update tempValid if the
// current element is non-zero
if (arr[i] != 0) {
tempValid = arr[i];
}
}
// Print the result
for (int i = 0; i < N; i++) {
cout << result[i] << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 0, 2, 4, 5, 0 };
int N = sizeof(arr) / sizeof(arr[0]);
NextValidInteger(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to find the next non-zero
// element to the right of each array
// elements
static void NextValidInteger(int arr[], int N)
{
// Stores the resultant array
int result[] = new int[N];
// Keeps the track of next non-zero
// element for each array element
int tempValid = -1;
// Iterate the array from right to left
// and update tempValid
for (int i = N - 1; i >= 0; i--) {
// If tempValid is -1, the valid
// number at current index is the
// number itself
if (tempValid == -1) {
result[i] = arr[i];
}
else {
result[i] = tempValid;
}
// Update tempValid if the
// current element is non-zero
if (arr[i] != 0) {
tempValid = arr[i];
}
}
// Print the result
for (int i = 0; i < N; i++) {
System.out.print(result[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 2, 0, 2, 4, 5, 0 };
int N = 7;
NextValidInteger(arr, N);
}
}
// This code is contributed by dwivediyash
Python3
# python program for the above approach
# Function to find the next non-zero
# element to the right of each array
# elements
def NextValidInteger(arr, N):
# Stores the resultant array
result = [0 for _ in range(N)]
# Keeps the track of next non-zero
# element for each array element
tempValid = -1
# Iterate the array from right to left
# and update tempValid
for i in range(N-1, -1, -1):
# If tempValid is -1, the valid
# number at current index is the
# number itself
if (tempValid == -1):
result[i] = arr[i]
else:
result[i] = tempValid
# Update tempValid if the
# current element is non-zero
if (arr[i] != 0):
tempValid = arr[i]
# Print the result
for i in range(0, N):
print(result[i], end=" ")
# Driver Code
if __name__ == "__main__":
arr = [1, 2, 0, 2, 4, 5, 0]
N = len(arr)
NextValidInteger(arr, N)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the next non-zero
// element to the right of each array
// elements
static void NextValidInteger(int[] arr, int N)
{
// Stores the resultant array
int[] result = new int[N];
// Keeps the track of next non-zero
// element for each array element
int tempValid = -1;
// Iterate the array from right to left
// and update tempValid
for (int i = N - 1; i >= 0; i--) {
// If tempValid is -1, the valid
// number at current index is the
// number itself
if (tempValid == -1) {
result[i] = arr[i];
}
else {
result[i] = tempValid;
}
// Update tempValid if the
// current element is non-zero
if (arr[i] != 0) {
tempValid = arr[i];
}
}
// Print the result
for (int i = 0; i < N; i++) {
Console.Write(result[i] + " ");
}
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 2, 0, 2, 4, 5, 0 };
int N = 7;
NextValidInteger(arr, N);
}
}
// This code is contributed by Saurabh
Javascript
输出:
2 2 2 4 5 5 0
时间复杂度: O(N)
辅助空间: O(1)