EVEN3N5 Even 3 and 5

Author - Ayush Kumar

Tester - Sidharth Sethi

Editorial - Ayush Kumar

Topic :

Linear Search , Array

Difficulty Level :

EASY

Problem Name :

James asked for candy from his father Jack, Jack gave him a task to complete, his father gave him an Array of integers and asked him to count the number of elements present in the array at even positions which is divisible by both 3 and 5 if James completed the task Jack will buy him candy.

Note

Given Array has zero-based indexing.

Function Description :

In the provided code snippet, implement the function provided requireCount(…) using an integer variable and array and return your answer to the function. You can write your code in the space below the phrase “WRITE YOUR LOGIC HERE”.

Input Format :

First line contains an integer n

Second line contains array of size n

Sample Test Case :

Input :

5

15 25 40 30 45

Output :

2

Constraints :

1 <= n <= 10^6

1 <= A[i] <= 1000000

Output Format :

Integer

Explanation :

In the given array 3 elements 15, 30 and 45 are divisible by both 3 and 5 out of these three elements 15 and 45 are present at an even position. so, only 2 elements are present in an array that satisfies the given condition.

Hence, the count will be 2.

Solution Steps:

  1. Get value from console.

  2. And store in variable n.

  3. Declare array a of size n.

  4. Get n value from console.

  5. Store in array a.

  6. Initialize a variable count to 0 to store the answer.

  7. Implement a logic that if a number is divisible by both 3 and 5 then it must be divisible by 15.

  8. Now iterate over the array using a loop and check for every element which is present at an even position if it is divisible by 15 then increase the count variable by 1.

Running Solution in C++ :

#include <bits/stdc++.h>
using namespace std;
int requireCount(int n, int ar[])
{
    int count = 0;
    for (int i = 0; i < n; i++)
        if ((ar[i] % 15) == 0 && (i % 2) == 0)
            count++;

    return count;
}

int main()
{
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++)
        cin >> a[i];

    cout << (requireCount(n, a));
    return 0;
}