-
Notifications
You must be signed in to change notification settings - Fork 7
/
94.c
52 lines (51 loc) · 1.23 KB
/
94.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*Given an integer array, write a function to reverse every set of 'k' numbers. Modify the same array without creating another array. Reverse the remaining elements even if it is less than 'k'.
Example:
Input: {2, 1 , 3 , 5 , 8 , 6 , 7 , 9} and k = 3
Output: {3 , 1 , 2 , 6 , 8 , 5 , 9 , 7}*/
#include<stdio.h>
int main()
{
int n,*input_array,i,k;
printf("Enter the array size\t");
scanf("%d",&n);
input_array=(int *)malloc(n*sizeof(int));
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&input_array[i]);
}
printf("Enter the value of K\t");
scanf("%d",&k);
for(i=0;i<n;i+=k)
{
int left_element = i;
int right_element=min(i+k-1,n-1);
//printf("The right element : %d",right_element);
while(left_element<right_element)
{
input_array[left_element]^=input_array[right_element];
input_array[right_element]=input_array[left_element]^input_array[right_element];
input_array[left_element]^=input_array[right_element];
left_element++;
right_element--;
}
}
for(i=0;i<n;i++)
{
printf("%d\t",input_array[i]);
}
free(input_array);
}
int min(int a,int b)
{
int minimum;
if(a<b)
{
minimum=a;
}
else
{
minimum=b;
}
return minimum;
}