Last Updated: 26 Nov, 2020

Terms Of AP

Easy
Asked in companies
Hewlett Packard EnterpriseNucleus SoftwareQuest Global Pvt. Services Ltd

Problem statement

Ayush is given a number ‘X’. He has been told that he has to find the first ‘X’ terms of the series 3 * ‘N’ + 2, which are not multiples of 4. Help Ayush to find it as he has not been able to answer.

Example: Given an ‘X’ = 4. The output array/list which must be passed to Ayush will be [ 5, 11, 14, 17 ].

Input Format:
The first line contains a single integer ‘T’ representing the number of test cases. 

The first line of each test case will contain one integer, ‘X’ that denotes the number of terms he has to answer.
Output Format:
For each test case, return a vector with the first ‘X’ integer of the series 3 * ‘N’ + 2, which is not multiple of 4.

Output for every test case will be printed in a separate line.
Note:
You don’t need to print anything; It has already been taken care of.
Constraints:
1 <= T <= 10^2
1 <= X <= 10^5

Time Limit: 1 sec

Approaches

01 Approach

The basic idea of this approach is to iterate through all the elements obtained in series 3 * ‘N’ + 2. We will see if the number is divisible by 4 or not. If it is acceptable, then we will store it in our final answer array/list. We will run the process until we don’t get the first ‘X’ number of elements.

 

Here is the algorithm:

 

  1. Declare a temporary array/list variable ‘ANS’ in which we store our answer.
  2. Declare a temporary variable ‘GOT’ that will store the total number of elements we obtained until now, which are acceptable.
  3. Declare a temporary variable ‘CURRENT’ that will store the current number of series 3 * ‘N’ + 2 and initialize with the first number of series 5.
  4. Run a loop while ‘GOT’ is not equal to ‘X’:
    • If ‘CURRENT’ is not divisible by 4, we will append the value at the end of ‘ANS’ and increment the value of ‘GOT’ by 1.
    • Increment the value of ‘CURRENT’ by three as the next value of the series.
  5. Finally, return ‘ANS’.