Counting pairs with prime bitwise AND in a Singly Linked List
Given a Singly linked list of integers, the task is to count the number of pairs of nodes whose bitwise AND is a prime number.
Examples:
Input: 4 -> 2 -> 3 -> 1 -> 5 -> 6
Output: 3
Explanation: The only pair with bitwise AND being a prime number is (2, 3), (2, 6), (3, 6).Input: 10 -> 3 -> 5 -> 7 -> 2 -> 15
Output: 11
Explanation: The pairs with bitwise AND being a prime number are (10, 3), (10, 7), (10, 2), (3, 7), (3, 2), (3, 15), (5, 7), (5, 15), (7, 2), (7, 15), (2, 15).
Approach: This can be solved with the following idea:
To solve this problem, we can iterate through the linked list, and for each node, we can iterate over all nodes after it and check if the bitwise AND of their values is a prime number. If yes, we can increment the count. To check if a number is prime or not, we can write a helper function
Below are the steps to implement the above idea:
- Define a helper function to check if a given number is prime or not.
- Define a variable “count” to keep track of the number of pairs with bitwise AND being a prime number.
- Traverse the linked list using two nested loops :
- For each node “curr”, traverse all nodes after it using another loop:
- If the bitwise AND of the values of “curr” and “next” nodes is a prime number, increment the count.
- For each node “curr”, traverse all nodes after it using another loop:
- Return the count.
Below is the code for the above approach:
C++
|
Time Complexity: O(n^2 * log(max_val))
Auxiliary Space: O(1)
Last Updated :
24 Apr, 2023
Like Article
Save Article
Given a Singly linked list of integers, the task is to count the number of pairs of nodes whose bitwise AND is a prime number.
Examples:
Input: 4 -> 2 -> 3 -> 1 -> 5 -> 6
Output: 3
Explanation: The only pair with bitwise AND being a prime number is (2, 3), (2, 6), (3, 6).Input: 10 -> 3 -> 5 -> 7 -> 2 -> 15
Output: 11
Explanation: The pairs with bitwise AND being a prime number are (10, 3), (10, 7), (10, 2), (3, 7), (3, 2), (3, 15), (5, 7), (5, 15), (7, 2), (7, 15), (2, 15).
Approach: This can be solved with the following idea:
To solve this problem, we can iterate through the linked list, and for each node, we can iterate over all nodes after it and check if the bitwise AND of their values is a prime number. If yes, we can increment the count. To check if a number is prime or not, we can write a helper function
Below are the steps to implement the above idea:
- Define a helper function to check if a given number is prime or not.
- Define a variable “count” to keep track of the number of pairs with bitwise AND being a prime number.
- Traverse the linked list using two nested loops :
- For each node “curr”, traverse all nodes after it using another loop:
- If the bitwise AND of the values of “curr” and “next” nodes is a prime number, increment the count.
- For each node “curr”, traverse all nodes after it using another loop:
- Return the count.
Below is the code for the above approach:
C++
|
Time Complexity: O(n^2 * log(max_val))
Auxiliary Space: O(1)
Last Updated :
24 Apr, 2023
Like Article
Save Article