03/02/2023

Day 7 Arrays Hackerrank Solution in C & C++ | 30 Days of Code

Write a Program to Day 7 Arrays Hackerrank Solution in C and C++ with Complete Explanation. This is a very simple problem, here we need to only reverse the array and print the elements of the array. As we know the array index starts with zero to n-1. here we just reverse the array by first printing the array's last index and then the second last index like n, n-1, n-2,..................... up to (greater or equal) 0 (Zero).

Day 7 Arrays Hackerrank Solution in C and C++

Example: suppose an array with 5 elements is 10, 20, 30, 40, 50 and the index of the array is 0, 1, 2, 3, 4 so first print index 4 and element 50 then index 3 and element 40 and so on up to index 0 and element 10. Check the below picture its shows how exactly the array is printing the elements in reverse order.

All solutions provided here are in C++ (CPP). If you want these solutions in C, and Java comments below or send a mail with your query like " day n solution in C / C++ / Java. If you want 30 days solution (All previous solutions from day 0 ) from Day 0 please check the below link. You can also find more programs below in this post. Check the below solution with the full explanation (explanation in simple language).


Submit Your Solution Here: Click Here

Hackerrank C++ Array Solution


#include <bits/stdc++.h>

using namespace std;

vector < string > split_string(string);

int main() {
  int n;
  cin >> n;
  cin.ignore(numeric_limits < streamsize > ::max(), '\n');

  string arr_temp_temp;
  getline(cin, arr_temp_temp);

  vector < string > arr_temp = split_string(arr_temp_temp);

  vector < int > arr(n);

  for (int i = 0; i < n; i++) {
    int arr_item = stoi(arr_temp[i]);

    arr[i] = arr_item;
  }

  for (int i = n - 1; i >= 0; i--) {
    cout << arr[i] << " ";
  }

  return 0;
}

vector < string > split_string(string input_string) {
  string::iterator new_end = unique(input_string.begin(), input_string.end(), [](const char & x,
    const char & y) {
    return x == y and x == ' ';
  });

  input_string.erase(new_end, input_string.end());

  while (input_string[input_string.length() - 1] == ' ') {
    input_string.pop_back();
  }

  vector < string > splits;
  char delimiter = ' ';

  size_t i = 0;
  size_t pos = input_string.find(delimiter);

  while (pos != string::npos) {
    splits.push_back(input_string.substr(i, pos - i));

    i = pos + 1;
    pos = input_string.find(delimiter, i);
  }

  splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));

  return splits;
}

Day 7 Arrays Hackerrank Solution in C


#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
 int size;
scanf("%d", &size);
int arr[size];
int m=0;
// Now while loop will read all the integers ignoring white spaces.
while(m<size && scanf("%d",&arr[m])==1)
    m++;

int x=0;
int y=size-1;
int temp;
//Exchanging or Swapping the first and last values and then second and 2nd last.so on
while(x<y)
{
    temp=arr[x];
    arr[x]=arr[y];
    arr[y]=temp;
    x++;
    y--;
}
//printing the  single space seperated array values.
for(int k=0;k<size;k++)
{
    printf("%d ", arr[k]);
}
return 0;
}

The Output of Day 7 Arrays Hackerrank Solution in C and C++


The Output of Day 7 Arrays Hackerrank Solution in C and C++

Similar to 30 Days of Code


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: