Zycus Interview Question
Software Engineer / Developerstry this one:
import java.io.*;
class Mindist
{
public static void main(String args[]) throws java.io.IOException
{
int[] a=new int[5];
int min,i;
System.out.println("Enter the array of numbers: ");
for(i=0;i<a.length;i++)
{
a[i]=Integer.parseInt(System.console().readLine());
}
min=Math.abs(a[0]);
for(i=0;i<a.length;i++)
{
if(Math.abs(a[i])<min)
{
min=Math.abs(a[i]);
}
else
continue;
}
System.out.println("Minimum distance value is "+min);
}
}
This code has a bug.
For input -1,2,3,4,5 it will give output as 1,whereas this number is not present in the initial array
You dont need second scanning In the first scan itself if difference is a tie and number is positive then replace it with existing one if its negative dont replace
int retval = a[0],mindist = abs(a[0]);
for(i=1;i<n;i++){
if(abs(a[i]) > mindist) continue;
if(abs(a[i]) == mindist && a[i] >0) retval = a[i];
else{
mindist = abs(a[0]);
retval = abs(a[i])
}
}
This is working perfectly guys....
import java.util.Arrays;
public class FindNeartoZero {
public static void main(String[] args) {
int a[]={-10,-20,-30,-10,-50,-1,1};
Arrays.sort(a);
int i;
for(i=0;i<a.length;i++)
{
if(a[i]>0)
{
if(i==0)
{
System.out.println(a[i]);
System.exit(0);
}
int k=a[i-1];
System.out.println(Math.abs(k)<Math.abs(a[i])?k:a[i]);
System.exit(0);
}
}
if(i==a.length)
System.out.println(a[--i]);
}
}
<pre lang="" line="1" title="CodeMonkey10873" class="run-this">set data "-1 6 4 -5 9 -3 -2"
set min [ expr abs([ lindex $data 0 ]) ]
set result [ lindex $data 0 ]
for { set i 1 } { $i < [ llength $data ] } { incr i } {
if { [ expr abs([ lindex $data $i ]) ] < $min } {
set min [ expr abs([ lindex $data $i ]) ]
set result [ lindex $data $i ]
} elseif { [ expr abs([ lindex $data $i ]) ] == $min } {
set result [ expr abs([ lindex $data $i ])]
}
}
puts $result</pre><pre title="CodeMonkey10873" input="yes">
</pre>
Hi All, This code logic is working fine . In all conditions....
// number near to zero
$array = array(-87,-90,-67,45,67,-340,-45);
$array = array(-1,2,3,4,-2);
// initialize nearToZero variable with $array[0] absolute value
$nearToZero = abs($array[0]);
$pos = 0;
for($i=1;$i<count($array);$i++)
{
//check $array[$i] is bigger than $nearToZero or not
if(abs($array[$i])<abs($nearToZero))
{
$nearToZero = abs($array[$i]);
$pos = $i;
}
else if(abs($array[$i]) == abs($nearToZero))
{
if($array[$i]>0)
{
$pos = $i;
}
}
/*if($nearToZero<0)
{
if()
} */
}
echo ' near to zero :- '.$nearToZero.' position of number :- '.$pos;
public class NearestToZero {
public static void main(String[] args) {
int positiveNearest = Integer.MAX_VALUE;
int negativeNearest = ((-1) * Integer.MIN_VALUE);
int nearest = 0;
int[] a = { -5,6,2,-1,-2,1 };
for(int i = 0 ; i < a.length ; i++) {
if(a[i] > 0 && a[i] < positiveNearest) { // for positive integers
positiveNearest = a[i];
nearest = positiveNearest;
}else if(a[i] < 0 && a[i] > negativeNearest) { // for negative integers
negativeNearest = a[i];
nearest = negativeNearest;
}
}
if((positiveNearest + negativeNearest) == 0)
nearest = positiveNearest;
System.out.println(positiveNearest + " : " + negativeNearest);
System.out.println("Nearest to zero : " + nearest);
}
}
public class NearestToZero {
public static void main(String[] args) {
int positiveNearest = Integer.MAX_VALUE;
int negativeNearest = ((-1) * Integer.MIN_VALUE);
int nearest = 0;
int[] a = { -5,6,2,-1,-2,1 };
for(int i = 0 ; i < a.length ; i++) {
if(a[i] > 0 && a[i] < positiveNearest) { // for positive integers
positiveNearest = a[i];
nearest = positiveNearest;
}else if(a[i] < 0 && a[i] > negativeNearest) { // for negative integers
negativeNearest = a[i];
nearest = negativeNearest;
}
}
if((positiveNearest + negativeNearest) == 0)
nearest = positiveNearest;
System.out.println(positiveNearest + " : " + negativeNearest);
System.out.println("Nearest to zero : " + nearest);
}
}
public class CodePractice {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
/*1.An array contains a set of +ve and –ve numbers.
Find the number that is nearest to zero. If two numbers are equally near (like 2 and -2) then return +ve number.*/
int a[] = {-3, 8, 2, 7, -2, -9, 1};
int min = Integer.MAX_VALUE;
for(int i=0; i< a.length; i++){
if(a[i] < min && a[i]> 0){
min = a[i];
}
}
System.out.println(min);
}
}
Hello,
This code has bug...
This code will give wrong output for input array, say (-1,2,3,4,-2)
output for this input should be -1 but your code will give 2 as output.
Try again. Have a look on the code which I've posted having 3 likes on the top of this page.
Try this one....
- Ajay Narvekar August 14, 2013