Sort by

recency

|

513 Discussions

|

  • + 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

  • + 0 comments

    After updating to OxygenOS 15 (CPH2487_15.0.0.300), some OnePlus users are facing an issue where home screen text remains black, making dark wallpapers difficult to use. A simple workaround is to apply a lighter wallpaper or use a third-party launcher. This kind of issue doesn’t occur on the iPhone 16 Pro Max, which handles text contrast more effectively. I prefer that you visit on The Last Price store and purchase a brand new mobile, such as Samsung S24 and others Smart Watches in Qatar.

  • + 0 comments

    Scent has a unique way of influencing our mood, confidence, and presence. If you're someone who appreciates high-quality, long-lasting fragrances inspired by luxury brands, I recommend exploring AWScents. Their curated collection of premium perfumes offers both elegance and affordability, making it easy to express your identity through scent.