Day 3: Conquering Two Array Questions — My 100-Day Journey
Introduction
Hey everyone! 👋 Today’s the 3rd day of my 100-day coding challenge . Join me as I share my experience conquering two coding challenges and exploring Arrays.
Coding Challenges
Challenge 1: Left Rotate an Array by One
Solution 1:
It stores the first element of the array in a temporary variable temp. Then, it iterates through the array and shifts each element to the left by one position, excluding the last element. Finally, it assigns the value of temp to the last element of the array. After the rotation, it prints the elements of the array. When executed with n = 5 and arr = [1, 2, 3, 4, 5], it will output the array [2, 3, 4, 5, 1], representing the array rotated to the left by one position.
Time Complexity: O(n), as we iterate through the array only once.
Get UTTAM KUMAR SAHOO’s stories in your inbox
Join Medium for free to get updates from this writer.
Space Complexity: O(1) as no extra space is used
Challenge 2: Missing Number
Solution 2:
It first computes the summation of the first N numbers using the formula (N * (N + 1)) // 2. Then, it calculates the summation of all elements in the given array a. The difference between the summation of the first N numbers and the summation of array elements gives the missing number. Finally, it returns the missing number. This approach leverages the concept that the difference between the expected summation and the actual summation will be equal to the missing number.
Time Complexity: O(N), where N = size of array+1.
Space Complexity: O(1) as we are not using any extra space.
One thing that i found exciting today🌻
“Knowing what it feels to be in pain, is exactly why we try to be kind to others” — Master Jiraiya(Naruto)







