We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. C++
  3. Introduction
  4. Arrays Introduction
  5. Discussions

Arrays Introduction

Problem
Submissions
Leaderboard
Discussions

Sort 1216 Discussions, By:

recency

Please Login in order to post a comment

  • vm097363
    2 days ago+ 1 comment

    include

    include

    include

    include

    include

    using namespace std; void reversearrays(int arr[], int n) { int s = 0; int e = n - 1; while (s <= e) { swap(arr[s], arr[e]); s++; e--; }

    } void printarray(int arr[], int n) { for (int i = 0; i < n; i++) { cout << arr[i] << ' '; } }

    int main() { int n; cin >> n; int* arr = new int[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; }

    reversearrays(arr, n);
    cout << endl;
    cout << "Array elements after reversing " << endl;
    printarray(arr, n);
    return 0;
    

    }

    0|
    Permalink
  • DylJFern
    2 days ago+ 0 comments

    //Dynamic allocation of an array

    int *arrPtr{}, userInput{};
    
    cin >> userInput; 
    arrPtr = new int[userInput]; //allocate array at runtime
    
    for(size_t i{1}; i<=userInput; i++) { 
    	cin >> arrPtr[i-1]; //store elements starting at index 0
    }
    
    for(size_t j{1}; j<=userInput; j++) {
    	cout << arrPtr[userInput-j] << " ";
    }
    
    delete [] arrPtr; //free allocated storage (good practice)
    

    //Pre-defined array with a fixed size (based on constraints)

    int userInput{}, arrFixed[1000]{};
        
    cin >> userInput;
        
    for(size_t i{1}; i<=userInput; i++) {
    		cin >> arrFixed[i-1]; 
    }
    
    for(size_t j{1}; j<=userInput; j++) {
    		cout << arrFixed[userInput-j] << " ";
    }
    
    0|
    Permalink
  • uixarsh
    4 days ago+ 0 comments

    C++ 20

    include

    using namespace std;

    int main() { int N, arr[1000];

    cin >> N;
    
    for (int i = 0; i < N; i++) {
    cin >> arr[i];
    }
    
    
    for (int i = N-1; i >= 0; i--) {
    cout << arr[i] << " ";
    }
    
    return 0;
    

    }

    0|
    Permalink
  • Diwakar_181330
    5 days ago+ 0 comments

    int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int n; cin>>n; int arr[n]; for (int i =0; i>arr[i]; } for(int i = n-1; i >= 0; i--){ cout<

    return 0;
    

    }

    0|
    Permalink
  • saimoom1362
    7 days ago+ 0 comments

    C++20:

    #include <iostream>
    using namespace std;
    
    int main() {
        int N, arr[1000];
        
        cin >> N;
        
        for (int i = 0; i < N; i++) {
        cin >> arr[i];
        }
        
        for (int i = 0; i < N / 2; i++) {
        int temp = arr[i];
        arr[i] = arr[N - 1 - i];
        arr[N - 1 - i] = temp;
        }
        
        for (int i = 0; i < N; i++) {
        cout << arr[i] << " ";
        }
        
        return 0;
    }
    
    0|
    Permalink
Load more conversations

Need Help?


View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy