Sum of first last element in array

Beginner Problems Language Basics Easy

Given an integer array nums, return the sum of the 1st and last element of the array.

Examples:

Input: nums = [2, 3, 4, 5, 6]

Output: 8

Explanation: 1st element = 2, last element = 6, sum = 2 + 6 = 8.

Input: nums = [2]

Output: 4

Explanation: 1st element = last element = 2, sum = 2 + 2 = 4.

Input: nums = [-1, 2, 4, 1]

Constraints

  • 1 <= Number of elements in nums <= 100
  • -100 <= nums[i] <= 100

Company Tags

TCS Cognizant Accenture Infosys Capgemini Wipro IBM HCL Tech Mahindra MindTree