Python Array Manipulation Challenge

🚀 Coding Practice | Array Manipulation (Python) 📌 Problem Statement You are given an array of size n, where n is always an even number. Your task is to add every pair of adjacent elements and store the result in a new array of size n/2. 🔹 Example Input array: [1, 2, 3, 4, 4, 5, 6, 7] Output array: [3, 7, 9, 13] 👉 Explanation: (1 + 2) = 3 (3 + 4) = 7 (4 + 5) = 9 (6 + 7) = 13 ✅ My Python Solution arr = [1, 2, 3, 4, 4, 5, 6, 7] n = len(arr) ans = [] for i in range(0, n, 2): a = arr[i] + arr[i + 1] ans.append(a) print(ans) ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n/2) 💡 Challenge for You Can you solve this using: List comprehension? In-place modification? Better space optimization? Drop your alternative solutions in the comments 👇 Let’s learn from each other 🚀 #Python #DSA #CodingPractice #ProblemSolving #LearningEveryday #LinkedInCoding

To view or add a comment, sign in

Explore content categories