首页 后端开发 php教程 XOR Queries of a Subarray

XOR Queries of a Subarray

Sep 13, 2024 pm 10:17 PM

XOR Queries of a Subarray

1310. XOR Queries of a Subarray

Difficulty: Medium

Topics: Array, Bit Manipulation, Prefix Sum

You are given an array arr of positive integers. You are also given the array queries where queries[i] = [lefti, righti].

For each query i compute the XOR of elements from lefti to righti (that is, arr[lefti] XOR arr[lefti + 1] XOR ... XOR arr[righti] ).

Return an array answer where answer[i] is the answer to the ith query.

Example 1:

  • Input: arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
  • Output: [2,7,14,8]
  • Explanation: The binary representation of the elements in the array are:
  1 = 0001
  3 = 0011
  4 = 0100
  8 = 1000
登录后复制

The XOR values for queries are:

  [0,1] = 1 xor 3 = 2
  [1,2] = 3 xor 4 = 7
  [0,3] = 1 xor 3 xor 4 xor 8 = 14
  [3,3] = 8
登录后复制

Example 2:

  • Input: arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
  • Output: [8,0,4,4]

Constraints:

  • 1 <= arr.length, queries.length <= 3 * 104
  • 1 <= arr[i] <= 109
  • queries[i].length == 2
  • 0 <= lefti <= righti < arr.length

Hint:

  1. What is the result of x ^ y ^ x ?
  2. Compute the prefix sum for XOR.
  3. Process the queries with the prefix sum values.

Solution:

We can make use of the prefix XOR technique. Here's how it works:

Approach:

  1. Prefix XOR Array: We compute a prefix XOR array where prefix[i] represents the XOR of all elements from the start of the array up to index i. This allows us to compute the XOR of any subarray in constant time.

  2. XOR of a subarray:

    • To compute the XOR of elements between indices left and right:
      • If left > 0, we can compute the XOR from left to right as prefix[right] ^ prefix[left - 1].
      • If left == 0, then the result is simply prefix[right].

      This allows us to answer each query in constant time after constructing the prefix XOR array.

      Plan:

      1. Build the prefix XOR array.
      2. For each query, use the prefix XOR array to compute the XOR for the range [left_i, right_i].

      Let's implement this solution in PHP: 1310. XOR Queries of a Subarray

      <?php
      /**
       * @param Integer[] $arr
       * @param Integer[][] $queries
       * @return Integer[]
       */
      function xorQueries($arr, $queries) {
          ...
          ...
          ...
          /**
           * go to ./solution.php
           */
      }
      
      // Example 1
      $arr1 = [1, 3, 4, 8];
      $queries1 = [[0, 1], [1, 2], [0, 3], [3, 3]];
      print_r(xorQueries($arr1, $queries1)); // Output: [2, 7, 14, 8]
      
      // Example 2
      $arr2 = [4, 8, 2, 10];
      $queries2 = [[2, 3], [1, 3], [0, 0], [0, 3]];
      print_r(xorQueries($arr2, $queries2)); // Output: [8, 0, 4, 4]
      ?>
      
      登录后复制

      Explanation:

      1. Prefix XOR Construction:

        • The array prefix is built such that prefix[i] contains the XOR of all elements from arr[0] to arr[i].
        • For example, given arr = [1, 3, 4, 8], the prefix array will be [1, 1^3, 1^3^4, 1^3^4^8] or [1, 2, 6, 14].
      2. Answering Queries:

        • For each query [left, right], we compute the XOR of the subarray arr[left] to arr[right] using:
          • prefix[right] ^ prefix[left - 1] (if left > 0)
          • prefix[right] (if left == 0)

      Time Complexity:

      • Building the prefix array: O(n), where n is the length of the arr.
      • Processing the queries: O(q), where q is the number of queries.
      • Overall Time Complexity: O(n + q), which is efficient for the given constraints.

      This approach ensures we can handle up to 30,000 queries on an array of size up to 30,000 efficiently.

      Contact Links

      If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!

      If you want more helpful content like this, feel free to follow me:

      • LinkedIn
      • GitHub

      以上是XOR Queries of a Subarray的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

11个最佳PHP URL缩短脚本(免费和高级) 11个最佳PHP URL缩短脚本(免费和高级) Mar 03, 2025 am 10:49 AM

11个最佳PHP URL缩短脚本(免费和高级)

Instagram API简介 Instagram API简介 Mar 02, 2025 am 09:32 AM

Instagram API简介

在Laravel中使用Flash会话数据 在Laravel中使用Flash会话数据 Mar 12, 2025 pm 05:08 PM

在Laravel中使用Flash会话数据

构建具有Laravel后端的React应用程序:第2部分,React 构建具有Laravel后端的React应用程序:第2部分,React Mar 04, 2025 am 09:33 AM

构建具有Laravel后端的React应用程序:第2部分,React

简化的HTTP响应在Laravel测试中模拟了 简化的HTTP响应在Laravel测试中模拟了 Mar 12, 2025 pm 05:09 PM

简化的HTTP响应在Laravel测试中模拟了

php中的卷曲:如何在REST API中使用PHP卷曲扩展 php中的卷曲:如何在REST API中使用PHP卷曲扩展 Mar 14, 2025 am 11:42 AM

php中的卷曲:如何在REST API中使用PHP卷曲扩展

在Codecanyon上的12个最佳PHP聊天脚本 在Codecanyon上的12个最佳PHP聊天脚本 Mar 13, 2025 pm 12:08 PM

在Codecanyon上的12个最佳PHP聊天脚本

宣布 2025 年 PHP 形势调查 宣布 2025 年 PHP 形势调查 Mar 03, 2025 pm 04:20 PM

宣布 2025 年 PHP 形势调查

See all articles