
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to find out the distinct elements in a given sequence
Suppose we are given three integer numbers n, x, y, and z. We have to make a sequence from the given integers where the first item of the sequence is (x modulo 231). Other than the first element, other elements in the sequence ai = (a(i-1) * y + z) modulo 231 , where 1 ≤ i ≤ n - 1. We have to find out the number of distinct integers in the sequence we made.
So, if the input is like n = 5, x = 1, y = 2, z = 1, then the output will be 5.
The unique values are {1, 3, 7, 15, 31}. So, the answer is 5.
To solve this, we will follow these steps −
- MOD := 2^31
- Define an array temp
- resize the array temp to the value of MOD
- p := x mod MOD
- temp[p] := true
- ans := 1
- for initialize i := 1, when i < n, update (increase i by 1), do −
- p := ((p * y) + z) mod MOD
- if temp[p] is true, then −
- Come out from the loop
- increase ans by 1
- temp[p] := true
- return ans
Example
Let us see the following implementation to get better understanding −
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; const long long MOD = 2147483648; int solve(int n, long long x, long long y, long long z) { vector<bool> temp; temp.resize(MOD); long long p = x % MOD; temp[p] = true; int ans = 1; for (int i = 1; i < n; ++i) { p = ((p * y) + z) % MOD; if (temp[p]) break; ++ans; temp[p] = true; } return ans; } int main() { cout<< solve(5, 1, 2, 1) <<endl; return 0; }
Input
5, 1, 2, 1
Output
5
- Related Articles
- Program to find out number of distinct substrings in a given string in python
- Program to Find Out a Sequence with Equivalent Frequencies in Python
- C# Program to get distinct element from a sequence
- Python program to print all distinct elements of a given integer array.
- Program to find nth sequence after following the given string sequence rules in Python
- Golang Program to find the Distinct elements from two arrays
- Program to find sum of given sequence in C++
- Java program to print all distinct elements of a given integer array in Java
- C# program to print all distinct elements of a given integer array in C#
- Program to find the largest product of two distinct elements in Python
- C++ Program to Find the Longest Prefix Matching of a Given Sequence
- C++ Program to Find the Longest Increasing Subsequence of a Given Sequence
- Program to find last digit of the given sequence for given n in Python
- Program to find out the value of a given equation in Python
- C# Program to invert the order of elements in a sequence

Advertisements