Find Maximum ElementProblem StatementGiven an array of integers, find the largest element present in the array.Solutiondef find_max(arr): if not arr: return None max_val = arr[0] for num in arr: if num > max_val: max_val = num return max_val
Find Minimum ElementProblem StatementGiven an array of integers, find the smallest element present in the array.Solutiondef find_min(arr): if not arr: return None min_val = arr[0] for num in arr: if num < min_val: min_val = num return min_val
Reverse an ArrayProblem StatementReverse the order of elements in a given array in-place.Solutiondef reverse_array(arr): left, right = 0, len(arr) - 1 while left < right: arr[left], arr[right] = arr[right], arr[left] left += 1 right -= 1 return arr
Check if Array is SortedProblem StatementVerify whether the given array is sorted in non-decreasing order.Solutiondef is_sorted(arr): for i in range(len(arr) - 1): if arr[i] > arr[i+1]: return False return True
Count Frequency of ElementsProblem StatementFind the number of occurrences of each distinct element in an array.Solution`def count_frequency(arr): frequency = {
Find Second Largest ElementProblem StatementGiven an array of integers, find the second largest element. Assume there are at least two distinct elements.Solutiondef find_second_largest(arr): if len(arr) < 2: return None first = second = float('-inf') for num in arr: if num > first: second = first first = num elif num > second and num != first: second = num return second if second != float('-inf') else None
Sum of Array ElementsProblem StatementCalculate the sum of all elements in a given numeric array.Solutiondef array_sum(arr): total = 0 for num in arr: total += num return total
Rotate Array by K PositionsProblem StatementRotate the array to the right by k steps, where k is non-negative.Solutiondef rotate_array(arr, k): n = len(arr) if n == 0: return arr k %= n def reverse(start, end): while start < end: arr[start], arr[end] = arr[end], arr[start] start += 1 end -= 1 reverse(0, n - 1) reverse(0, k - 1) reverse(k, n - 1) return arr
Remove Duplicates from Sorted ArrayProblem StatementGiven a sorted array, remove duplicates in-place such that each element appears only once and return the new length.Solutiondef remove_duplicates(arr): if not arr: return 0 i = 0 for j in range(1, len(arr)): if arr[j] != arr[i]: i += 1 arr[i] = arr[j] return i + 1
Find Missing NumberProblem StatementGiven an array containing n distinct numbers in the range [0, n], return the only number in the range that is missing.Solutiondef find_missing_number(arr): n = len(arr) expected_sum = n * (n + 1) // 2 actual_sum = sum(arr) return expected_sum - actual_sum
Move All Zeros to EndProblem StatementGiven an array, move all 0's to the end while maintaining the relative order of non-zero elements.Solutiondef move_zeros(arr): pos = 0 for i in range(len(arr)): if arr[i] != 0: arr[pos], arr[i] = arr[i], arr[pos] pos += 1 return arr
Find Index of ElementProblem StatementFind the index of the first occurrence of a target element in an array. Return -1 if not found.Solutiondef find_index(arr, target): for i in range(len(arr)): if arr[i] == target: return i return -1
Linear Search ImplementationProblem StatementImplement the linear search algorithm to find if an element exists in an array.Solutiondef linear_search(arr, target): for element in arr: if element == target: return True return False
Count Even and Odd NumbersProblem StatementReturn a count of even and odd integers present in the array.Solution`def count_even_odd(arr): even_count = 0 odd_count = 0 for num in arr: if num % 2 == 0: even_count += 1 else: odd_count += 1 return {"even": even_count, "odd": odd_count
Find Largest Pair SumProblem StatementFind the maximum sum of any two elements in an array.Solutiondef largest_pair_sum(arr): if len(arr) < 2: return None max1 = max2 = float('-inf') for num in arr: if num > max1: max2 = max1 max1 = num elif num > max2: max2 = num return max1 + max2
Merge Two ArraysProblem StatementMerge two sorted arrays into a single sorted array.Solutiondef merge_sorted_arrays(arr1, arr2): i = j = 0 result = [] while i < len(arr1) and j < len(arr2): if arr1[i] < arr2[j]: result.append(arr1[i]) i += 1 else: result.append(arr2[j]) j += 1 result.extend(arr1[i:]) result.extend(arr2[j:]) return result
Check Palindrome ArrayProblem StatementDetermine if the given array is a palindrome (reads the same forward and backward).Solutiondef is_palindrome(arr): left, right = 0, len(arr) - 1 while left < right: if arr[left] != arr[right]: return False left += 1 right -= 1 return True
Find Duplicate ElementProblem StatementFind all duplicate elements in an array. Each duplicate should appear only once in the result.Solutiondef find_duplicates(arr): seen = set() duplicates = set() for num in arr: if num in seen: duplicates.add(num) else: seen.add(num) return list(duplicates)
Count Occurrences of a NumberProblem StatementFind how many times a target number appears in an array.Solutiondef count_occurrences(arr, target): count = 0 for num in arr: if num == target: count += 1 return count
Find Intersection of Two ArraysProblem StatementFind the common elements present in two given arrays.Solutiondef find_intersection(arr1, arr2): set1 = set(arr1) set2 = set(arr2) return list(set1 & set2)
Find Union of Two ArraysProblem StatementFind the union of two arrays (all unique elements from both).Solutiondef find_union(arr1, arr2): return list(set(arr1) | set(arr2))
Replace Elements with Greatest on RightProblem StatementReplace every element in the array with the greatest element among the elements to its right, and replace the last element with -1.Solutiondef replace_elements(arr): n = len(arr) max_seen = -1 for i in range(n - 1, -1, -1): temp = arr[i] arr[i] = max_seen if temp > max_seen: max_seen = temp return arr
Find Equilibrium IndexProblem StatementFind the index where the sum of elements to its left equals the sum of elements to its right.Solutiondef equilibrium_index(arr): total_sum = sum(arr) left_sum = 0 for i in range(len(arr)): right_sum = total_sum - left_sum - arr[i] if left_sum == right_sum: return i left_sum += arr[i] return -1
Prefix Sum ArrayProblem StatementCreate a prefix sum array where each element at index i is the sum of elements from 0 to i.Solutiondef get_prefix_sum(arr): if not arr: return [] prefix = [0] * len(arr) prefix[0] = arr[0] for i in range(1, len(arr)): prefix[i] = prefix[i-1] + arr[i] return prefix
Find Smallest ElementProblem StatementIdentify the minimum value in an array.Solutiondef find_min(arr): if not arr: return None min_val = arr[0] for num in arr: if num < min_val: min_val = num return min_val
Check if Array Contains ElementProblem StatementCheck if a specific element exists in the given array.Solutiondef contains_element(arr, target): for num in arr: if num == target: return True return False
Copy Array ElementsProblem StatementCreate a new array that is a copy of the original array.Solutiondef copy_array(arr): copy_arr = [] for num in arr: copy_arr.append(num) return copy_arr
Find Difference Between Max and MinProblem StatementCalculate the difference between the maximum and minimum elements in an array.Solutiondef max_min_difference(arr): if not arr: return 0 max_val = min_val = arr[0] for num in arr: if num > max_val: max_val = num if num < min_val: min_val = num return max_val - min_val
Check Array RotationProblem StatementCheck if an array is a rotated version of another array.Solutiondef is_rotated(arr1, arr2): if len(arr1) != len(arr2): return False # Simplified check using concatenation s1 = "".join(map(str, arr1)) s2 = "".join(map(str, arr2)) return s2 in (s1 + s1)
Left Rotate Array by OneProblem StatementShift all elements of the array to the left by one position in-place.Solutiondef left_rotate_one(arr): if not arr: return arr first = arr[0] for i in range(len(arr) - 1): arr[i] = arr[i+1] arr[len(arr)-1] = first return arr
Find Pair with Given SumProblem StatementFind if there exists a pair of elements in the array whose sum equals a given target.Solutiondef find_pair_sum(arr, target): seen = set() for num in arr: complement = target - num if complement in seen: return (complement, num) seen.add(num) return None
Find Subarray with Sum ZeroProblem StatementReturn True if there is a subarray (continuous elements) whose sum is equal to zero.Solution`def has_zero_sum_subarray(arr): seen_sums = {0
Count Distinct ElementsProblem StatementCalculate the number of unique elements in an array.Solutiondef count_distinct(arr): return len(set(arr))
Find First Repeating ElementProblem StatementFind the first element in an array that repeats. Return the element or None.Solutiondef first_repeating(arr): seen = set() for num in arr: if num in seen: return num seen.add(num) return None
Find Last OccurrenceProblem StatementFind the index of the last occurrence of a target element in an array.Solutiondef find_last_occurrence(arr, target): for i in range(len(arr) - 1, -1, -1): if arr[i] == target: return i return -1
Sum of Alternate ElementsProblem StatementCalculate the sum of elements at even indices (0, 2, 4...) in an array.Solutiondef sum_alternate(arr): alt_sum = 0 for i in range(0, len(arr), 2): alt_sum += arr[i] return alt_sum
Find Largest Three ElementsProblem StatementFind the three largest distinct elements in an array.Solutiondef largest_three(arr): first = second = third = float('-inf') for num in arr: if num > first: third = second second = first first = num elif num > second and num != first: third = second second = num elif num > third and num != first and num != second: third = num return [first, second, third]
Check Monotonic ArrayProblem StatementDetermine if an array is monotonic (either entirely non-increasing or entirely non-decreasing).Solutiondef is_monotonic(arr): increasing = decreasing = True for i in range(1, len(arr)): if arr[i] < arr[i-1]: increasing = False if arr[i] > arr[i-1]: decreasing = False return increasing or decreasing
Remove Specific ElementProblem StatementRemove all occurrences of a specific element from the array in-place and return the new length.Solutiondef remove_element(arr, val): pos = 0 for i in range(len(arr)): if arr[i] != val: arr[pos] = arr[i] pos += 1 return pos
Find Majority Element (Basic)Problem StatementFind the element that appears more than n/2 times in an array of size n.Solutiondef find_majority(arr): candidate = None count = 0 for num in arr: if count == 0: candidate = num count = 1 elif num == candidate: count += 1 else: count -= 1 return candidate