Sort by

recency

|

2192 Discussions

|

  • + 0 comments

    This problem is a simple exercise in reversing an array of integers. The function should take an array as input and return the array in reversed order. It helps practice basic array manipulation and indexing concepts. गोल्ड 365

  • + 0 comments
    function reverseArray(a) {
        var reversed = []
        for(var i =0; i < a.length; i++) {
            reversed[i] = a[a.length - 1 - i];
        }
        
        return reversed
    
    }
    
  • + 0 comments

    include

    using namespace std;

    void reverse(int arr[], int n) { // Change return type to void int start = 0; int end = n - 1; int temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } }

    int main() { int arr[] = {2, 3, 4, 5, 6, 7, 8}; int n = sizeof(arr) / sizeof(arr[0]);

    reverse(arr, n);  // Call the reverse function
    
    // Print the reversed array
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
    
    return 0;
    

    }

  • + 1 comment

    My Java soluton with o(n) time complexity and o(1) space complexity:

    public static List<Integer> reverseArray(List<Integer> a) {
            // use a pointer at the start and the end
            //flip while the left pointer is less than the right pointer
            int left = 0, right = a.size() - 1;
            while(left < right){
                int leftVal = a.get(left);
                int rightVal = a.get(right);
                a.set(left, rightVal);
                a.set(right, leftVal);
                left++;
                right--;
            }
            return a;
        }
    
  • + 0 comments
    function reverseArray(a) {
        // Write your code here
        let i = 0
        let j = a.length - 1
        
        while (j > i) {
            let temp = a[i]
            a[i] = a[j]
            a[j] = temp
            i = i + 1
            j = j - 1
        }
        
        return a;
    }