Assignment 3
Assignment 3
General Instructions
1. Write a separate function/method for every task listed below.
3. Add 1–2 short comments inside each function to describe the main logic.
1. Anyone who has not completed the work will be marked absent immediately—no
excuses.
5. Attempt every problem. Even a partial solution is better than leaving it blank.
6. If you have any doubts, DM me anytime. I won’t judge you, but remember—I can only help if
you also put in effort.
🔹 Problem Statement:
You are given an array of elements. Your task is to print each element one by one, from the rst to
the last. The order in which elements are printed must exactly match the order in which they appear
in the array. You must not skip or rearrange any elements.
📌 Examples:
• Input: [3, 5, 7]
Output: 3 5 7
Explanation: The array has three elements, and they are printed in the same order as they
appear.
• Input: [42]
Output: 42
Explanation: The array contains only one element, so only that element is printed.
📌 Examples:
• Input: [1, 2, 3, 4]
Output: 4 3 2 1
Explanation: The original order is reversed. You start from the last element and go
backwards.
• Input: [99]
Output: 99
Explanation: Only one element—so the reverse is the same.
🔹 Problem Statement:
You are given an array of numbers and a target value. Your task is to nd where in the array the
target value appears for the rst time. You should return the index of this rst appearance. If the
target is not present in the array at all, indicate that appropriately.
📌 Examples:
🔹 Problem Statement:
You are given an array and a target value. Your goal is to nd the last position in the array where
this target appears. If the target value doesn’t appear at all, you should indicate that clearly.
fi
fi
fi
fi
fi
fi
fi
fi
📌 Examples:
5. Two-Sum Problem
🔹 Problem Statement:
You are given an array of numbers and a target value. You need to nd any two numbers in the array
that add up exactlyto the target value. The numbers must be different elements from the array. Your
nal output should be the indices of these two numbers. If no such pair exists, make sure to return
something that makes this clear.
📌 Examples:
🔹 Problem Statement:
You are given an array of elements. You must print them in a zig-zag manner: rst print the rst
element, then the last, then the second element, then the second last, and so on. This pattern
continues inward until all elements are printed.
📌 Examples:
fi
fi
fi
fi
fi
• Input: [1, 2, 3, 4, 5]
Output: 1 5 2 4 3
Explanation: Start with the outermost pair (1 and 5), move inward (2 and 4), and nally
print the center (3).
• Input: [100]
Output: 100
Explanation: Only one element—so nothing else to alternate with.
🔹 Problem Statement:
Given an array of numbers, nd the largest number in the array and also tell its position (index). If
the largest number appears multiple times, just return the position where it rst appears.
📌 Examples:
• Input: [3, 7, 2, 9, 5]
Output: 9 at index 3
Explanation: 9 is the largest number and it appears at index 3.
• Input: [5, 5, 5]
Output: 5 at index 0
Explanation: All elements are equal, so we return the rst occurrence.
🔹 Problem Statement:
You are given an array of integers. Count how many of these numbers are even (i.e., divisible by 2
without any remainder). Return the count of such numbers.
📌 Examples:
• Input: [2, 3, 4, 6, 7]
Output: 3
Explanation: 2, 4, and 6 are even numbers.
fi
fi
fi
fi
• Input: [1, 5, 9]
Output: 0
Explanation: No even numbers are present.