Sort by

recency

|

515 Discussions

|

  • + 0 comments

    You are probably missing the case where a swap can occur at two different indices. Check swap at 2, visit website for reference, and consider both adjacent values and values that are far apart.

  • + 0 comments

    Almost Sorted is that feeling when everything around the house is running smoothly, but then something small goes off track and grabs attention. A squeaky hinge, a remote that refuses to work, or even a door that suddenly won’t close properly can really disrupt the flow of the day. That’s why services like garage door repair coral springs come in handy, and checking a trusted Website makes it even easier to find quick solutions. Having someone reliable to step in and handle it brings a sense of relief, letting everything get back to normal without stress. It’s those quick fixes and professional touches that make daily life feel more manageable. Almost Sorted is about keeping things balanced and knowing help is just around the corner.

  • + 0 comments
    /*
     * Complete the 'almostSorted' function below.
     *
     * The function accepts INTEGER_ARRAY arr as parameter.
     */
    
    void printVec(const vector<int>& v) {
        for (auto e: v) {
            cout << e << " ";
        }
        cout << endl;
    }
    
    
    int myswap(vector<int>& v, size_t i, size_t j) {
        int count = 0;
        while ((i < j) && (v[i] == j) && (v[j] == i)) {
            swap(v[i], v[j]);
            count++;
            i++;
            j--;
        }
        return count;
    }
    
    int check(const vector<int>& v) {
        for (size_t i = 0; i < v.size(); ++i) {
            if (v[i] != i) {
                return i;
            }
        }
        return -1;
    }
    
    void almostSorted(vector<int> arr) {
        size_t n = arr.size();
        vector<int> ix(n);
        iota(ix.begin(), ix.end(), 0);
        sort(
            ix.begin(),
            ix.end(),
            [&arr](size_t i, size_t j) { return arr[i] < arr[j]; }
        );
        // printVec(arr);
        // printVec(ix);
        
        int i = check(ix);
        if (i == -1) {
            cout << "yes" << endl;
            return;
        }
        
        size_t l = i;
        size_t r = ix[i];
        // cout << "l=" << l << " r=" << r << endl;
        
        int count = myswap(ix, l, r);
        // cout << "count=" << count << endl;
    
        i = check(ix);
        if (i == -1) {
            if (count == 1) {
                cout << "yes" << endl;
                cout << "swap " << l+1 << " " << r+1 << endl;
            } else {
                cout << "yes" << endl;
                cout << "reverse " << l+1 << " " << r+1 << endl;
            } 
        } else {
            cout << "no" << endl;
        }
        // printVec(ix);
    }
    
  • + 0 comments

    Here is my solution in PHP

    $original = $arr;
    $length = count($arr);
    $decresingIndex = 0;
    $decreasingLastIndex = 0;
    $decreasingArray =[];
    $max = 0;
    $output = 'yes';
    
    for ($i=1; $i < $length ; $i++) { 
       
        if($original[$i] > $original[$i-1])
        {
            continue;
        }
        else
        {
            //echo $i;
           
            $decresingIndex = $i-1;
            $decreasingArray[] = $original[$i-1];
            $max = $original[$i-1];
            break;
        }
      
    }
    
    for ($i=$decresingIndex+1; $i < $length ; $i++) { 
       
       if($original[$i-1] > $original[$i])
       {
          $decreasingArray[] = $original[$i];
    
       }
       else
       {
        $decreasingLastIndex = $i-1;
       
        break;
       }
        
    }
    
     $cc  = count($decreasingArray);
    
    
     if($cc == 0)
     {
     echo "yes\n";
     }
     else
     {
        if($decreasingLastIndex == 0)
     {
        if($cc == 2)
        {
            echo "yes\n";
            echo "swap ". ($decresingIndex + 1)." ". ($decresingIndex + 2)."\n";
    
        }
        else
        {
            echo "yes\n";
            echo "reverse  ". ($decresingIndex + 1)." ". ($decresingIndex + $cc)."\n";
    
        }
     }
     else
     {
        if ($original[$decreasingLastIndex+1] > $max) {
             if($cc == 2)
          {
           $output = "yes\n";
            $output .= "swap ". ($decresingIndex + 1)." ". ($decresingIndex + 2)."\n";
             $val = $original[$decresingIndex+1];
              for ($i=$decresingIndex-1; $i >= 0  ; $i--) { 
                  if ($original[$i] < $val) {
                      continue;
    
                  }
                  else
                  {
                    $output = 'no';
                    break;
                  }
              }
              echo $output;
            }
            else
            {
                $output = "yes\n";
                $output .=  "reverse ". ($decresingIndex + 1)." ". ($decresingIndex + $cc)."\n";
                
    
                  for ($i=$decresingIndex + $cc; $i < $length-1 ; $i++) { 
                       if ((int)$original[$i+1] > (int)$original[$i]) {
                           continue;
                       }
                       else
                       {
                          $output = 'no';
                         break;
                       }
                     }
                  echo $output;   
    
            }
        }
        else
        {
             $output = 'no';
             $swapIndex = 0;
          for ($i=$decresingIndex +1 ; $i < $length ; $i++) { 
              if ($original[$i] > $max) {
                   $output = "yes\n";
                   $output.= "swap ". ($decresingIndex + 1)." ". ($i)."\n";
                   $swapIndex = $i;
                   break;
              }
             
          }
          if ($swapIndex > 0) {
             for ($i=$swapIndex; $i < $length-1 ; $i++) { 
               if ($original[$i+1] > $original[$i]) {
                   continue;
               }
               else
               {
                 $output = 'no';
                 break;
               }
            }
          }
          
          echo $output;
        }
    
    
     }
    
     }
    
  • + 0 comments

    Here is problem solution in python java c++ c and Javascript - https://programmingoneonone.com/hackerrank-almost-sorted-problem-solution.html