📌  相关文章
📜  将数组的每个元素替换为其下一个元素

📅  最后修改于: 2021-05-31 18:46:19             🧑  作者: Mango

给定一个数组arr ,任务是将数组中的每个元素替换为其后出现的元素,并将最后一个元素替换为-1

例子:

方法:将数组从0遍历到n-2并更新arr [i] = arr [i + 1] 。最后设置a [n-1] = -1并打印更新后的数组的内容。

下面是上述方法的实现:

C++
// C++ program to replace every element of the array
// with the element that appears after it
#include 
using namespace std;
  
// Function to print the array after replacing every element
// of the array with the element that appears after it
void updateArray(int arr[], int n)
{
    // Update array
    for (int i = 0; i <= n - 2; i++)
        arr[i] = arr[i + 1];
  
    // Change the last element to -1
    arr[n - 1] = -1;
  
    // Print the updated array
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
  
// Driver program
int main()
{
    int arr[] = { 5, 1, 3, 2, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
    updateArray(arr, N);
    return 0;
}


Java
// Java program to replace every element 
// of the array with the element that 
// appears after it
class GFG
{
  
// Function to print the array after 
// replacing every element of the array
// with the element that appears after it
static void updateArray(int arr[], int n)
{
    // Update array
    for (int i = 0; i <= n - 2; i++)
        arr[i] = arr[i + 1];
  
    // Change the last element to -1
    arr[n - 1] = -1;
  
    // Print the updated array
    for (int i = 0; i < n; i++)
        System.out.print(arr[i] + " ");
}
  
// Driver Code
public static void main(String []args)
{
    int arr[] = { 5, 1, 3, 2, 4 } ;
    int N = arr.length ;
    updateArray(arr, N);
}
}
  
// This code is contributed by Ryuga


Python3
# Python3 program to replace every 
# element of the array with the
# element that appears after it 
  
# Function to print the array after 
# replacing every element of the 
# array with the element that appears
# after it 
def updateArray(arr, n):
  
    # Update array 
    for i in range (n - 1):
        arr[i] = arr[i + 1]
  
    # Change the last element to -1 
    arr[n - 1] = -1
  
    # Print the updated array 
    for i in range( n):
        print (arr[i], end = " ")
  
# Driver Code 
if __name__ == "__main__":
  
    arr = [ 5, 1, 3, 2, 4 ]
    N = len(arr)
    updateArray(arr, N)
  
# This code is contributed 
# by ChitraNayal


C#
// C# program to replace every element 
// of the array with the element that 
// appears after it
using System;
  
class GFG
{
  
// Function to print the array after 
// replacing every element of the array
// with the element that appears after it
static void updateArray(int[] arr, int n)
{
    // Update array
    for (int i = 0; i <= n - 2; i++)
        arr[i] = arr[i + 1];
  
    // Change the last element to -1
    arr[n - 1] = -1;
  
    // Print the updated array
    for (int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
}
  
// Driver Code
public static void Main()
{
    int[] arr = { 5, 1, 3, 2, 4 } ;
    int N = arr.Length ;
    updateArray(arr, N);
}
}
  
// This code is contributed 
// by Akanksha Rai


PHP


输出:
1 3 2 4 -1

时间复杂度: O(n)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。