C Programming – HackerRank Solution | Reverse Array 1 |

(HackerRank) Write a Modular C Programming code to solve Reverse Array 1 , Given an array of ‘N’ elements reverse an array with the group size of ‘K’.

Given an array of ‘N’ elements reverse an array with the group size of ‘K’.

Input Format

First Line contains ‘N’ size of an array followed by N array elements in second line.

Third Line contains ‘K’, the reversal factor

Constraints

N>0 and N<=10^5

10^-5 <= a[i]<= 10^5

K>0

Output Format

Display the rotated array seperated by a space

Sample Input 0

-1

Sample Output 0

Invalid

Sample Input 1

8
1 2 3 4 5 6 7 8
5

Sample Output 1

5 4 3 2 1 6 7 8

Explanation 1

Since the reversal factor is 5, reverse the first five elements of the array, the array becomes 5 4 3 2 1, there are three remaining elements 6 7 and 8, and there are three elements less than K (5) so we don't reverse them

Sample Input 2

5
1 2 3 4 5
6

Sample Output 2

5 4 3 2 1

Explanation 2

If the K, is greater than N, then reverse the complete array

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

void calculate(int a[1000],int n,int k);
int main() {

   int n,a[1000],i,k;
    scanf("%d",&n);
    if(n>0 && n<=pow(10,5))
    {
        for(i=0;ipow(10,5))
            {
                printf("Invalid");
                exit(0);
            }
        }
            scanf("%d",&k);
            if(k<0)
            {
               printf("Invalid");
                exit(0);    
            }
            calculate(a,n,k);
        
    }
    else
    {
        printf("Invalid");
                exit(0);   
    }
    return 0;
}
void calculate(int a[1000],int n,int k)
{
    int i=0,j,b[1000],x,y,k1=k;
    if(k>n)
    {
        k=n;
    }
    j=k-1;
    while(k<=n)
    {
     for(;i

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

-1
Your Output (stdout)

Invalid
Expected Output

Invalid

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes - KLE Technological University







Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!