SlideShare a Scribd company logo
1 of 4
Download to read offline
Need this in JAVA We have N numbers as an array, you need to find a prefix array and a suffix
array, which we can get the maximum x or value with all elements in them. Notice that for
prefix[0, 1] and suffix[r, n - 1], do not intersect (I
Solution
// C++ program for a Trie based O(n) solution to find max
// subarray XOR
#include
using namespace std;
// Assumed int size
#define INT_SIZE 32
// A Trie Node
struct TrieNode
{
int value; // Only used in leaf nodes
TrieNode *arr[2];
};
// Utility function tp create a Trie node
TrieNode *newNode()
{
TrieNode *temp = new TrieNode;
temp->value = 0;
temp->arr[0] = temp->arr[1] = NULL;
return temp;
}
// Inserts pre_xor to trie with given root
void insert(TrieNode *root, int pre_xor)
{
TrieNode *temp = root;
// Start from the msb, insert all bits of
// pre_xor into Trie
for (int i=INT_SIZE-1; i>=0; i--)
{
// Find current bit in given prefix
bool val = pre_xor & (1<arr[val] == NULL)
temp->arr[val] = newNode();
temp = temp->arr[val];
}
// Store value at leaf node
temp->value = pre_xor;
}
// Finds the maximum XOR ending with last number in
// prefix XOR 'pre_xor' and returns the XOR of this maximum
// with pre_xor which is maximum XOR ending with last element
// of pre_xor.
int query(TrieNode *root, int pre_xor)
{
TrieNode *temp = root;
for (int i=INT_SIZE-1; i>=0; i--)
{
// Find current bit in given prefix
bool val = pre_xor & (1<arr[1-val]!=NULL)
temp = temp->arr[1-val];
// If there is no prefix with opposite
// bit, then look for same bit.
else if (temp->arr[val] != NULL)
temp = temp->arr[val];
}
return pre_xor^(temp->value);
}
// Returns maximum XOR value of a subarray in arr[0..n-1]
int maxSubarrayXOR(int arr[], int n)
{
// Create a Trie and insert 0 into it
TrieNode *root = newNode();
insert(root, 0);
// Initialize answer and xor of current prefix
int result = INT_MIN, pre_xor =0;
// Traverse all input array element
for (int i=0; i
using namespace std;
// Assumed int size
#define INT_SIZE 32
// A Trie Node
struct TrieNode
{
int value; // Only used in leaf nodes
TrieNode *arr[2];
};
// Utility function tp create a Trie node
TrieNode *newNode()
{
TrieNode *temp = new TrieNode;
temp->value = 0;
temp->arr[0] = temp->arr[1] = NULL;
return temp;
}
// Inserts pre_xor to trie with given root
void insert(TrieNode *root, int pre_xor)
{
TrieNode *temp = root;
// Start from the msb, insert all bits of
// pre_xor into Trie
for (int i=INT_SIZE-1; i>=0; i--)
{
// Find current bit in given prefix
bool val = pre_xor & (1<arr[val] == NULL)
temp->arr[val] = newNode();
temp = temp->arr[val];
}
// Store value at leaf node
temp->value = pre_xor;
}
// Finds the maximum XOR ending with last number in
// prefix XOR 'pre_xor' and returns the XOR of this maximum
// with pre_xor which is maximum XOR ending with last element
// of pre_xor.
int query(TrieNode *root, int pre_xor)
{
TrieNode *temp = root;
for (int i=INT_SIZE-1; i>=0; i--)
{
// Find current bit in given prefix
bool val = pre_xor & (1<arr[1-val]!=NULL)
temp = temp->arr[1-val];
// If there is no prefix with opposite
// bit, then look for same bit.
else if (temp->arr[val] != NULL)
temp = temp->arr[val];
}
return pre_xor^(temp->value);
}
// Returns maximum XOR value of a subarray in arr[0..n-1]
int maxSubarrayXOR(int arr[], int n)
{
// Create a Trie and insert 0 into it
TrieNode *root = newNode();
insert(root, 0);
// Initialize answer and xor of current prefix
int result = INT_MIN, pre_xor =0;
// Traverse all input array element
for (int i=0; i

More Related Content

Similar to Need this in JAVA We have N numbers as an array, you need to find a .pdf

operating system linux,ubuntu,Mac#include iostream #include .pdf
operating system linux,ubuntu,Mac#include iostream #include .pdfoperating system linux,ubuntu,Mac#include iostream #include .pdf
operating system linux,ubuntu,Mac#include iostream #include .pdf
aquacareser
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
Saad Gabr
 
Given an IntNode struct and the operating functions for a linked list-.pdf
Given an IntNode struct and the operating functions for a linked list-.pdfGiven an IntNode struct and the operating functions for a linked list-.pdf
Given an IntNode struct and the operating functions for a linked list-.pdf
NicholasflqStewartl
 
8-25 LAB- Simple linked list Given an IntNode struct and the operating.docx
8-25 LAB- Simple linked list Given an IntNode struct and the operating.docx8-25 LAB- Simple linked list Given an IntNode struct and the operating.docx
8-25 LAB- Simple linked list Given an IntNode struct and the operating.docx
NathanBOCAbrahamc
 
take the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdftake the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdf
fastechsrv
 
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
arihantelehyb
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
JUSTSTYLISH3B2MOHALI
 

Similar to Need this in JAVA We have N numbers as an array, you need to find a .pdf (20)

Write a program in C that generates a list of random numbers greater.pdf
Write a program in C that generates a list of random numbers greater.pdfWrite a program in C that generates a list of random numbers greater.pdf
Write a program in C that generates a list of random numbers greater.pdf
 
Binary Tree
Binary  TreeBinary  Tree
Binary Tree
 
Provide copy constructor- destructor- and assignment operator for the.docx
Provide copy constructor- destructor- and assignment operator for the.docxProvide copy constructor- destructor- and assignment operator for the.docx
Provide copy constructor- destructor- and assignment operator for the.docx
 
operating system linux,ubuntu,Mac#include iostream #include .pdf
operating system linux,ubuntu,Mac#include iostream #include .pdfoperating system linux,ubuntu,Mac#include iostream #include .pdf
operating system linux,ubuntu,Mac#include iostream #include .pdf
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
 
Lec2
Lec2Lec2
Lec2
 
Lec2&3_DataStructure
Lec2&3_DataStructureLec2&3_DataStructure
Lec2&3_DataStructure
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
Consider L = {a^nb^2nc^P p 0}. Prove L is not a context-free langu.pdf
Consider L = {a^nb^2nc^P  p  0}. Prove L is not a context-free langu.pdfConsider L = {a^nb^2nc^P  p  0}. Prove L is not a context-free langu.pdf
Consider L = {a^nb^2nc^P p 0}. Prove L is not a context-free langu.pdf
 
Given an IntNode struct and the operating functions for a linked list-.pdf
Given an IntNode struct and the operating functions for a linked list-.pdfGiven an IntNode struct and the operating functions for a linked list-.pdf
Given an IntNode struct and the operating functions for a linked list-.pdf
 
Computer Programming- Lecture 8
Computer Programming- Lecture 8Computer Programming- Lecture 8
Computer Programming- Lecture 8
 
8-25 LAB- Simple linked list Given an IntNode struct and the operating.docx
8-25 LAB- Simple linked list Given an IntNode struct and the operating.docx8-25 LAB- Simple linked list Given an IntNode struct and the operating.docx
8-25 LAB- Simple linked list Given an IntNode struct and the operating.docx
 
Hw3
Hw3Hw3
Hw3
 
take the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdftake the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdf
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
PyTorch Introduction
PyTorch IntroductionPyTorch Introduction
PyTorch Introduction
 
C++11 - STL Additions
C++11 - STL AdditionsC++11 - STL Additions
C++11 - STL Additions
 
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
I need help with the 2nd TODO comment and the other comments Ill.pdf
I need help with the 2nd TODO comment and the other comments Ill.pdfI need help with the 2nd TODO comment and the other comments Ill.pdf
I need help with the 2nd TODO comment and the other comments Ill.pdf
 

More from arorastores

Modify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdfModify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdf
arorastores
 
looking for help with this question, have another question that.pdf
looking for help with this question, have another question that.pdflooking for help with this question, have another question that.pdf
looking for help with this question, have another question that.pdf
arorastores
 
I have a Student.java class constructor like thisAnd I need to re.pdf
I have a Student.java class constructor like thisAnd I need to re.pdfI have a Student.java class constructor like thisAnd I need to re.pdf
I have a Student.java class constructor like thisAnd I need to re.pdf
arorastores
 
How do incomplete dominance and codominance differ There is no diff.pdf
How do incomplete dominance and codominance differ  There is no diff.pdfHow do incomplete dominance and codominance differ  There is no diff.pdf
How do incomplete dominance and codominance differ There is no diff.pdf
arorastores
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
arorastores
 
Describe the transmission of the meningococcus, and discuss the path.pdf
Describe the transmission of the meningococcus, and discuss the path.pdfDescribe the transmission of the meningococcus, and discuss the path.pdf
Describe the transmission of the meningococcus, and discuss the path.pdf
arorastores
 
dass Defining Species art C Gene flow and the biological species conc.pdf
dass Defining Species art C Gene flow and the biological species conc.pdfdass Defining Species art C Gene flow and the biological species conc.pdf
dass Defining Species art C Gene flow and the biological species conc.pdf
arorastores
 
Describe one way in which you could use butterfly rearing in the fut.pdf
Describe one way in which you could use butterfly rearing in the fut.pdfDescribe one way in which you could use butterfly rearing in the fut.pdf
Describe one way in which you could use butterfly rearing in the fut.pdf
arorastores
 
An infinitely long sheet of charge of width L lies in the xy -plane .pdf
An infinitely long sheet of charge of width L lies in the xy -plane .pdfAn infinitely long sheet of charge of width L lies in the xy -plane .pdf
An infinitely long sheet of charge of width L lies in the xy -plane .pdf
arorastores
 
A study tracked incidence rates of a blood vessel disorder over time.pdf
A study tracked incidence rates of a blood vessel disorder over time.pdfA study tracked incidence rates of a blood vessel disorder over time.pdf
A study tracked incidence rates of a blood vessel disorder over time.pdf
arorastores
 

More from arorastores (20)

Modify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdfModify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdf
 
looking for help with this question, have another question that.pdf
looking for help with this question, have another question that.pdflooking for help with this question, have another question that.pdf
looking for help with this question, have another question that.pdf
 
In a population of Canadian Eskimos the autosomal recessive gene for.pdf
In a population of Canadian Eskimos the autosomal recessive gene for.pdfIn a population of Canadian Eskimos the autosomal recessive gene for.pdf
In a population of Canadian Eskimos the autosomal recessive gene for.pdf
 
How is that terrorists are able to use media to their advantage an.pdf
How is that terrorists are able to use media to their advantage an.pdfHow is that terrorists are able to use media to their advantage an.pdf
How is that terrorists are able to use media to their advantage an.pdf
 
Identify the three ventral body cavities and the two dorsal body cavi.pdf
Identify the three ventral body cavities and the two dorsal body cavi.pdfIdentify the three ventral body cavities and the two dorsal body cavi.pdf
Identify the three ventral body cavities and the two dorsal body cavi.pdf
 
I have a Student.java class constructor like thisAnd I need to re.pdf
I have a Student.java class constructor like thisAnd I need to re.pdfI have a Student.java class constructor like thisAnd I need to re.pdf
I have a Student.java class constructor like thisAnd I need to re.pdf
 
How do incomplete dominance and codominance differ There is no diff.pdf
How do incomplete dominance and codominance differ  There is no diff.pdfHow do incomplete dominance and codominance differ  There is no diff.pdf
How do incomplete dominance and codominance differ There is no diff.pdf
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
 
Fruits develop from microsporangia receptacles fertilized eggs ovarie.pdf
Fruits develop from microsporangia receptacles fertilized eggs ovarie.pdfFruits develop from microsporangia receptacles fertilized eggs ovarie.pdf
Fruits develop from microsporangia receptacles fertilized eggs ovarie.pdf
 
Find all equilibrium solutions of the equation dydx = y^2 - 1. Use o.pdf
Find all equilibrium solutions of the equation dydx = y^2 - 1. Use o.pdfFind all equilibrium solutions of the equation dydx = y^2 - 1. Use o.pdf
Find all equilibrium solutions of the equation dydx = y^2 - 1. Use o.pdf
 
Determine which protist causes each of the following diseases in huma.pdf
Determine which protist causes each of the following diseases in huma.pdfDetermine which protist causes each of the following diseases in huma.pdf
Determine which protist causes each of the following diseases in huma.pdf
 
Describe the transmission of the meningococcus, and discuss the path.pdf
Describe the transmission of the meningococcus, and discuss the path.pdfDescribe the transmission of the meningococcus, and discuss the path.pdf
Describe the transmission of the meningococcus, and discuss the path.pdf
 
dass Defining Species art C Gene flow and the biological species conc.pdf
dass Defining Species art C Gene flow and the biological species conc.pdfdass Defining Species art C Gene flow and the biological species conc.pdf
dass Defining Species art C Gene flow and the biological species conc.pdf
 
Describe one way in which you could use butterfly rearing in the fut.pdf
Describe one way in which you could use butterfly rearing in the fut.pdfDescribe one way in which you could use butterfly rearing in the fut.pdf
Describe one way in which you could use butterfly rearing in the fut.pdf
 
Critical Thinking Course...PhilosophyIn Chapter 8, we learned the .pdf
Critical Thinking Course...PhilosophyIn Chapter 8, we learned the .pdfCritical Thinking Course...PhilosophyIn Chapter 8, we learned the .pdf
Critical Thinking Course...PhilosophyIn Chapter 8, we learned the .pdf
 
Can I get a detailed answer on the following question. Bacterial ind.pdf
Can I get a detailed answer on the following question. Bacterial ind.pdfCan I get a detailed answer on the following question. Bacterial ind.pdf
Can I get a detailed answer on the following question. Bacterial ind.pdf
 
Another algebraic way to express a constraint is E_1 = E_2, where bot.pdf
Another algebraic way to express a constraint is E_1 = E_2, where bot.pdfAnother algebraic way to express a constraint is E_1 = E_2, where bot.pdf
Another algebraic way to express a constraint is E_1 = E_2, where bot.pdf
 
An infinitely long sheet of charge of width L lies in the xy -plane .pdf
An infinitely long sheet of charge of width L lies in the xy -plane .pdfAn infinitely long sheet of charge of width L lies in the xy -plane .pdf
An infinitely long sheet of charge of width L lies in the xy -plane .pdf
 
A study tracked incidence rates of a blood vessel disorder over time.pdf
A study tracked incidence rates of a blood vessel disorder over time.pdfA study tracked incidence rates of a blood vessel disorder over time.pdf
A study tracked incidence rates of a blood vessel disorder over time.pdf
 
A distant galaxy was found to have a redshift of 1.8. How far away i.pdf
A distant galaxy was found to have a redshift of 1.8. How far away i.pdfA distant galaxy was found to have a redshift of 1.8. How far away i.pdf
A distant galaxy was found to have a redshift of 1.8. How far away i.pdf
 

Recently uploaded

會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 

Need this in JAVA We have N numbers as an array, you need to find a .pdf

  • 1. Need this in JAVA We have N numbers as an array, you need to find a prefix array and a suffix array, which we can get the maximum x or value with all elements in them. Notice that for prefix[0, 1] and suffix[r, n - 1], do not intersect (I Solution // C++ program for a Trie based O(n) solution to find max // subarray XOR #include using namespace std; // Assumed int size #define INT_SIZE 32 // A Trie Node struct TrieNode { int value; // Only used in leaf nodes TrieNode *arr[2]; }; // Utility function tp create a Trie node TrieNode *newNode() { TrieNode *temp = new TrieNode; temp->value = 0; temp->arr[0] = temp->arr[1] = NULL; return temp; } // Inserts pre_xor to trie with given root void insert(TrieNode *root, int pre_xor) { TrieNode *temp = root; // Start from the msb, insert all bits of // pre_xor into Trie for (int i=INT_SIZE-1; i>=0; i--) { // Find current bit in given prefix
  • 2. bool val = pre_xor & (1<arr[val] == NULL) temp->arr[val] = newNode(); temp = temp->arr[val]; } // Store value at leaf node temp->value = pre_xor; } // Finds the maximum XOR ending with last number in // prefix XOR 'pre_xor' and returns the XOR of this maximum // with pre_xor which is maximum XOR ending with last element // of pre_xor. int query(TrieNode *root, int pre_xor) { TrieNode *temp = root; for (int i=INT_SIZE-1; i>=0; i--) { // Find current bit in given prefix bool val = pre_xor & (1<arr[1-val]!=NULL) temp = temp->arr[1-val]; // If there is no prefix with opposite // bit, then look for same bit. else if (temp->arr[val] != NULL) temp = temp->arr[val]; } return pre_xor^(temp->value); } // Returns maximum XOR value of a subarray in arr[0..n-1] int maxSubarrayXOR(int arr[], int n) { // Create a Trie and insert 0 into it TrieNode *root = newNode(); insert(root, 0); // Initialize answer and xor of current prefix int result = INT_MIN, pre_xor =0; // Traverse all input array element for (int i=0; i
  • 3. using namespace std; // Assumed int size #define INT_SIZE 32 // A Trie Node struct TrieNode { int value; // Only used in leaf nodes TrieNode *arr[2]; }; // Utility function tp create a Trie node TrieNode *newNode() { TrieNode *temp = new TrieNode; temp->value = 0; temp->arr[0] = temp->arr[1] = NULL; return temp; } // Inserts pre_xor to trie with given root void insert(TrieNode *root, int pre_xor) { TrieNode *temp = root; // Start from the msb, insert all bits of // pre_xor into Trie for (int i=INT_SIZE-1; i>=0; i--) { // Find current bit in given prefix bool val = pre_xor & (1<arr[val] == NULL) temp->arr[val] = newNode(); temp = temp->arr[val]; } // Store value at leaf node temp->value = pre_xor; } // Finds the maximum XOR ending with last number in // prefix XOR 'pre_xor' and returns the XOR of this maximum // with pre_xor which is maximum XOR ending with last element
  • 4. // of pre_xor. int query(TrieNode *root, int pre_xor) { TrieNode *temp = root; for (int i=INT_SIZE-1; i>=0; i--) { // Find current bit in given prefix bool val = pre_xor & (1<arr[1-val]!=NULL) temp = temp->arr[1-val]; // If there is no prefix with opposite // bit, then look for same bit. else if (temp->arr[val] != NULL) temp = temp->arr[val]; } return pre_xor^(temp->value); } // Returns maximum XOR value of a subarray in arr[0..n-1] int maxSubarrayXOR(int arr[], int n) { // Create a Trie and insert 0 into it TrieNode *root = newNode(); insert(root, 0); // Initialize answer and xor of current prefix int result = INT_MIN, pre_xor =0; // Traverse all input array element for (int i=0; i