A basic array manipulation problem that introduces conditional element modification.
Given an array nums of size n, replace all negative numbers in the array with zero. Return the modified array.
This problem introduces you to array traversal and conditional modification of elements, which are fundamental skills in array manipulation and data processing.
Your task: Replace all negative numbers in the array with zero and return the modified array.
Input:
nums = [-1, 2, -3, 4, 5]
Output:
[0, 2, 0, 4, 5]
Input:
nums = [0, -5, -10]
Output:
[0, 0, 0]
Input:
nums = [1, 2, 3, 4]
Output:
[1, 2, 3, 4]
A problem that requires finding a missing number and a duplicate number in an array.
Given an unsorted array of size n. Array elements are in the range of 1 to n. One number from set {1, 2, ...n} is missing and one number occurs twice in the array. The task is to find these two numbers.
Your task: Find the missing number and the number that appears twice in the array.
Input:
[3, 1, 3]
Output:
Missing: 2, Twice: 3
Input:
[4, 3, 6, 2, 1, 1]
Output:
Missing: 5, Twice: 1
A fundamental array operation that calculates the total sum of all elements.
Given an array of integers, write a program that calculates and returns the sum of all elements in the array.
This is one of the most fundamental array operations where you need to traverse through each element and accumulate their values. This type of operation is commonly used in mathematical calculations and data analysis.
Your task: Calculate and return the sum of all elements in the array.
Input:
[1, 2, 3, 4, 5]
Output:
15
Input:
[10, 20, 30]
Output:
60
Input:
[7, 3, 9, 1, 6]
Output:
26