Maximum XOR6 Feb 2025 | 3 min read Given an array of positive integers, find 2 elements such that their xor: a ^ b is maximum. Let us take an example to know what to be implemented. If the array elements are: 12, 15, 9. We need to find out the maximum xor value possible from these elements by taking any two integers from the given array. Possible xor's are: 12^15=3. 12^9=5. 15^9=6. In the above results, the maximum xor value is 6, so the answer is 6. Let us take another example to understand in detail. If the array elements are 13 11 35 3 32. If we apply xor for all possible combinations, we get a maximum xor value of 46. Now, we need to implement the code for this, i.e., to find the maximum xor value by using any two values from the array. CODE: Output: ![]() Explanation: Input Reading: The program starts by reading an integer t from the standard input. This represents the number of test cases. Test Case Loop: The program then enters a loop that iterates t times, representing each test case. Array Initialization: Inside the loop, a long, long integer n is read from the standard input, representing the size of an array. Array Population: A vector named vec is declared to store n integers. The program then enters another loop to read n integers and populate the vector. Bitwise XOR Operation: The program initializes an integer variable and sets it to 0. It then uses nested loops to iterate through all pairs of elements in the vector (vec), performing a bitwise XOR operation (^) on each pair. Finding Maximum XOR: The result of the XOR operation is compared with the current value of ans, and the maximum value is stored in ans. This process continues until all pairs of elements in the vector have been considered. Output: Finally, the maximum XOR value (ans) for the current test case is printed to the standard output. Let us discuss the time and space complexity: Time Complexity: Input Reading: Reading the integer t takes constant time. Test Case Loop: The test case loop runs t times, where t is the number of test cases. Array Population: The array/vector is populated with n elements, where n is the array size for each test case. Bitwise XOR Operation: The nested loops iterate through all pairs of elements in the array/vector. This results in a time complexity of O(n^2), where n is the array size for each test case. Output: Printing the result takes constant time. The nested loops dominate the overall time complexity, resulting in O(t * n^2), where t is the number of test cases, and n is the size of the array for each test case. Space Complexity: Input Variables: The input variables, including integer t and long integer n, determine the space complexity. These variables take constant space. Vector vec: The vector vec is used to store the array elements. Its space complexity is O(n), where n is the array size for each test case. Other Variables: Other variables, such as ans, are scalar and take constant space. The overall space complexity is O(n), where n is the maximum array size for any test case. In summary: Time Complexity: O(t * n^2) Space Complexity: O(n) Next TopicMerge-without-extra-space-problem-in-dsa |
Introduction: The goal of creating every possible combination of letters in a phone number is a fascinating issue in the field of algorithms and problem-solving. In addition to requiring a solid understanding of basic programming ideas, this challenge calls for an original method of assigning numbers to...
5 min read
Introduction: Data structures, fundamental elements of computer science, are essential for effectively organizing and managing data. Two fundamental ideas with unique properties and uses among the numerous data structures are trees and forests. We shall examine the key distinctions between trees and forests in data structures in...
4 min read
In computer programming, data structures provide methods for organizing and retrieving data. Each data structure comes with its set of functions, advantages and disadvantages. One used linear data structure that allows for last in, first out (LIFO) access is a stack. Elements are. Removed from the...
12 min read
? Introduction Heaps are fundamental data structures utilized in a variety of computer science applications, providing fast solutions to problems like priority queues, sorting, and graph algorithms. As we learn more about heap building, one fascinating issue arises: is a heap's structure unique? In this article, we will...
4 min read
In a binary search tree, the concept of floor and ceiling play a vital role in looking for the elements that may not exist in the tree. The floor of the given value in the binary search tree points out the biggest value that is smaller...
6 min read
Introduction is a set of virtual infrastructure additives that together enable various entities, including governments, corporations, and individuals, to interact and behavior transactions electronically. It is a set of open Application Programming Interfaces (APIs) and digital public items that objectives to unlock the economic primitives of...
4 min read
With inorder tree traversal, nodes are visited in the following order: left child, current node, then right child. Commonly, this sequence is referred to as "LNR." A systematic method for exploring and processing each node in a binary tree is provided by inorder tree traversal, which enables...
8 min read
Understanding Linked Lists and Matrices Linked Lists: In the domain of computer science, the linked list emerges as an important data structure, containing a complexity that we often overlook. It arranges its elements, designating each as a "node." Unlike the known traits of arrays, linked lists represent an...
5 min read
In this topic, we will see the vertical traversal of a binary tree. For the vertical traversal, we will calculate the horizontal distance. We will assign the horizontal distance to every node, and the horizontal distance could be from any side of the tree. In this...
8 min read
This article compares and contrasts the Hash table and an STL Map. How does the hash table work? Which data structure options can be used instead of a hash table if the number of inputs is small? HASH TABLE A hash table stores a value by calling a...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India