crazy helix hackerrank solution code example
Example 1: crazy helix hackerrank solution
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int q = in.nextInt();
int[] arr = new int[n];
for(int ar_i = 0; ar_i < n; ar_i++){
arr[ar_i] = ar_i+1;
}
for(int ind=1;ind<=q+1;ind++){
int a = in.nextInt();
if(1==a){
reverseSubArray(arr, in.nextInt()-1, in.nextInt()-1);
}else if(2==a){
int b = in.nextInt();
for(int i=0;i<arr.length;i++){
if(arr[i]==b){
System.out.println("element "+ b +" is at position "+ (i+1));
break;
}
}
}else if(3==a){
int b = in.nextInt();
System.out.println("element at position " + b + " is " + arr[b-1]);
}
}
}
static void reverseSubArray(int arr[], int i, int j){
if(i>=j) return;
for(;i<j;i++,j--){
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
Example 2: crazy helix hackerrank solution
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int main()
{
int n,q,x,y,z,t;
scanf("%d %d",&n,&q);
int a[n];
for(int i=0;i<n;i++)
{
a[i]=i+1;
}
for(int j=0;j<q;j++)
{
scanf("%d",&x);
if(x==1)
{
int temp=0;
scanf("%d",&y);
scanf("%d",&z);
t=z-y+1;
y--;
z--;
for(int j=0;j<t/2;j++)
{
temp=a[y+j];
a[y+j]=a[z-j];
a[z-j]=temp;
}
else if(x==2)
{
int t=0;
scanf("%d",&y);
for(int i=0;i<n;i++)
{
if(a[i]==y)
{
t=i;
break;
}
}
printf("element %d is at position %d\n",y,t+1);
}
else
{
scanf("%d",&y);
printf("element at position %d is %d \n",y,a[y-1]);
}
}
return 0;
Example 3: crazy helix hackerrank solution
n,queries=map(int,input().split())
num=[i+1 for i in range(n)]
for i in range(queries):
l=list(map(int,input().split()))
if len(l)==3:
if l[1]==1:
num=num[:l[1]-1]+num[l[2]-1::-1]+num[l[2]:]//1
else:
num=num[:l[1]-1]+num[l[2]-1:l[1]-2:-1]+num[l[2]:]//1
elif l[0]==2:
print("element {0} is at position {1}".format(l[1],num.index(l[1])+1))//2
else:
print("element at position {0} is {1}".format(l[1],num[l[1]-1]))