Persistent Systems Interview Question
Country: India
The simplest way of solving this problem:
step1: Sort the numbers strictly by considering absolute values.(java program)
step2: Add first two elements of the sorted array
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] numbers=new int[n];
for(int i=0;i<n;i++)
{
numbers[i]=in.nextInt();
}
int temp;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (Math.abs(numbers[i]) > Math.abs(numbers[j]))
{
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
int answer=numbers[0]+numbers[1];
System.out.print(" "+answer);
The simplest way of solving this problem:
step 1: sort the array by considering absolute values.
step 2: Add first two elements of the sorted array.
code-
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] numbers=new int[n];
for(int i=0;i<n;i++)
{
numbers[i]=in.nextInt();
}
int temp;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (Math.abs(numbers[i]) > Math.abs(numbers[j]))
{
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
int answer=numbers[0]+numbers[1];
System.out.print(" "+answer);
}
The simplest way of solving this problem:
step 1: sort the array by considering absolute values.
step 2: Add first two elements of the sorted array.
code-
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] numbers=new int[n];
for(int i=0;i<n;i++)
{
numbers[i]=in.nextInt();
}
int temp;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (Math.abs(numbers[i]) > Math.abs(numbers[j]))
{
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
int answer=numbers[0]+numbers[1];
System.out.print(" "+answer);
}
I think this can be resolved by scan the array and keep the two numbers closest to 0. It will only need O(n). The python function:
def get_sum_of_2_numbers_close_to_0(a):
if type(a) is not list or len(a) < 2:
raise Exception("The input must be an array with at least 2 numbers")
import sys
min_num = [sys.maxsize]*2
for k in a:
if abs(k) < abs(min_num[1]):
min_num[1] = k
if abs(min_num[1]) < abs(min_num[0]):
min_num[0], min_num[1] = min_num[1], min_num[0]
return min_num[0]+min_num[1]
def sum_closest_to_zero_revised(a):
if len(a) <= 2:
return sum(a)
else:
closestOneDiff, closesTwoDiff = abs(a[0]-0), abs(a[1]-0)
if closestOneDiff < closesTwoDiff:
result = {"Smallest": a[0], "SecondSmallest": a[1]}
else:
result = {"Smallest": a[1], "SecondSmallest": a[0]}
for n in xrange(2,len(a)):
if abs(a[n] - 0) > abs(result["SecondSmallest"]-0):
pass
elif abs(a[n] - 0) <= abs(result["SecondSmallest"]-0) and abs(a[n] - 0) > abs(result["Smallest"]-0):
result["SecondSmallest"] = a[n]
else:
result["SecondSmallest"] = result["Smallest"]
result["Smallest"] = a[n]
return result
The simplest method to solve this problem:
Step 1: sort the array by taking absolute values.
Step 2: Add first two elements of the sorted array.
code-
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] numbers=new int[n];
for(int i=0;i<n;i++)
{
numbers[i]=in.nextInt();
}
int temp;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (Math.abs(numbers[i]) > Math.abs(numbers[j]))
{
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
int answer=numbers[0]+numbers[1];
System.out.print(" "+answer);
}
1.Add a 0 to the array.
2. Sort the array ,
3. Find the location of 0 in the sorted array
4. Find the elements closes to 0..can be before or after
5. Return their sum
There is a edge case that a 0 already exists in which case you would slightly modify the logic to find the position of nearest values. I have not coded that. Time efficiency O(nLgn) for the sort and O(n) for iterating over the array. space complexity O(n).
- APill March 02, 2016