Создайте отсортированный массив таким образом, чтобы setbit в побитовом XOR любой пары был четным.
Учитывая целое число N (1 ≤ N ≤ 500), создайте целое число arr[] длины N, такое, что оно должно соответствовать всем указанным ниже условиям:
- Каждый элемент arr[] должен быть уникальным .
- Двоичное представление ( arr[i]^arr[j]) должно содержать четное количество установленных битов для всех ( 1≤i≤N ) и ( i≠j ).
- Все элементы в arr[] должны быть отсортированы.
- A i > 0 для всех (1 ≤ i ≤ N).
Примеры:
Input: N = 2
Output: {10, 15}
Explanation: It can be verified that all elements of arr[]
in each output are distinct and in sorted order and
all the possible pairs of arr[] fulfill the condition mentioned in problem statement..Input: N = 3
Output: {3, 5, 12}
Подход: Чтобы решить проблему, следуйте следующему наблюдению:
It can be observe from outputs that arr[] contains only elements having even parity of set bits in their binary representation. If we try to get some such type of elements under range 1 to 10 using a brute-force code we will get a mathematical series as { 3, 5, 6, 9}.
This series is called “Evil Number“ series and related to Number theory. This series follows all the given conditions of the problem. Therefore, This problem can be solve by printing first N terms of Evil Number series(Excluding 0, As Arr[i] should be greater than zero).
Следуйте инструкциям, чтобы решить проблему:
- Выведите первые N членов Серии Злых чисел или Первые N целых чисел больше нуля, имеющих четную четность установленных битов .
Ниже приведена реализация вышеуказанного подхода:
Временная сложность: O(N 2 )
Вспомогательное пространство: O(1)