Jump to content

balance numbers in given arrray and input value without changing the largest number

- - - - -

  • Please log in to reply
1 reply to this topic

#1
java_geek

java_geek

    Newbie

  • Members
  • Pip
  • 1 posts
i have made a program in java *** groovy to balance an array by modifying the values (except the largest one) according to a given input which is a number . i have written the following code but i also have to include the functionality in which the difference between the smallest and largest element should also gets minimized ..so plz help guys??

def input;
def diff , rem;
def extra ,each
def t,k;
def t1;
int  j;
result_final = new SimpleResults();
def a =[0,0,0,0]
def ind =[0,0,0,0]
int i=0;
while(res_set.next())                  // for converting result set into array list
{ if(i>=0 && i<4)
{   ind[i] = res_set.getValue("col1");
    a[i] = res_set.getValue("col2");
    ++i;
    }
    else 
    {
    input= res_set.getValue("col2");
    }
} 

  for(i=0; i<4; i++)                  
  {
   for(j=1; j<(4-i); j++)
   {
    if(a[j-1]>a[j])
    {
       t = a[j-1];
       t1 = ind[j-1];
       a[j-1]=a[j];
       ind[j-1]=ind[j];
       a[j]=t;
       ind[j]=t1;
       }
  }
  }
  i=0;
 
diff =a[3]-a[0];                      
if (diff <= input)
{
     a[0]=a[0]+ diff;
    rem = input-diff;
    for(j = 0;j < 4;j++)
    {
        if((j!= 0)&&(j!= 3)&&(rem > 0))
        {
            diff = a[3]-a[j];
            if(diff <= rem)
            {
                 a[j]+=diff;
                 rem = rem-diff;
            }
            else
            {   a[j]+=rem;
                 rem=0;
            }
        }
    }
}
else
{
    a[0]+=input;
}


if (rem > 0)                                 // for  allocating remaining value
{
    if (rem == 1)
    {
        a[0] += 1;
    }
    else if (rem == 2)
    {
        a[0] += 1;
        a[1] += 1;
    }
    else if (rem == 3)
    {
        a[0] += 1;
        a[1] += 1;
        a[2] += 1;
    }
    else if (rem >= 4)
    {
        extra = rem%4 ;
        rem=rem-extra;
        each =rem/4;
         for(k = 0;k<4;k++)
        {
            a[k] +=each;
        }
        if (extra > 0)                                 // for  allocating remaining value
{
    if (extra == 1)
    {
        a[0] += 1;
    }
    else if (extra == 2)
    {
        a[0] += 1;
        a[1] += 1;
    }
    else if (extra == 3)
    {
        a[0] += 1;
        a[1] += 1;
        a[2] += 1;
    } 
    }
}
}
//its working properly but not following the second criteria

Edited by WingedPanther, 06 September 2011 - 04:34 AM.


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Moved to the correct forum, and code tags added.

This code cannot be compiled, as is, as Java. You have to have a main method, a class, etc.

You also haven't given us the details of what it's supposed to do. It accepts input, does something to minimize something based on... huh?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users