Bit Array Hackerrank Solution in C++. You are given four integers: N, S, P, Q. You will use them in order to create the sequence a with the following pseudo-code.

a[0] = S (modulo 2^31)

for i = 1 to N-1

a[i] = a[i-1]*P+Q (modulo 2^31)

Your task is to calculate the number of distinct integers in the sequence a.

**Input Format**

Four space-separated integers on a single line, N, S, P, and Q respectively.

**Output Format**

A single integer denotes the number of distinct integers in the sequence a.

**Sample Input**

3 1 1 1

**Sample Output**

3

**Explanation**

a = [1, 2, 3]

Hence, there are 3 different integers in the sequence.

Submit your solution here: Click here

## Bit Array Hackerrank Solution in C++

```
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
/*Enter your code here. Read input from STDIN. Print output to STDOUT */
unsigned int N, S, P, Q, mod, prev;
cin >> N >> S >> P >> Q;
mod = pow(2, 31);
prev = S;
int numDistinct = 1;
for (int i = 1; i < N; i++)
{
S = (S *P + Q) % mod;
numDistinct += (S != prev) ? 1 : 0;
prev = S;
}
cout << numDistinct;
return 0;
}
```

### The Output of Bit Array Hackerrank Solution

### Similar to Bit Array

- C++ Variadics Hackerrank Solution in C++
- C++ Class Template Specialization Hackerrank Solution in C++
- Attending Workshops Hackerrank Solution in C++
- Overload Operators Hackerrank Solution in C++
- Abstract Classes - Polymorphism Hackerrank Solution in C++
- Exceptional Server Hackerrank Solution in C++
- Inherited Code Hackerrank Solution in C++
- Box It Hackerrank Solution in C++
- Classes and Objects Hackerrank Solution in C++
- Class Hackerrank Solution in C++
- Virtual Functions Hackerrank Solution in C++
- C++ Class Templates Hackerrank Solution in C++
- Operator Overloading Hackerrank Solution in C++

## 0 Comments: