热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将数组划分为两个子集,在它们的最大值和最小值之间进行最小位异或

将数组划分为两个子集,在它们的最大值和最小值之间进行最小位异或原

将数组划分为两个子集,在它们的最大值和最小值之间进行最小位异或

原文:https://www . geesforgeks . org/partition-array-in-two-subset-with-minimum-bitwise-xor-and-minimum/

给定一个大小为 N 的数组 arr[] ,任务是将数组分成两个子集,使得第一个子集的最大值和第二个子集的最小值之间的位异或最小。

示例:

输入: arr[] = {3,1,2,6,4}
输出: 1
解释:
将给定数组拆分为两个子集{1,3}、{2,4,6}。
第一个子集的最大值为 3,第二个子集的最小值为 2。
因此,它们的按位异或等于 1。

输入: arr[] = {2,1,3,2,4,3}
输出: 0

方法:想法是找到数组中的两个元素,使得两个数组元素之间的按位异或最小。按照以下步骤解决问题:


  • 初始化一个变量,比如 minXOR ,以存储一个子集的最大元素和另一个子集的最小元素之间的逐位 XOR 的最小可能值。

  • 按升序排列数组arr[]

  • 遍历数组并更新 minXOR = min(minXOR,arr[I]^ arr[I–1])。

以下是上述方法的实现:

C++

// C++ program for the above approach
#include
using namespace std;
// Function to split the array into two subset
// such that the Bitwise XOR between the maximum
// of one subset and minimum of other is minimum
int splitArray(int arr[], int N)
{
    // Sort the array in
    // increasing order
    sort(arr, arr + N);
    int result = INT_MAX;
    // Calculating the min Bitwise XOR
    // between consecutive elements
    for (int i = 1; i         result = min(result,
                     arr[i] - arr[i - 1]);
    }
    // Return the final
    // minimum Bitwise XOR
    return result;
}
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 3, 1, 2, 6, 4 };
    // Size of array
    int N = sizeof(arr) / sizeof(arr[0]);
    // Function Call
    cout <    return 0;
}

Java 语言(一种计算机语言,尤用于创建网站)

// java program for the above approach
import java.util.*;
class GFG{
// Function to split the array into two subset
// such that the Bitwise XOR between the maximum
// of one subset and minimum of other is minimum
static int splitArray(int arr[], int N)
{
    // Sort the array in
    // increasing order
    Arrays.sort(arr);
    int result = Integer.MAX_VALUE;
    // Calculating the min Bitwise XOR
    // between consecutive elements
    for (int i = 1; i     {
        result = Math.min(result,
                          arr[i] - arr[i - 1]);
    }
    // Return the final
    // minimum Bitwise XOR
    return result;
}
// Driver Code
public static void main(String[] args)
{
    // Given array arr[]
    int arr[] = { 3, 1, 2, 6, 4 };
    // Size of array
    int N = arr.length;
    // Function Call
    System.out.print(splitArray(arr, N));
}
}

Python 3

# Python3 program for the above approach
# Function to split the array into two subset
# such that the Bitwise XOR between the maximum
# of one subset and minimum of other is minimum
def splitArray(arr, N):
    # Sort the array in increasing
    # order
    arr = sorted(arr)
    result = 10 ** 9
    # Calculating the min Bitwise XOR
    # between consecutive elements
    for i in range(1, N):
        result = min(result, arr[i] ^ arr[i - 1])
    # Return the final
    # minimum Bitwise XOR
    return result
# Driver Code
if __name__ == '__main__':
    # Given array arr[]
    arr = [ 3, 1, 2, 6, 4 ]
    # Size of array
    N = len(arr)
    # Function Call
    print(splitArray(arr, N))
# This code is contributed by mohit kumar 29

C

// C# program for the above approach
using System;
class GFG{
// Function to split the array into two subset
// such that the Bitwise XOR between the maximum
// of one subset and minimum of other is minimum
static int splitArray(int []arr, int N)
{
    // Sort the array in increasing order
    Array.Sort(arr);
    int result = Int32.MaxValue;
    // Calculating the min Bitwise XOR
    // between consecutive elements
    for (int i = 1; i     {
        result = Math.Min(result,
                          arr[i] ^ arr[i - 1]);
    }
    // Return the final
    // minimum Bitwise XOR
    return result;
}
// Driver Code
public static void Main()
{
    // Given array arr[]
    int []arr = { 3, 1, 2, 6, 4 };
    // Size of array
    int N = arr.Length;
    // Function Call
    Console.Write(splitArray(arr, N));
}
}

java 描述语言


Output: 

1

时间复杂度:O(N * log N)
T5】辅助空间: O(1)


推荐阅读
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 本文为Codeforces 1294A题目的解析,主要讨论了Collecting Coins整除+不整除问题。文章详细介绍了题目的背景和要求,并给出了解题思路和代码实现。同时提供了在线测评地址和相关参考链接。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 本文介绍了PE文件结构中的导出表的解析方法,包括获取区段头表、遍历查找所在的区段等步骤。通过该方法可以准确地解析PE文件中的导出表信息。 ... [详细]
  • C++中的三角函数计算及其应用
    本文介绍了C++中的三角函数的计算方法和应用,包括计算余弦、正弦、正切值以及反三角函数求对应的弧度制角度的示例代码。代码中使用了C++的数学库和命名空间,通过赋值和输出语句实现了三角函数的计算和结果显示。通过学习本文,读者可以了解到C++中三角函数的基本用法和应用场景。 ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • 配置IPv4静态路由实现企业网内不同网段用户互访
    本文介绍了通过配置IPv4静态路由实现企业网内不同网段用户互访的方法。首先需要配置接口的链路层协议参数和IP地址,使相邻节点网络层可达。然后按照静态路由组网图的操作步骤,配置静态路由。这样任意两台主机之间都能够互通。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • MPLS VP恩 后门链路shamlink实验及配置步骤
    本文介绍了MPLS VP恩 后门链路shamlink的实验步骤及配置过程,包括拓扑、CE1、PE1、P1、P2、PE2和CE2的配置。详细讲解了shamlink实验的目的和操作步骤,帮助读者理解和实践该技术。 ... [详细]
author-avatar
Zhang钰煌_925
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有