Created
October 22, 2017 23:00
-
-
Save MukulLatiyan/ea92208007c1f184af94a3ecedbe4fb2 to your computer and use it in GitHub Desktop.
Reverse array in groups(GeeksForGeeks)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Java Code for geeksforgeeks solution of Reverse array in groups problem given on gfg array school section. | |
import java.util.*; | |
import java.lang.*; | |
import java.io.*; | |
class GFG | |
{ | |
public static void main (String[] args) | |
{ | |
Scanner s = new Scanner(System.in); | |
int ntest = s.nextInt(); | |
while (ntest-- > 0){ | |
int size = s.nextInt(); | |
int[] arr = new int[size]; | |
for (int i = 0; i < size; i++){ | |
arr[i] = s.nextInt(); | |
} | |
int group_size = s.nextInt(), orig = group_size, count = 1; | |
for (int i = 0; i < size; i++){ | |
if (group_size >= size) | |
group_size = size; | |
if (group_size-- > orig * (count - 1)) | |
System.out.print(arr[group_size] + " "); | |
//prints->3 2 1 5 4 | |
else{ | |
group_size = orig * ++count; | |
i--; | |
} | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment