DSAAGP378 - Editorial

Problem Link - Vector Introduction

Problem Statement:

Given N integers, write a program to print:

  • middle element if the N odd and is multiple of 3

  • first and last element(space separated) if N is even and is multiple of 3.

  • else sum of first and last element.

Approach:

The key idea of this solution is to:

  • Check if n is divisible by 3 and then:

    • If true, determine whether n is odd or even.

    • If odd, print the middle element of the list.

    • If even, print the first and last elements of the list.

  • If n is not divisible by 3, print the sum of the first and last elements in the list.

Time Complexity:

  • O(t * n), where t is the number of test cases and n is the length of the sequence in each test case.

Space Complexity:

  • O(n) to store the sequence of numbers for each test case.