Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7185545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:26:19+00:00 2026-05-28T18:26:19+00:00

let say I have an array of this values: $arr = array(190, 2215, 2,

  • 0

let say I have an array of this values:

$arr = array(190, 2215, 2, 61);

after I run my code with 694 loops, I got this final values:

Array
(
    [0] => 696
    [1] => 696
    [2] => 696
    [3] => 696
)

how can I make the process faster and much better?? I only can increase or decrease the value by 1

Below are my codes.

<?php 
    echo '<pre>';
    $arr = array(190, 2215, 2, 61);
    sort($arr);
    print_r($arr);

    $arrLength = count($arr);
    $counter = 0;

    loop:
    $counter++;

    for($i=0; $i<$arrLength; $i++){
        if($arr[$i] < $arr[$arrLength-1] && $arr[$arrLength-1] != $arr[$arrLength-2]) {
            $arr[$i]++;
            $arr[$arrLength-1]--;
        }else if($arr[$i] < $arr[$arrLength-1] && $arr[$arrLength-1] == $arr[$arrLength-2]) {
            $arr[$i]++;
        }
     }

    if($arr[0] != $arr[$arrLength-1]) goto loop;

    print_r($arr);
    print_r($counter);
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:26:20+00:00Added an answer on May 28, 2026 at 6:26 pm

    Your code just finds a weighted average between the two largest values in an array, and then sets everything to it (Second largest element has weight of the number of elements - 1, the largest element has weight 1). You can do this much more easily and faster like this:

    <?php 
        echo '<pre>';
        $arr = array(190, 2215, 2, 61);
        sort($arr);
        print_r($arr);
    
        $arrLength = count($arr);
    
        // Find weighted average of second largest element and largest element in array:
        $avg = floor(($arr[$arrLength - 2] * ($arrLength - 1) + $arr[$arrLength - 1])/$arrLength);
    
        // Only need this line if you need the end value of counter from your code
        // $counter = $avg - $arr[0]; // (964 in this example)
        $arr = array_fill(0, $arrLength, $avg);
    
        print_r($arr);
    
    ?>
    

    Note: if all you need is the number (696) then you don’t need the array_fill line. Just use the $avg variable for whatever you need it for later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of values. Let's say the array is like this: [apple,
Let's say I have this $(document).ready(function() { var array = $.makeArray($('p')); $(array).appendTo(document.body); }); });
I have a 2d array, let's say like this : 2 0 8 9
Let's say I have array of bytes: byte[] arr = new byte[] { 0,
Let's say I have an array of lots of values (C++ syntax, sorry): vector<double>
Let's say I have an array with the following values: 0.7523262 0.9232192 1.5824928 5.2362123
I have a 2D bitmap-like array of let's say 500*500 values. I'm trying to
Let's say I have an array var test = new Array() the values in
Let say I have an array like: Array ( [0] => Array ( [Data]
Let's say I have an array, and I know I'm going to be doing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.