📜  打印数组的备用元素

📅  最后修改于: 2021-04-23 20:26:15             🧑  作者: Mango

给定一个大小为N的数组arr [] ,任务是打印给定数组的元素在奇数索引处(基于1的索引)。

例子:

天真的方法:解决此问题的最简单方法是遍历给定的数组,并检查当前元素的位置是否为奇数。如果发现为真,则打印当前元素。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to print
// Alternate elemnts
// of the given array
void printAlter(int arr[], int N)
{
    // Print elements
    // at odd positions
    for (int currIndex = 0;
         currIndex < N; currIndex++) {
 
        // If currIndex stores even index
        // or odd position
        if (currIndex % 2 == 0) {
            cout << arr[currIndex] << " ";
        }
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    printAlter(arr, N);
}


C
// C program to implement
// the above approach
 
#include 
 
// Function to print
// Alternate elemnts
// of the given array
void printAlter(int arr[], int N)
{
    // Print elements
    // at odd positions
    for (int currIndex = 0;
         currIndex < N; currIndex++) {
 
        // If currIndex stores even index
        // or odd position
        if (currIndex % 2 == 0) {
            printf("%d ", arr[currIndex]);
        }
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    printAlter(arr, N);
}


Java
// Java program to implement
// the above approach
import java.io.*;
 
class GFG{
 
// Function to print
// Alternate elemnts
// of the given array
static void printAlter(int[] arr, int N)
{
     
    // Print elements
    // at odd positions
    for(int currIndex = 0;
            currIndex < N;
            currIndex++)
    {
         
        // If currIndex stores even index
        // or odd position
        if (currIndex % 2 == 0)
        {
            System.out.print(arr[currIndex] + " ");
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int[] arr = { 1, 2, 3, 4, 5 };
    int N = arr.length;
     
    printAlter(arr, N);
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program to implement
# the above approach
 
# Function to print
# Alternate elemnts
# of the given array
def printAlter(arr, N):
     
    # Print elements
    # at odd positions
    for currIndex in range(0, N):
         
        # If currIndex stores even index
        # or odd position
        if (currIndex % 2 == 0):
            print(arr[currIndex], end = " ")
 
# Driver Code
if __name__ == "__main__":
     
    arr = [ 1, 2, 3, 4, 5 ]
    N = len(arr)
     
    printAlter(arr, N)
 
# This code is contributed by akhilsaini


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to print
// Alternate elemnts
// of the given array
static void printAlter(int[] arr, int N)
{
     
    // Print elements
    // at odd positions
    for(int currIndex = 0;
            currIndex < N;
            currIndex++)
    {
         
        // If currIndex stores even index
        // or odd position
        if (currIndex % 2 == 0)
        {
            Console.Write(arr[currIndex] + " ");
        }
    }
}
 
// Driver Code
public static void Main()
{
    int[] arr = { 1, 2, 3, 4, 5 };
    int N = arr.Length;
     
    printAlter(arr, N);
}
}
 
// This code is contributed by akhilsaini


C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to print
// Alternate elemnts
// of the given array
void printAlter(int arr[], int N)
{
    // Print elements
    // at odd positions
    for (int currIndex = 0;
         currIndex < N; currIndex += 2) {
 
        // Print elements of array
        cout << arr[currIndex] << " ";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    printAlter(arr, N);
}


C
// C program to implement
// the above approach
 
#include 
 
// Function to print
// Alternate elemnts
// of the given array
void printAlter(int arr[], int N)
{
    // Print elements
    // at odd positions
    for (int currIndex = 0;
         currIndex < N; currIndex += 2) {
 
        // Print elements of array
        printf("%d ", arr[currIndex]);
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    printAlter(arr, N);
}


Java
// Java program to implement
// the above approach
import java.io.*;
 
class GFG{
 
// Function to print
// Alternate elemnts
// of the given array
static void printAlter(int[] arr, int N)
{
     
    // Print elements
    // at odd positions
    for(int currIndex = 0;
            currIndex < N;
            currIndex += 2)
    {
         
        // Print elements of array
        System.out.print(arr[currIndex] + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int[] arr = { 1, 2, 3, 4, 5 };
    int N = arr.length;
     
    printAlter(arr, N);
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program to implement
# the above approach
 
# Function to print
# Alternate elemnts
# of the given array
def printAlter(arr, N):
     
    # Print elements
    # at odd positions
    for currIndex in range(0, N, 2):
         
        # Print elements of array
        print(arr[currIndex], end = " ")
 
# Driver Code
if __name__ == "__main__":
 
    arr = [ 1, 2, 3, 4, 5 ]
    N = len(arr)
     
    printAlter(arr, N)
 
# This code is contributed by akhilsaini


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to print
// Alternate elemnts
// of the given array
static void printAlter(int[] arr, int N)
{
     
    // Print elements
    // at odd positions
    for(int currIndex = 0;
            currIndex < N;
            currIndex += 2)
    {
         
        // Print elements of array
        Console.Write(arr[currIndex] + " ");
    }
}
 
// Driver Code
public static void Main()
{
    int[] arr = { 1, 2, 3, 4, 5 };
    int N = arr.Length;
     
    printAlter(arr, N);
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program to implement
# the above approach
 
# Function to print
# Alternate elemnts
# of the given array
def printAlter(arr, N):
 
    # Print elements
    # at odd positions by using slicing
    # we use * to print with spaces
    print(*arr[::2])
 
 
# Driver Code
if __name__ == "__main__":
 
    arr = [1, 2, 3, 4, 5]
    N = len(arr)
 
    printAlter(arr, N)
 
# This code is contributed by vikkycirus


输出:
1 3 5

时间复杂度: O(N)
辅助空间: O(1)

高效方法:为了优化上述方法,其思想是仅遍历给定数组中出现在奇数位置的那些元素。请按照以下步骤解决问题:

  • 使用循环变量currIndex0N循环循环。
  • 打印arr [currIndex]的值并将currIndex的值增加2,直到currIndex超过N。

下面是上述方法的实现:

C++

// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to print
// Alternate elemnts
// of the given array
void printAlter(int arr[], int N)
{
    // Print elements
    // at odd positions
    for (int currIndex = 0;
         currIndex < N; currIndex += 2) {
 
        // Print elements of array
        cout << arr[currIndex] << " ";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    printAlter(arr, N);
}

C

// C program to implement
// the above approach
 
#include 
 
// Function to print
// Alternate elemnts
// of the given array
void printAlter(int arr[], int N)
{
    // Print elements
    // at odd positions
    for (int currIndex = 0;
         currIndex < N; currIndex += 2) {
 
        // Print elements of array
        printf("%d ", arr[currIndex]);
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    printAlter(arr, N);
}

Java

// Java program to implement
// the above approach
import java.io.*;
 
class GFG{
 
// Function to print
// Alternate elemnts
// of the given array
static void printAlter(int[] arr, int N)
{
     
    // Print elements
    // at odd positions
    for(int currIndex = 0;
            currIndex < N;
            currIndex += 2)
    {
         
        // Print elements of array
        System.out.print(arr[currIndex] + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int[] arr = { 1, 2, 3, 4, 5 };
    int N = arr.length;
     
    printAlter(arr, N);
}
}
 
// This code is contributed by akhilsaini

Python3

# Python3 program to implement
# the above approach
 
# Function to print
# Alternate elemnts
# of the given array
def printAlter(arr, N):
     
    # Print elements
    # at odd positions
    for currIndex in range(0, N, 2):
         
        # Print elements of array
        print(arr[currIndex], end = " ")
 
# Driver Code
if __name__ == "__main__":
 
    arr = [ 1, 2, 3, 4, 5 ]
    N = len(arr)
     
    printAlter(arr, N)
 
# This code is contributed by akhilsaini

C#

// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to print
// Alternate elemnts
// of the given array
static void printAlter(int[] arr, int N)
{
     
    // Print elements
    // at odd positions
    for(int currIndex = 0;
            currIndex < N;
            currIndex += 2)
    {
         
        // Print elements of array
        Console.Write(arr[currIndex] + " ");
    }
}
 
// Driver Code
public static void Main()
{
    int[] arr = { 1, 2, 3, 4, 5 };
    int N = arr.Length;
     
    printAlter(arr, N);
}
}
 
// This code is contributed by akhilsaini
输出:
1 3 5

时间复杂度: O(N)
辅助空间: O(1)

方法3:在Python使用切片:

通过将步骤值设置为2使用Python列表切片进行切片

下面是实现:

Python3

# Python3 program to implement
# the above approach
 
# Function to print
# Alternate elemnts
# of the given array
def printAlter(arr, N):
 
    # Print elements
    # at odd positions by using slicing
    # we use * to print with spaces
    print(*arr[::2])
 
 
# Driver Code
if __name__ == "__main__":
 
    arr = [1, 2, 3, 4, 5]
    N = len(arr)
 
    printAlter(arr, N)
 
# This code is contributed by vikkycirus

输出:

1 3 5

时间复杂度: O(N)

空间复杂度: O(1)