Day 29 DSA: Problem Solving

DSA problem guide in JavaScript: sum of array elements and maximum profit with a single buy and sell, with efficient solutions and practical examples.

domingo, 17 de agosto de 2025 • 2 min read • Q2BSTUDIO Team

Artificial-Intelligence-

Question 1 Sum of elements

Statement Given an array of integers called prices, return the sum of all elements in the array

Example Input [1, 2, 3] Output 6

Approach Initialize a variable sum to 0. Loop through each element of the array and add the current value to sum. Finally, return sum.

Example implementation in JavaScript var SumOfDigits = function(prices) { let sum = 0; for (let i = 0; i < prices.length; i++) { sum += prices[i]; } return sum; }; console.log(SumOfDigits([1,2,3])); // Output 6

Edge cases empty array [] returns 0; array with one element [10] returns 10; array with negative numbers [-1, -2, 3] the total sum would be 0 in this example if that result is expected according to the problem rules

Complexity Time O(n) where n is the length of the array. Space O(1) use of one additional constant variable.

Question 2 Maximum profit with a single buy and sell

Statement Given an array prices, find the maximum possible profit by making a single buy and a single sell with the condition of buying before selling

Example Input [7, 1, 5, 3, 6, 4] Output 5

Approach Keep the minimum price seen so far and the maximum profit so far. For each price, calculate the current profit prices[i] minus min_Price, update max_Profit if it is greater, and update min_Price if the current price is lower. Finally, return max_Profit.

Example implementation in JavaScript var maxProfit = function(prices) { if (prices.length === 0) return 0; let min_Price = prices[0]; let max_Profit = 0; for (let i = 1; i < prices.length; i++) { if (prices[i] - min_Price > max_Profit) { max_Profit = prices[i] - min_Price; } if (prices[i] < min_Price) { min_Price = prices[i]; } } return max_Profit; }; console.log(maxProfit([7,1,5,3,6,4])); // Output 5

Complexity Time O(n) we traverse the array once. Space O(1) only auxiliary variables min_Price and max_Profit.

About Q2BSTUDIO We are Q2BSTUDIO, a custom software and application development company specialized in artificial intelligence, cybersecurity, and cloud solutions. We develop custom software and custom applications tailored to each client's needs, offering aws and azure cloud services, business intelligence services, and power bi implementation. We are specialists in artificial intelligence and offer AI solutions for companies, including personalized AI agents that automate processes and improve efficiency.

Services we offer Custom application development, custom software, integration with aws and azure cloud services, artificial intelligence solutions, implementation of AI agents, business intelligence services, advanced analytics and visualization with power bi, and cybersecurity strategies to protect data and infrastructures.

Why choose us Our team combines experience in custom development with advanced capabilities in artificial intelligence and cybersecurity to deliver secure, scalable, and business-oriented solutions. We implement business intelligence services that transform data into decisions and offer support for deployments on aws and azure cloud services.

Keywords custom applications, custom software, artificial intelligence, cybersecurity, aws and azure cloud services, business intelligence services, AI for companies, AI agents, power bi

Day 29 Of Dsa Problem Solving This article is part of the Day 29 Of Dsa Problem Solving series, practice focused on solving classic algorithm and data structure problems by applying solutions that can be integrated into real Q2BSTUDIO projects

A BREAK?

Play for a moment before you go

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.