Problem Link - Vector Introduction
Problem Statement:
Given N integers, write a program to print:
-
middle element if the
Nodd and is multiple of3 -
first and last element(space separated) if
Nis even and is multiple of3. -
else sum of first and last element.
Approach:
The key idea of this solution is to:
-
Check if
nis divisible by3and then:-
If true, determine whether
nis odd or even. -
If odd, print the middle element of the list.
-
If even, print the first and last elements of the list.
-
-
If
nis not divisible by 3, print the sum of the first and last elements in the list.
Time Complexity:
- O(t * n), where
tis the number of test cases andnis the length of the sequence in each test case.
Space Complexity:
- O(n) to store the sequence of numbers for each test case.