Thursday, July 18, 2024
HomeProgrammingEmbarking on a 100-Day Adventure: Day 3 - Reverse An Array

Embarking on a 100-Day Adventure: Day 3 – Reverse An Array

-

Greetings, Coders! Today, on Day 3 of our coding journey, we’re unraveling the art of array reversal in the simplest way possible. No fancy footwork, just straightforward magic of programming. Let’s dive in to the problem.

Question Statement:

Today, Your task is to write a program that Reverses all the elements in an integer Array.

Basically, if we were given an array with values of 1, 2, 3, 4, and 5. The goal is to flip it into the order 5, 4, 3, 2, 1.

Unlocking the Code:

Before you peek into the solution, please take 10-20 minutes and try to solve it to see if you can do it. It is these tiny struggles to try and understand the problem and try to find an approach that makes us stronger day by day.

Still unable to feel the essence of it? How about a hint?

Can you try it using an additional array that can store the values in reverse?

Okay. Now you know that you tried it well and might want to verify the problem or might still have some trouble understanding it, try to look into the solution and then attempt it from your end.

Example 1: Reversing An Array

public class ReverseAnArray {
public static void main(String[] args) {
int [] a = {1,2,3,4,5};
int [] rev = new int [a. length];
for(int i = 0; i< a.length; i++){
rev[i] = a[a.length -1 -i];
}
for(int v: rev){
System.out.println(v + "");
}
}
}

An array a is initialized with the elements 1, 2, 3, 4, and 5. Another array rev is created with the same length as array a to store the reversed elements. A for loop is used to iterate through each element of the original array a. The current element at index i is assigned to the corresponding position in the reversed array rev. The formula a.length -1-i calculates the reversed index.Another for loop is used to iterate through the reversed array rev. Each element v in the reversed array is printed to the console. Another for loop is used to iterate through the reversed array rev. Each element v in the reversed array is printed to the console.

Example 2: Swapping an Array

We initiate a loop that runs through the first half of the array. During each iteration, the elements at the beginning and end of the array swap places. This simple swap is accomplished using a temporary variable (temp).

public class SwappingAnArray {
public static void main(String[] args) {
int [] a = {1,2,3,4,5};
int n = a.length;
for(int i = 0; i< n/2; i++){
// Swap numbers from ith and (n-1-i)th position;
int temp = a[n-1-i];
a[n-1-i] = a[i];
a[i] = temp;
}
for( int v : a){
System.out.println(v + "");
}
}
}

Wrapping Up:

Array reversal is a fundamental coding challenge that unlocks insights into array manipulation and indexing. By understanding this simple code, you’ve taken a step closer to mastering arrays.

Ready to unlock more coding secrets? Stay tuned for our next adventure, and keep learning ! 💻✨

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

LATEST POSTS

Single Number | Leetcode 136 | 100 Days of Code Day 9

Welcome to Day 9 of Coderefer's 100 Days of code where we will be solving problems from Coderefer DSA sheet each day and today we...

3 Sum | Leetcode #15 | 100 Days of Code Day 10

Today we are going to solve 3 Sum - our first Medium Level Array problem from Coderefer DSA Sheet. If you are new to this...

Best Time to Buy and Sell Stock – Day 7 | 100 Days of Code

Welcome to Day 7 of 100 Days of Code where today we solve yet another most frequently asked easy array problem called - Best Time...

Contains Duplicate – Day 8 | 100 Days of Code

Welcome to Day 8 of 100 Days of Code and today we are going to solve yet another easy-level problem - Contains Duplicate as part...

Follow us

1,358FansLike
10FollowersFollow
397SubscribersSubscribe

Most Popular