原文:
给定一个大小为 n 的、 arr[] ,任务是打印给定数组中出现在奇数索引处的元素(基于 1 的索引)。
示例:
输入: arr[] = {1,2,3,4,5} 输出: 1 3 5 解释: 出现在奇数位置的数组元素为:{1,3,5}。 因此,需要的输出是 1 3 5。
输入: arr[] = {-5,1,4,2,12 } t3】输出: -5 4 12
天真法:解决这个问题最简单的方法是遍历给定数组,检查当前元素的位置是否为奇数。如果发现为真,则打印当前元素。
下面是上述方法的实现:
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
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
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
java 描述语言
// 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.
output:
1 3 5
时间复杂度:o(n) t5辅助空间:** o(1)
有效方法:为了优化上述方法,其思想是只遍历给定数组中出现在奇数位置的那些元素。按照以下步骤解决问题:
- 用循环变量迭代一个从 0 到 n 的循环。
- 打印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
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
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
java 描述语言
output:
1 3 5
时间复杂度:o(n) t5辅助空间:** o(1)
方法 3:在 python 中使用切片:
通过设置步长值为 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)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处