首页 > 其他分享 >3131. Find the Integer Added to Array I

3131. Find the Integer Added to Array I

时间:2024-08-08 22:53:22浏览次数:8  
标签:Added each equal element integer Integer Array nums1 nums2

You are given two arrays of equal length, nums1 and nums2.

Each element in nums1 has been increased (or decreased in the case of negative) by an integer, represented by the variable x.

As a result, nums1 becomes equal to nums2. Two arrays are considered equal when they contain the same integers with the same frequencies.

Return the integer x.

Example 1:

Input: nums1 = [2,6,4], nums2 = [9,7,5]

Output: 3

Explanation:

The integer added to each element of nums1 is 3.

Example 2:

Input: nums1 = [10], nums2 = [5]

Output: -5

Explanation:

The integer added to each element of nums1 is -5.

Example 3:

Input: nums1 = [1,1,1,1], nums2 = [1,1,1,1]

Output: 0

Explanation:

The integer added to each element of nums1 is 0.

Constraints:

  • 1 <= nums1.length == nums2.length <= 100
  • 0 <= nums1[i], nums2[i] <= 1000
  • The test cases are generated in a way that there is an integer x such that nums1 can become equal to nums2 by adding x to each element of nums1.
class Solution {
public:
    int addedInteger(vector<int>& nums1, vector<int>& nums2) {
        sort(nums1.begin(), nums1.end());
        sort(nums2.begin(), nums2.end());
        return nums2[0] - nums1[0];
    }
};

标签:Added,each,equal,element,integer,Integer,Array,nums1,nums2
From: https://blog.csdn.net/Recursions/article/details/141038337

相关文章

  • Java | Integer强转Double错误
    一、问题复现引发java.lang.ClassCastException:classjava.lang.Integercannotbecasttoclassjava.lang.Double错误的示例代码:publicclassClassCastExceptionExample{publicstaticvoidmain(String[]args){Objectnumber=Integer.valueOf(10);......
  • array-value
    赛时想到了二分+trie但是却没有想到怎么维护啊看到异或,可以想位运算,trie和异或基。跟位运算肯定没啥关系,而异或基是选任意多个数进行异或,trie是两个数进行异或,所以这道题目用trie二分,考虑是否存在至少\(k\)个区间不超过\(mid\)check函数:我们枚举区间的右端点\(r\),找到一个最大......
  • new_d_array()函数接受一个int类型的参数和double类型的参数。该函数返回一个指针,指向
    /*下面是使用变参函数的一段程序:include<stdio.h>include<string.h>incude<stdarg.h>voidshow_array(constdoublear[],intn);double*new_d_array(intN,...);intmain(void){doublep1;doublep2;p1=new_d_array(5,1.2,2.3,3.4,4.5,5.6);p2=new_d_ar......
  • leetcode 1486. 数组异或操作 https://leetcode.cn/problems/xor-operation-in-an-arr
    1486.数组异或操作题目描述给你两个整数,n和start。数组nums定义为:nums[i]=start+2*i(下标从0开始)且n==nums.length。请返回nums中所有元素按位异或(XOR)后得到的结果。示例示例1:输入:n=5,start=0输出:8解释:数组nums为[0,2,4,6,8],其中(0^......
  • C. Even Subarrays
    原题链接题解这题用到的知识点很多,思维+前缀异或+容斥原理。1、题目告诉我们要找被除数个数是偶数个的异或和,那么什么数的被除数有偶数个?答案:非完全平方数。2、非完全平方数太多了,不好求。而我们又知道所有序列个数为(n+1)*n/2所以我们只要求出序列异或和为完全平方数......
  • 2024牛客暑期多校训练营7 C Array Sorting 题解
    乱搞非正解写法。分类讨论各种情况。降序排序对应交换即可数组个数小直接考虑相邻的交换其他都看做随机数据考虑结合前面情况,很容易想到,先把数组变成一个尽量有序的数组(每个元素和自己正确的位置相差不大)。最后再多次相邻交换,使得每个元素都在正确位置。把数组变成......
  • AtomicInteger
    importlombok.SneakyThrows;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.atomic.AtomicInteger;publicclassAtomicIntegerTest{privatestaticfinalAtomicIntegeratomicInteger=newAtomicInteger(0);@SneakyThrowspub......
  • Number of k-good subarrays
    我们发现,如果我们将满足题意的点在数轴上标出,那么我们可以获得若干个连续段。对于一个长度为\(l\)的连续段,他对答案的贡献就是\(\frac{l(l+1)}{2}\),我们把所有连续段的贡献加起来就得到了答案于是我们发现这个可以拆分成子问题,具体见这篇题解。\(sol(n-mx,k-1)\)就是拆分成的子问......
  • ArrayList和LinkList实现的比较
    一、ArrayList和LinkList实现的比较1.使用get()获取元素1.1ArrayList.get()​ 如果希望使用ArrayList的get(intindex)方法获取元素,实现可以简单地将这项任务委托给其内部数组:publicEget(intindex){rangeCheck(index);returnelementData(index);}​ 当然,......
  • Java集合:Collection and Map;ArrayList;LinkList;HashSet;TreeSet;HashMap;TreeMap;Iterator:
        集合介绍:                        是一组变量类型(容器),跟数组很像。一,引用集合的原因(必要性):                  A:数组的空间长度固定,一旦确定不可以更改。多了浪费,少了报错。          B:使用数......