Given a Xor linked list and an integer K , the task is to find the last element from the beginning whose N%K is equal to zero where N is the index of that element.
Examples :
Input : 4<–>7<–>5, K=2 Output : 7 Explanation : Index of 7 is 2 and 2 % K is equal to zero So 7 is the modular element. Input : 4<–>7<–>5<–>1<–>2<–>8<–>3<–>0, K=3 Output : 8 Explanation : Index of 8 is 6 and 6 % K is equal to zero So 8 is the modular element.
Approach :
Follow the steps below to solve the problem:
- Traverse the linked list.
- Check if N%K==0 then store it as modular node.
- Continue the process until we reach the end of the linked list.
Implementation :
// C program to implement
// the above approach
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
// Structure of a node
// in XOR linked list
struct Node {
// Stores data value
// of a node
int data;
// Stores XOR of previous
// pointer and next pointer
struct Node* nxp;
};
// Function to find the XOR of two nodes
struct Node* XOR(struct Node* a, struct Node* b)
{
return (struct Node*)((uintptr_t)(a) ^ (uintptr_t)(b));
}
// Function to insert a node with
// given value at given position
struct Node* insert(struct Node** head, int value)
{
// If XOR linked list is empty
if (*head == NULL) {
// Initialize a new Node
struct Node* node
= (struct Node*)malloc(sizeof(struct Node));
// Stores data value in
// the node
node->data = value;
// Stores XOR of previous
// and next pointer
node->nxp = XOR(NULL, NULL);
// Update pointer of head node
*head = node;
}
// If the XOR linked list
// is not empty
else {
// Stores the address
// of current node
struct Node* curr = *head;
// Stores the address
// of previous node
struct Node* prev = NULL;
// Initialize a new Node
struct Node* node
= (struct Node*)malloc(sizeof(struct Node));
// Update curr node address
curr->nxp = XOR(node, XOR(NULL, curr->nxp));
// Update new node address
node->nxp = XOR(NULL, curr);
// Update head
*head = node;
// Update data value of
// current node
node->data = value;
}
return *head;
}
// Function to find the modular node
void modular(struct Node** head, int k)
{
struct Node* modularNode;
int N = 1;
// If k is negative or zero
if (k <= 0) {
printf("Wrong Input");
}
else {
// Stores XOR pointer
// in current node
struct Node* curr = *head;
// Stores XOR pointer of
// in previous Node
struct Node* prev = NULL;
// Stores XOR pointer of
// in next node
struct Node* next;
// Traverse XOR linked list
while (curr != NULL) {
// Find the modular node
if (N % k == 0) {
modularNode = curr;
}
// Forward traversal
next = XOR(prev, curr->nxp);
// Update prev
prev = curr;
// Update curr
curr = next;
// Index counter
N++;
}
// print the modular node
printf("%d", modularNode->data);
}
}
// Driver Code
int main()
{
/* Create following XOR Linked List
head-->4<–>7<–>5<–>1<–>2<–>8<–>3<–>0*/
struct Node* head = NULL;
insert(&head, 0);
insert(&head, 3);
insert(&head, 8);
insert(&head, 2);
insert(&head, 1);
insert(&head, 5);
insert(&head, 7);
insert(&head, 4);
modular(&head, 3);
return (0);
}
Time Complexity : O(N)
Auxiliary Space : O(1)