DEV Community

Deepak Raj
Deepak Raj

Posted on • Originally published at codeperfectplus.com on

Mini-Max Sum - HackerRank Problem Solving

Mini-Max Sum is a Hackerrank problem from the Algorithms subdomain that requires the understanding of the sum of For-Loop and Array. In this post, you will learn how to solve Hackerrank’s Mini-Max Sum problem and its solution in Python and C++.

Problem Statement and Explanation

Find the maximum and minimum sum of the given array of integers. print the maximum and minimum sum of any four elements of the array.

Example: 2 3 1 4 5

  • Maximum sum: 3 + 4 + 5 + 2 = 14
  • Minimum sum: 1 + 2 + 3 + 4 = 10

Mini-Max Sum Solution in Python

Explanation of Solution in Python

The miniMaxSum(), which takes an array of integers as input and prints the minimum and maximum sum of any four elements of the array.

The function works by first initializing a variable sum to 0. Then, it iterates over the input array and adds each element to sum.

After the loop has finished iterating, the function calculates the minimum and maximum sum of any four elements of the array by subtracting the largest and smallest elements from sum, respectively.

Finally, the function prints the minimum and maximum sum to the console.

Mini-Max Sum Solution in C++

Explanation of Solution in C++

The miniMaxSum(), takes a vector of integers as input and prints the minimum and maximum sum of any four elements of the vector.

The function works by first initializing two variables: min and max, to the maximum and minimum possible long values, respectively.

Then, it iterates over the input vector and calculates the sum of all the elements in the vector except for the current element. This is done by using a nested for loop.

For each iteration of the outer loop, the function updates the min and max variables if the calculated sum is less than min or greater than max, respectively.

After the loop has finished iterating, the function prints the values of min and max to the console.

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay