两个列表的最小索引总和_在最小时间复杂度中找到总和最接近零的对

两个列表的最小索引总和

Problem statement

问题陈述

Given an array with both positive and negative integers. Find the pair whose sum is closest to zero in minimum time complexity.

给定一个既有正整数又有负整数的数组。 找到总和最接近零的最小时间复杂度对。

Description:

描述:

Here we are going to see the algorithm with minimum time complexity to find a pair such that their sum is closest to 0.

在这里,我们将看到具有最小时间复杂度的算法,以找到对使得它们的总和最接近0。

Algorithm:

算法:

  1. Sort the array.

    对数组进行排序。

  2. Maintain two indexes, one at beginning, i, (i=0) & the other at the ending, j, (j=n-1, where n is the array length).

    保持两个索引,一个索引在开头i (i = 0) ,另一个索引在结尾j ( j = n-1 ,其中n是数组长度)。

  3. Maintain two variables indexP and indexN to keep track of the pairs which sum closest to 0.

    保持两个变量indexP和indexN来跟踪总和最接近0的对。

  4. Set a variable minsum to INT_MAX.

    将变量minsum设置为INT_MAX 。

  5. While (i<j)

    而(i <j)

    • If abs(current pair-sum)< abs(minsum)
    • If(current pair-sum>0)
  6. End loop

    结束循环

  7. indexP & indexN marks to the pair that sum closest to 0.

    indexP和indexN标记为总和最接近0的对。

  8. Print array[indexN] & array[indexP].

    打印array [indexN]和array [indexP] 。

Time complexity: O(nlogn) (O(logn) for sorting the array)

时间复杂度: O(nlogn)(用于数组排序的O(logn))

Space complexity: O(1)

空间复杂度: O(1)

该算法的C ++实现 (C++ implementation of the algorithm )

#include<bits/stdc++.h>
using namespace std;

void findpairs(int* a, int n){
	//sort the array using default sort library function, O(logn) generally
	sort(a,a+n);   
	int temp,i=0,j=n-1,minsum=INT_MAX,indexN=i,indexP=j;

	while(i<j){
		// current pair-sum
		temp=a[i]+a[j];  
		//if abs(current pair-sum)<abs(minsum)
		if(abs(temp)<abs(minsum)){      
			minsum=temp;
			indexN=i;
			indexP=j;
		}
		//if current pair-sum<0
		if(temp<0){  
			//Increment i
			i++;           
		}
		else		
		j--; // Decrement j
	}
	
	//print the pair
	cout<<"the pair is "<<a[indexN]<<","<<a[indexP]<<endl;    
}

int main(){

	int x,count=0,n;

	// enter array length
	cout<<"enter no of elements\n";        
	cin>>n;
	
	int* a=(int*)(malloc(sizeof(int)*n));
	
	//fill the array
	cout<<"enter elements................\n";  
	for(int i=0;i<n;i++)
		scanf("%d",&a[i]);

	findpairs(a,n);           

	return 0;	
}

Output

输出量

enter no of elements
9
enter elements................
11
-4
7 
31
-30
-6
8
17
-14
the pair is -30,31


翻译自: https://www.includehelp.com/algorithms/find-the-pair-whose-sum-is-closest-to-zero-in-minimum-time-complexity.aspx

两个列表的最小索引总和

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Kotlin 编写的解题代码: ```kotlin class Solution { fun sampleStats(count: IntArray): DoubleArray { val min = count.indexOfFirst { it > 0 }.toDouble() val max = count.indexOfLast { it > 0 }.toDouble() val sum = count.foldIndexed(0.0) { i, acc, c -> acc + i * c.toDouble() } val mean = sum / count.sum() val mid = (count.sum() + 1) / 2 var mode = 0.0 var modeCount = 0 for (i in count.indices) { if (count[i] > modeCount) { mode = i.toDouble() modeCount = count[i] } } var median = 0.0 var countSoFar = 0 var i = 0 while (i < count.size) { countSoFar += count[i] if (mid <= countSoFar) { median = i.toDouble() if (countSoFar == mid && count[i] > 1) { for (j in i + 1 until count.size) { if (count[j] > 0) { median = (median + j) / 2.0 break } } } break } i++ } return doubleArrayOf(min, max, mean, median, mode) } } ``` 算法思路如下: 我们可以使用 `indexOfFirst` 和 `indexOfLast` 找到最小和最大的非元素的索引,然后将它们转换为双精度浮点数。接下来,我们可以使用 `foldIndexed` 计算所有元素的总和,然后将其除以元素总数以获得平均值。 对于位数,我们需要找到间的元素。如果样本的元素数是奇数,则位数是排序后的间元素。否则,位数是排序后的两个元素的平均值。我们可以使用 `sum()` 算出样本的元素总数,然后计算出位数所在的位置。接下来,我们可以使用一个循环来计算位数。 对于众数,我们可以使用一个循环来找到出现次数最多的数字,并记录它们的出现次数。最后,我们只需要将所有这些值放入一个双精度浮点数数组并返回即可。 时间复杂度为 $O(256)$,空间复杂度为 $O(1)$,其 256 是样本元素的范围。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值