Tuesday, March 5, 2024
HomeProgrammingEmbarking on a 100-Day Adventure: Day 4 - Maximum and Minimum Values...

Embarking on a 100-Day Adventure: Day 4 – Maximum and Minimum Values in An Array

-

Greetings, code enthusiasts! Welcome back to our coding journey. On Day 4, we’re diving into the thrilling quest of finding the maximum and minimum value in an array. Today’s adventure takes us through the landscape of arrays, unveiling the simplicity and power of a code snippet designed to scale the heights and conquer the peaks. Let’s embark on this exciting quest!

Question Statement:

You are given an array of integers. Your task is to write a Java program that finds and displays the maximum and minimum values within the array.

Here’s the challenge:

In this article, as part solution, we’ll be discussing about finding the maximum value in an array. Since finding the minimum value will be almost same, we challenge you to complete the minimum value problem.

Before looking into code we recommend you to spend minimum 15-30 minutes on the code.

Solution:

Let’s break down the basic code that accomplishes this task:

public class MaximumValueInAnArray {
public static void main(String[] args) {
int[] a ={ -1,2,3,-4,5};
int max = Integer.MIN_VALUE;
for(int i = 0; i <a.length; i++){
if (a[i] > max){
max = a[i];
}
}
System.out.println(max);
}
}

Our journey begins with an array, a sequence of numbers representing peaks and valleys. For this example, { -1,2,3,-4,5}.The variable max is initialized with the smallest possible integer value, Integer.MIN_VALUE. This serves as our starting point, preparing for the exploration.

The for loop takes us through each element of the array. The code inside the loop checks if the current element is greater than the current maximum. If yes, it updates the maximum.

the variable max gets updated whenever a higher value is encountered. It’s like ascending a peak and updating our record when we find a higher summit.

The journey concludes with the program proudly displaying the maximum value found during this uncomplicated exploration.

Wrapping Up

As we conclude our journey through the code that unveils the maximum value in an array, let’s reflect on the simplicity and elegance that defines this coding exploration.

Coding is not just about understanding; it’s about hands-on experience. Feel the thrill by running this code with different arrays. Experiment with various numbers and witness the simplicity in action. This journey is not just a demonstration; it’s an invitation to explore, experiment, and enhance your coding skills.

Our exploration doesn’t end here. Stay tuned for more coding adventures, challenges, and discoveries. As we navigate through arrays and algorithms, may simplicity be your companion, guiding you through the peaks and valleys of code.

Happy coding! 💻🚀

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

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...

Two Sum – Day 6 | 100 Days of Code

Welcome to Day 6 of 100 Days of Code where we solve the most frequently asked Easy level Array problem - Two Sum. Let us...

Embarking on a 100-day adventure: Day 5 – No.Of Occurrences In An Array

Welcome to Day 5 of our coding journey! Today, we dive deep into the world of programming to tackle a common, yet curious problem: finding...

Follow us

1,358FansLike
10FollowersFollow
400SubscribersSubscribe

Most Popular