Sort by

recency

|

498 Discussions

|

  • + 0 comments
    void almostSorted(vector<int> arr) {
        vector<int> s, e, copy = arr;
        map<int, int> mp;
        for(int i = 0; i < arr.size(); i++) mp[arr[i]] = i;
        sort(arr.begin(), arr.end());
        for(int i = 0; i < arr.size(); i++){
            if( arr[i] != copy[i] ){
                 s.push_back(i);
                 e.push_back(mp[arr[i]]);
            }
        }
        if(s.size() == 0) cout << "yes";
        if(s.size() == 2) cout << "yes" << endl << "swap " << s[0] + 1 << " " << e[0] + 1;
        else {
            reverse(s.begin(), s.end());
            if(s == e) cout << "yes" << endl << "reverse " << s[s.size() - 1] + 1 << " " << s[0] + 1;
            else cout << "no";
        }
    }
    
  • + 0 comments

    What does wrong with the code? Why it thinks that 5<4? code

  • + 0 comments

    Looks like you’re getting things organized! If you ever need a hand with upholstery cleaning services, I’ve got you covered. It’s amazing what a good clean can do for your space!

  • + 0 comments

    swift

    func isSorted(arr: [Int]) -> Bool {
        for index in 1 ... arr.count - 1 {
            if arr[index] < arr[index - 1] {
                 return false   
            }
        }
        return true
    }
    
    func reverseIt(arr: [Int], startIndex: Int, endIndex: Int) -> [Int] {
        let slicedArr: [Int] = Array(arr[startIndex ... endIndex])
        var arrTemp: [Int] = arr
        var index = slicedArr.count - 1
    
        while(index > -1) {
            arrTemp[startIndex + ((slicedArr.count - 1) - index)] = slicedArr[index]
            index -= 1
        }
        return arrTemp
    }
    
    func almostSorted(arr: [Int]) -> Void {
        // Write your code here
        var dict: [Int: Bool] = [:]
        var arrTemp: [Int] = arr
        var lastTrend: Bool?
        var resultWording: String = ""
        var decreaseIndex: [Int] = [Int]()
        var decreaseCount: Int = 0
        var increaseCount: Int = 0
        
        for index in 0 ... arr.count - 2 {
            let currElement: Int = arr[index]
            let nextElement: Int = arr[index + 1]
            let isUp: Bool = nextElement > currElement
            
            if (lastTrend != nil && lastTrend != isUp) || lastTrend == nil {
                dict[index + 1] = isUp
                lastTrend = isUp
            }
        }
        
        var arrayDict = dict.sorted(by: { $0.key < $1.key })
            
        if arrayDict.count == 1 && arrayDict.first!.value {
            print("yes")
            return
        }
        
        for index in 0 ... arrayDict.count - 1 {
            if !arrayDict[index].value {
                decreaseCount += 1
                decreaseIndex.append(arrayDict[index].key)
            }
            if arrayDict[index].value { increaseCount += 1 }
            
            if decreaseCount == 1 {
                arrTemp.swapAt(arrayDict[index].key - 1, arrayDict[index].key)
                resultWording = "yes\nswap \(arrayDict[index].key) \(arrayDict[index].key + 1)"
            }
            if decreaseCount == 2 {
                arrTemp.swapAt(decreaseIndex.first! - 1, decreaseIndex.last!)
                resultWording = "yes\nswap \(decreaseIndex.first!) \(decreaseIndex.last! + 1)"
            }
            if isSorted(arr: arrTemp) { 
                print(resultWording)
                return
            }
            
            if increaseCount == 2 { 
                arrTemp = reverseIt(arr: arr, startIndex: decreaseIndex.first! - 1, endIndex: arrayDict[index].key - 1)
                resultWording = "yes\nreverse \(decreaseIndex.first!) \(arrayDict[index].key)"
                
                if isSorted(arr: arrTemp) { 
                    print(resultWording)
                    return
                }
             }
    
            arrTemp = arr
        }
        
        print("no")
    }
    
  • + 0 comments

    Simple Python solution:

    def almostSorted(arr):
            # Write your code here
    
            sorted_arr = sorted(arr)
    
            if arr == sorted_arr:
                    print("yes")
                    return
    
            wrong = []
            for i, (num, sorted_num) in enumerate(zip(arr, sorted_arr)):
                    if num == sorted_num:
                            continue
                    wrong.append(i+1)
    
            if len(wrong) == 2:
                    print("yes")
                    print(f"swap {wrong[0]} {wrong[1]}")
                    return
    
            if arr[wrong[0]-1:wrong[-1]] == sorted_arr[wrong[0]-1:wrong[-1]][::-1]:
                    print("yes")
                    print(f"reverse {wrong[0]} {wrong[-1]}")
                    return
    
            print("no")
            return