# Temperatures Puzzle Challenge help. (C#)

So I’m getting a little stuck From this point. About to try another function to check for negatives because I think that is the problem. Any suggestions?

``````using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
class Solution
{
public int Length {get;}
static void Main(string[] args)
{
bool areNegative = false;
string closestNumber;
int targetNumber = 0;
int n = int.Parse(Console.ReadLine()); // the number of temperatures to analyse
int[] newInputs = Array.ConvertAll(inputs, int.Parse);

//start of program
for (int i = 0; i < n; i++)
{
int t = int.Parse(inputs[i]);// a temperature expressed as an integer ranging from -273 to 5526
//sets clostest number to the number closest to 0 (working on negative bug)
closestNumber = Convert.ToString(findClosest(newInputs, targetNumber));
}

// Write an answer using Console.WriteLine()
// To debug: Console.Error.WriteLine("Debug messages...");

//Checks if numbers are negative and return absolute value new value
//Work in progress
static int isNegative (int[] oldValues, int[] newInputs, int[] newValues)
{
oldValues = newInputs;

}
//compares numbers to figure out which number is closest to target number (0)
static int findClosest (int[] newInputs, int targetNumber)
{

int n = newInputs.Length;
//corner cases
if(targetNumber < newInputs[0])
{
return newInputs[0];
}
if(targetNumber > newInputs[n - 1])
{
return newInputs[n - 1];
}
//binary search
int i = 0, j = n, mid = 0;
while (i < j)
{
mid = (i + j) / 2;
if (newInputs[mid] == targetNumber)
{
return newInputs[mid];
}
/* If target is less
than array element,
then search in left */
else if (targetNumber < newInputs[mid])
{
// If target is greater
// than previous to mid,
// return closest of two
if(mid > 0 && targetNumber > newInputs[mid - 1])
{
return getClosest(newInputs[mid - 1], newInputs[mid],targetNumber);
}
j = mid;
}
else
{
if(mid < n - 1 && targetNumber < newInputs[mid + 1])
{
return getClosest(newInputs[mid + 1], newInputs[mid], targetNumber);
}
i = mid + 1;
}

}
return newInputs[mid];
}
static int getClosest(int val1, int val2, int targetNumber)
{
if(targetNumber - val1 >= val2 - targetNumber)
{
return val2;
}
else
{
return val1;
}
}
closestNumber = Convert.ToString(findClosest(newInputs, targetNumber));
Array.ForEach(inputs,Console.Error.WriteLine);
Console.Error.WriteLine("Debug message: " + "number 'closest' to 0 =" + closestNumber);

Console.WriteLine(closestNumber);
}
}
``````

Isn’t it weird to do a binary search when your array is not sorted ?

Spoiler: You probably don’t need a binary search for this puzzle, you should for example look how to use Math.Abs instead