{"id":712,"date":"2025-07-21T14:26:04","date_gmt":"2025-07-21T08:56:04","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=712"},"modified":"2025-07-21T14:26:05","modified_gmt":"2025-07-21T08:56:05","slug":"find-xor-of-numbers-from-l-to-r","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/","title":{"rendered":"Find XOR of numbers from L to R | Optimal Solution using Bit Manipulation"},"content":{"rendered":"\n<p>You are given two integers L and R, your task is to find the XOR of elements of the range [L, R].<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/find-xor-of-numbers-from-l-to-r\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input: <\/strong>\nL = 4, R = 8 \n<strong>Output:<\/strong>\n8 \n<strong>Explanation:<\/strong>\n4 ^ 5 ^ 6 ^ 7 ^ 8 = 8<\/pre>\n\n\n\n<p><strong>Your Task:<\/strong><\/p>\n\n\n\n<p>Your task is to complete the function&nbsp;<strong>findXOR()<\/strong>&nbsp;which takes two integers l and r and returns the XOR of numbers from l to r.<\/p>\n\n\n\n<p><strong>Expected Time Complexity:<\/strong>&nbsp;O(1).<br><strong>Expected Auxiliary Space:<\/strong>&nbsp;O(1).<\/p>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1&lt;=l&lt;=r&lt;=10<sup>9<\/sup><\/li>\n<\/ul>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-e6e045fb-a860-46f5-aa8b-4d55e6ec5f23\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"true\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/#0-solution-1-brute-force-approach-loop-and-xor\" style=\"\">Solution 1: Brute Force Approach (Loop and XOR)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/#1-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/#2-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/#3-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/#4-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/#5-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/#6-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/#7-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/#8-solution-2-optimal-approach-xor-pattern\" style=\"\">Solution 2: Optimal Approach (XOR Pattern)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/#9-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/#10-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/#11-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/#12-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/#13-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/#14-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/#15-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-xor-of-numbers-from-l-to-r\/#16-summary\" style=\"\">Summary<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-solution-1-brute-force-approach-loop-and-xor\">Solution 1: Brute Force Approach (Loop and XOR)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition\">Intuition<\/h3>\n\n\n\n<p>This is the straightforward way, like adding up numbers one by one! Start with a result of 0, then loop from L to R, XORing each number into the result. It&#8217;s simple, but for large R &#8211; L (up to 10^9), it would be too slow because looping billions of times isn&#8217;t efficient.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-detailed-approach\">Detailed Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialize Result<\/strong>: Set ans = 0.<\/li>\n\n\n\n<li><strong>Loop from L to R<\/strong>: For each number i, do ans = ans ^ i.<\/li>\n\n\n\n<li><strong>Return the Result<\/strong>: After the loop, ans is the XOR from L to R.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def findXOR(self, l, r):\n        ans = 0  # Initialize result\n        \n        # Loop through each number and XOR\n        for i in range(l, r + 1):\n            ans = ans ^ i  # XOR current number\n        \n        return ans\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">findXOR<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">l<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">r<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        ans = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># Initialize result<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Loop through each number and XOR<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(l, r + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            ans = ans ^ i  <\/span><span style=\"color: #6A9955\"># XOR current number<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> ans<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p>We start with ans as 0 (XOR identity). Then, for every number from l to r, we XOR it with ans. This builds up the total XOR step by step. It&#8217;s correct but not optimal for large ranges because the loop runs (r &#8211; l + 1) times.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through: L=1, R=3<\/p>\n\n\n\n<p><strong>Step-by-Step:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ans=0<\/li>\n\n\n\n<li>i=1: ans=0 ^ 1 = 1<\/li>\n\n\n\n<li>i=2: ans=1 ^ 2 = 3<\/li>\n\n\n\n<li>i=3: ans=3 ^ 3 = 0<\/li>\n<\/ul>\n\n\n\n<p><strong>Result:<\/strong>&nbsp;0<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>&nbsp;O(r &#8211; l + 1) &#8211; Worst case O(10^9) for large ranges, which is too slow.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>&nbsp;O(1) &#8211; No extra space needed.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-simplifying-it\">Simplifying It<\/h3>\n\n\n\n<p>This is like manually adding scores in a game one by one. It works for small games (small ranges), but for a huge tournament (large R &#8211; L), you&#8217;d be there forever. We need a smarter way!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8-solution-2-optimal-approach-xor-pattern\">Solution 2: Optimal Approach (XOR Pattern)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-intuition\">Intuition<\/h3>\n\n\n\n<p>XOR has a cool repeating pattern every 4 numbers! If you compute XOR from 1 to N, it depends on N % 4:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If N % 4 == 0: XOR = N<\/li>\n\n\n\n<li>If N % 4 == 1: XOR = 1<\/li>\n\n\n\n<li>If N % 4 == 2: XOR = N + 1<\/li>\n\n\n\n<li>If N % 4 == 3: XOR = 0<\/li>\n<\/ul>\n\n\n\n<p>To get XOR from L to R, it&#8217;s XOR(1 to R) ^ XOR(1 to L-1). This way, we cancel out the part before L and get exactly what we need in constant time!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-detailed-approach\">Detailed Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Helper Function<\/strong>: Create XORfrom1ToN(num) that returns XOR from 1 to num based on num % 4.<\/li>\n\n\n\n<li><strong>Edge Case<\/strong>: If num &lt; 0, XOR is 0 (for L=1, XOR(1 to L-1)=0).<\/li>\n\n\n\n<li><strong>Compute Result<\/strong>: Return XORfrom1ToN(r) ^ XORfrom1ToN(l &#8211; 1).<\/li>\n\n\n\n<li><strong>Why It Works<\/strong>: XOR is associative and commutative, and A ^ B ^ C ^ &#8230; ^ Z = (1 to Z) ^ (1 to (A-1)).<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    # Helper function to compute XOR from 1 to num\n    def XORfrom1ToN(self, num):\n        if num &lt; 0:\n            return 0  # XOR from 1 to 0 or negative is 0\n        if num % 4 == 1:\n            return 1\n        elif num % 4 == 2:\n            return num + 1\n        elif num % 4 == 3:\n            return 0\n        else:\n            return num\n    \n    # Main function to compute XOR from l to r\n    def findXOR(self, l, r):\n        # XOR(L to R) = XOR(1 to R) ^ XOR(1 to L-1)\n        return self.XORfrom1ToN(l - 1) ^ self.XORfrom1ToN(r)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Helper function to compute XOR from 1 to num<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">XORfrom1ToN<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">num<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> num &lt; <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># XOR from 1 to 0 or negative is 0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> num % <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\"> == <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">elif<\/span><span style=\"color: #D4D4D4\"> num % <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\"> == <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> num + <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">elif<\/span><span style=\"color: #D4D4D4\"> num % <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\"> == <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> num<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Main function to compute XOR from l to r<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">findXOR<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">l<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">r<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># XOR(L to R) = XOR(1 to R) ^ XOR(1 to L-1)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.XORfrom1ToN(l - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">) ^ <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.XORfrom1ToN(r)<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p>The XORfrom1ToN function uses the pattern of XOR prefixes. For example, XOR(1 to 4) = 4, XOR(1 to 5) = 1, and so on. By computing the prefix XOR up to R and up to L-1, their XOR gives exactly the XOR from L to R because the parts before L cancel out (A ^ B = C means A ^ C = B). This is super fast &#8211; no loops needed!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through: L=4, R=7<\/p>\n\n\n\n<p><strong>Step 1: XORfrom1ToN(3) (L-1=3)<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>3 % 4 = 3 \u2192 return 0<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 2: XORfrom1ToN(7)<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>7 % 4 = 3 \u2192 return 0<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 3: 0 ^ 0 = 0<\/strong><\/p>\n\n\n\n<p>Wait, but actual XOR(4^5^6^7) = 4^5=1, 1^6=7, 7^7=0? Wait, let&#8217;s calculate:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>4=100, 5=101 \u2192 4^5=001 (1)<\/li>\n\n\n\n<li>6=110 \u2192 1^6=111 (7)<\/li>\n\n\n\n<li>7=111 \u2192 7^7=000 (0)<\/li>\n<\/ul>\n\n\n\n<p>But according to pattern:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>XOR(1 to 3)=1^2^3=0<\/li>\n\n\n\n<li>XOR(1 to 7)=1^2^3^4^5^6^7<br>Let&#8217;s verify the pattern:<br>Actual XOR(1 to 7):<br>1^2=3, 3^3=0, 0^4=4, 4^5=1, 1^6=7, 7^7=0<br>Yes, 7%4=3 \u2192 0, correct.<\/li>\n<\/ul>\n\n\n\n<p>But actual 4^5^6^7:<br>Let&#8217;s calculate: 4^5=1, 1^6=7, 7^7=0<br>But 0 ^ 0 = 0, matches!<\/p>\n\n\n\n<p>Another example: L=1, R=3<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>XOR(1 to 0)=0<\/li>\n\n\n\n<li>XOR(1 to 3)=0 (3%4=3\u21920)<\/li>\n\n\n\n<li>0 ^ 0 = 0, and 1^2^3=0, correct.<\/li>\n<\/ul>\n\n\n\n<p>One more: L=2, R=4<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>XOR(1 to 1)=1 (1%4=1\u21921)<\/li>\n\n\n\n<li>XOR(1 to 4)=4 (4%4=0\u21924)<\/li>\n\n\n\n<li>1 ^ 4 = 5<br>Actual: 2^3^4 = 2^3=1, 1^4=5, correct!<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>&nbsp;O(1) &#8211; Constant time, no loops!<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>&nbsp;O(1) &#8211; No extra space needed.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-simplifying-it\">Simplifying It<\/h3>\n\n\n\n<p>This is like knowing a secret formula for summing numbers quickly instead of adding one by one. The pattern repeats every 4 numbers, so we can jump straight to the answer. For L to R, it&#8217;s like subtracting the XOR up to L-1 from the XOR up to R using XOR properties. Genius for large ranges!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"16-summary\">Summary<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Approach<\/th><th>Time<\/th><th>Space<\/th><th>Difficulty<\/th><th>Best For<\/th><\/tr><\/thead><tbody><tr><td>Brute Force (Loop)<\/td><td>O(r &#8211; l + 1)<\/td><td>O(1)<\/td><td>Easy<\/td><td>Small ranges<\/td><\/tr><tr><td>Optimal (Pattern)<\/td><td>O(1)<\/td><td>O(1)<\/td><td>Medium<\/td><td>Large ranges \/ Interviews<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The&nbsp;<strong>optimal pattern approach<\/strong>&nbsp;is preferred because it runs in constant time, perfect for constraints like L and R up to 10^9. The brute force is easy to understand but too slow for big inputs.<\/p>\n\n\n\n<p>This problem shows the beauty of bitwise patterns, once you learn the cycle every 4 numbers, you can solve it instantly! Practice with small ranges to verify the pattern. If you have questions, drop them below. Happy coding! \ud83d\ude80<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given two integers L and R, your task is to find the XOR of elements of the range [L, R]. Here&#8217;s the [Problem Link] to begin with. Example: Input: L = 4, R = 8 Output: 8 Explanation: 4 ^ 5 ^ 6 ^ 7 ^ 8 = 8 Your Task: Your task<\/p>\n","protected":false},"author":1,"featured_media":713,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[31,19],"class_list":{"0":"post-712","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-bit-manipulation","10":"tag-medium"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/find-XOR-of-numbers-from-L-to-R-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/712","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=712"}],"version-history":[{"count":3,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/712\/revisions"}],"predecessor-version":[{"id":718,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/712\/revisions\/718"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/713"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}