27/03/2023

Bit Array Hackerrank Solution in C++ | Other Concepts

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.

Bit Array Hackerrank Solution in C++

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


The Output of Bit Array Hackerrank Solution

Similar to Bit Array


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: