Reverse In Groups
Coding Problem Keys
Reverse In Groups
Problem Statement
Given an array of integers of size N and size of group R as input, the program must reverse and print every R elements in the array. Please fill in the lines of code to define the method reverseInGroup so that the program runs successfully.
Boundary Condition(s)
Input Format
Output Format
Example Input/Output 1
Input
Output
Explanation
Example Input/Output 2
Input
Output
Explanation
Note: Max Execution Time Limit: 5000 millisecs
Solution
Programming Language: C Language
#include <stdio.h> void reverseInGroup(int arr[], int N, int R); void main(){ int N, R, index; scanf("%d%d", &N, &R); int arr[N]; for(index=0; index<N; index++){ scanf("%d",&arr[index]); } reverseInGroup(arr, N, R); for(index=0; index<N; index++){ printf("%d ",arr[index]); } }
void reverseInGroup(int arr[],int N,int R){
int w=R; int arrp,i=0,j=w-1; while(j<N){ int wp=j,new=i; while(new<wp){ arrp=arr[new]; arr[new]=arr[wp]; arr[wp]=arrp; new+=1; wp-=1; }i+=w;j+=w; }return arr; }
// Published By PKJCODERS
Programming Language: C++ Language
#include<iostream> using namespace std; int main(){ int n,k,q; cin>>n; q=n; int arr[n]; for(int i=0;i<n;i++){ cin>>arr[i]; } cin>>k; for(int i=0;i<n;i+=k){ if(k+i<=n){ for(int j=0;j<k/2;j++) swap(arr[i+j],arr[i+k-1-j]); }else{ for(int j=i;j<n;j++){ swap(arr[j],arr[n-1]); n--; } } } for(int i=0;i<q;i++) cout<<arr[i]<<" "; }// Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments