Given an array arr[] that contains integers. You need to print the elements of the array in a single line with a space between them.
Note: Don't add a new line at the end.
Examples:
Input: arr[] = [54, 43, 2, 1, 5]
Output: 54 43 2 1 5
Explanation: Just traverse and print the numbers.Input: arr[] = [324, 5, 2, 2]
Output: 324 5 2 2
Explanation: Just traverse and print the numbers.
Table of Content
Index-Based Traversal - O(n) Time and O(1) Space
The idea is to traverse the array using a for loop and print each element with a space between consecutive elements.
Working of Approach:
- Start traversing the array from index 0.
- Check if the current element is not the first element.
- Print a space before the current element if needed.
- Print the current array element.
- Continue until all elements are printed.
#include <iostream>
#include <vector>
using namespace std;
void arrayTraversal(vector<int> &arr)
{
// Traverse the array and print each element.
for (int i = 0; i < arr.size(); i++)
{
// Print space before every element except the first.
if (i > 0)
cout << " ";
cout << arr[i];
}
}
int main()
{
vector<int> arr = {324, 5, 2, 2};
arrayTraversal(arr);
return 0;
}
#include <stdio.h>
void arrayTraversal(int arr[], int n)
{
// Traverse the array and print each element.
for (int i = 0; i < n; i++)
{
// Print space before every element except the first.
if (i > 0)
printf(" ");
printf("%d", arr[i]);
}
}
int main()
{
int arr[] = {324, 5, 2, 2};
int n = sizeof(arr) / sizeof(arr[0]);
arrayTraversal(arr, n);
return 0;
}
class GFG {
static void arrayTraversal(int[] arr)
{
// Traverse the array and print each element.
for (int i = 0; i < arr.length; i++) {
// Print space before every element except the
// first.
if (i > 0)
System.out.print(" ");
System.out.print(arr[i]);
}
}
public static void main(String[] args)
{
int[] arr = { 324, 5, 2, 2 };
arrayTraversal(arr);
}
}
def arrayTraversal(arr):
# Traverse the array and print each element.
for i in range(len(arr)):
# Print space before every element except the first.
if i > 0:
print(" ", end="")
print(arr[i], end="")
if __name__ == "__main__":
arr = [324, 5, 2, 2]
arrayTraversal(arr)
using System;
class GFG {
static void arrayTraversal(int[] arr)
{
// Traverse the array and print each element.
for (int i = 0; i < arr.Length; i++) {
// Print space before every element except the
// first.
if (i > 0)
Console.Write(" ");
Console.Write(arr[i]);
}
}
static void Main()
{
int[] arr = { 324, 5, 2, 2 };
arrayTraversal(arr);
}
}
function arrayTraversal(arr, size)
{
// Traverse all elements of the array.
for (let i = 0; i < size; i++) {
// Print a space before every element except the
// first.
if (i > 0)
process.stdout.write(" ");
// Print the current element.
process.stdout.write(String(arr[i]));
}
}
// Driver Code
let arr = [ 324, 5, 2, 2 ];
let size = arr.length;
arrayTraversal(arr, size);
Output
324 5 2 2
For-Each Loop - O(n) Time and O(1) Space
The idea is to traverse the array using a for-each loop and print every element with a space between consecutive elements.
Working of Approach:
- Initialize i to track the position of the current element.
- Traverse all elements using a for-each loop.
- Print a space before every element except the first one.
- Print the current element and increment i.
- This ensures there is no extra space at the beginning or end.
Let us understand with an example:
Input: arr[] = [324, 5, 2, 2]
- x = 324, i = 0 -> print 324, then increment i to 1.
- x = 5, i = 1 -> print a space and 5, then increment i to 2.
- x = 2, i = 2 -> print a space and 2, then increment i to 3.
- x = 2, i = 3 -> print a space and 2, then increment i to 4.
Final output: 324 5 2 2.
#include <iostream>
using namespace std;
void arrayTraversal(vector<int> &arr)
{
int i = 0;
// Traverse the array using for-each loop
for (int x : arr)
{
// Print space before every element except the first
if (i > 0)
cout << " ";
cout << x;
i++;
}
}
int main()
{
vector<int> arr = {324, 5, 2, 2};
arrayTraversal(arr);
return 0;
}
#include <stdio.h>
void arrayTraversal(int arr[], int size)
{
int i = 0;
// Traverse the array using for loop
for (int i = 0; i < size; i++)
{
// Print space before every element except the first
if (i > 0)
printf(" ");
printf("%d", arr[i]);
}
}
int main()
{
int arr[] = {324, 5, 2, 2};
int size = sizeof(arr) / sizeof(arr[0]);
arrayTraversal(arr, size);
return 0;
}
import java.util.Arrays;
public class GFG {
public static void arrayTraversal(int[] arr)
{
int i = 0;
// Traverse the array using for-each loop
for (int x : arr) {
// Print space before every element except the
// first
if (i > 0)
System.out.print(" ");
System.out.print(x);
i++;
}
}
public static void main(String[] args)
{
int[] arr = { 324, 5, 2, 2 };
arrayTraversal(arr);
}
}
def arrayTraversal(arr):
i = 0
# Traverse the array using for-each loop
for x in arr:
# Print space before every element except the first
if i > 0:
print(" ", end="")
print(x, end="")
i += 1
if __name__ == "__main__":
arr = [324, 5, 2, 2]
arrayTraversal(arr)
using System;
class GFG {
static void arrayTraversal(int[] arr)
{
int i = 0;
// Traverse the array using for-each loop
foreach(int x in arr)
{
// Print space before every element except the
// first
if (i > 0)
Console.Write(" ");
Console.Write(x);
i++;
}
}
static void Main()
{
int[] arr = { 324, 5, 2, 2 };
arrayTraversal(arr);
}
}
function arrayTraversal(arr, size)
{
let i = 0;
// Traverse the array using for-each loop
for (let x of arr) {
// Stop after the given size
if (i >= size)
break;
// Print space before every element except the first
if (i > 0)
process.stdout.write(" ");
process.stdout.write(String(x));
i++;
}
}
// Driver code
let arr = [ 324, 5, 2, 2 ];
let size = arr.length;
arrayTraversal(arr, size);
Output
324 5 2 2