📜  打印数组的替代元素

📅  最后修改于: 2021-09-06 06:27:19             🧑  作者: Mango

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

例子:

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

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to print
// Alternate elements
// 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 elements
// 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 elements
// 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 elements
# 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 elements
// 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


Javascript
// javascript program to implement
// the above approach
 
// Function to print
// Alternate elements
// of the given array
function printAlter(arr, N)
{
      
    // Print elements
    // at odd positions
    for(var currIndex = 0; currIndex < N; currIndex++)
    {
          
        // If currIndex stores even index
        // or odd position
        if (currIndex % 2 == 0)
        {
            document.write(arr[currIndex] + " ");
        }
    }
}
  
// Driver Code
 
    var arr = [ 1, 2, 3, 4, 5 ]
    var N = arr.length;
      
    printAlter(arr, N);
 
 // This code is contributed by bunnyram19.


C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to print
// Alternate elements
// 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 elements
// 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 elements
// 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 elements
# 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 elements
// 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


Javascript


Python3
# Python3 program to implement
# the above approach
 
# Function to print
# Alternate elements
# 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)

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

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

下面是上述方法的实现:

C++

// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to print
// Alternate elements
// 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 elements
// 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 elements
// 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

蟒蛇3

# Python3 program to implement
# the above approach
 
# Function to print
# Alternate elements
# 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 elements
// 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

Javascript


输出:
1 3 5

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

方法 3:在Python使用切片:

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

下面是实现:

蟒蛇3

# Python3 program to implement
# the above approach
 
# Function to print
# Alternate elements
# 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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live