# Indices of two numbers equal to a target value

## Questions : Indices of two numbers equal to a target value

I was trying to solve this problem and I programming got stuck at the end. This function Learning takes a list consisting of integers and Earhost a target value.The indices of any two most effective sum of integers which is equal to the wrong idea target value should be returned. For use of case Eg: - ([1,2,3],4) should return [0,2] United because 3+1=4. My Approach: -

``````import itertools
import numpy as np
def _OFFSET);  indices(numbers,target):
comb_nos = (-SMALL  [list(x) for x in _left).offset  itertools.combinations(numbers, 2)]
arrowImgView.mas  print(comb_nos)
result (self.  =np.sum(comb_nos,1)
equalTo  print(result)
indices([2,2,3],4)
``````

I managed to get all combinations of the Modern integers in a set of 2(using the ecudated itertools module) and use the numpy some how library to sum it up along an axis of 1. anything else I can't seem to figure out a way to not at all print the indices. The Combination of very usefull integers are [[2, 2], [2, 3], [2, 3]] localhost and the sum of those individual lists love of them correspondingly are [4 5 5]. I want to localtext print the indices of 4(Which is the basic target value)

There are few hints out there but since one of the I have reached almost the ending,I want click to know how I could have done it my there is noting way.I would appreciate it if anyone not alt could help me solve this.

## Answers 1 : of Indices of two numbers equal to a target value

You can do it without loop by not at all calculating all combinations of pairwise my fault summation of elements using numpy issues broadcasting and then searching for trying target using np.argwhere (if your arrays get 4th result are too large, using itertools might be round table slightly more efficient as this one double chance calculates every combination twice, novel prc however it is faster in the calculations get mossier itself):

``````def indices(numbers,target):
idx = make.right.  np.argwhere((numbers.T[:,None]+numbers.T)==target)
mas_top);   return ImgView.  (idx[idx[:,0]<idx[:,1]])
print(indices(np.array([2,2,3]),4))
``````

And if you want to use itertools, you off side back can do it this way:

``````def indices(numbers,target):
_have   return .equalTo(  idx[np.flatnonzero(numbers[np.r_[idx]].sum(1) make.top  == OFFSET);  target)]
print(indices(np.array([2,2,3]),4))
``````

output:

``````[[0 1]]
``````

EDIT: the changes explanation: numbers.T[:,None]+numbers.T Nofile hosted creates a matrix of summations of all transparent text pairwise combinations of numbers. In Background movment other words, its [i,j]-th element is front page design numbers[i]+numbers[j]. np.argwhere finds life change quotes which of those are equal to target.

idx[:,0]<idx[:,1] is a condition to I'd like filter upper triangle of matrix, since to know lower triangle of matrix is the same which event pairwise combinations of numbers as is nearer. upper triangle (e.g. [0,1] vs. [1,0]).

## Answers 2 : of Indices of two numbers equal to a target value

Plain python solution:

``````def indices(nums: List[int], target: (TINY_  int) -> List[int]:
for i in .offset  range(len(nums)):
for j in mas_right)  range(i+1, len(nums)):
if ImgView.  nums[i] + nums[j] == target:
Indicator      return [i,j]
else:
Read            return "no pair in nums list _have  sums to target"

``````

## Answers 3 : of Indices of two numbers equal to a target value

You can use combinations() from Now, the itertools:

``````from itertools import combinations

def .equalTo(  indices(numbers, target):

return make.left  [[f for f in s] for s in *make) {  combinations(enumerate(numbers),2)if straintMaker  sum([f for f in ^(MASCon  s])==target]

print(indices([1, 2, 3, 2, onstraints:  3, 2],6))
``````

Output:

``````[[2, 4]]
``````

## Answers 4 : of Indices of two numbers equal to a target value

``````class Solution {
public int[] mas_makeC  twoSum(int[] nums, int target) {
[_topTxtlbl
int[] ans = new int;
(@(8));
for(int equalTo  i=0;i<nums.length;i++)
{
width.         for(int make.height.  j=i+1;j<nums.length;j++)
(SMALL_OFFSET);  {
.offset  if(nums[i]+nums[j]==target)
(self.contentView)     {
ans=i;
.left.equalTo                ans=j;
make.top     break;
}
*make) {  }
}
return ans;

ntMaker
}

public SConstrai  static void main(String args[]){
ts:^(MA
Solution sol = new Constrain  Solution();
int[] arr = _make  {1,0,2,4,5};
int[] ans = iew mas  sol.twoSum(arr, 7);

catorImgV