rashmi25a
BAN USERcurrent status
a b -1 c
final status
c -1 b a
Create hashmap with key as desired index and value as current index..
0 3
2 1
3 0
Step 1
Now in current array find out index of blank.. In this case it is 2
lookup map for desired location
(i.e 2 and value 1)
swap value from index 1 to 2
a -1 b c
Delete entry for 2 1 from map..
now map has following entry
0 3
3 0
Step 2
position of blank index = 1. Lookup 1 in the map
Result : entry not found
then read first entry from map (0 3)
then move value from position 3 to position 1
a c b -1
and update the map from 0 3 to 0 1
map entry looks like this
0 1
3 0
iterate the steps as long as map is not empty. when map is empty you will get the desired arrangement.
Use methods
add : to add a node at the end of linked list
pop : to remove node from desire index and return int
plug : to add new node at desired index
public class Node {
//data members
int inp_val;
Node next;
//constructor
public Node(int inpval){
this.inp_val=inpval;
this.next=null;
}
}
public class FbLL {
//data members
inp_val inp_val;
int size;
//Construct
public FbLL(Node inpval){
this.inp_val=inpval;
this.size = 1;
}
// add new Node
public void addCar(Node newnode){
Node temp = inp_val;
while (temp.next != null) {
temp=temp.next;
}
temp.next=newnode;
this.size++;
}
// pop a node
public int pop(int idx){
Node temp=inp_val;
for (int i=1; i <idx-1; i++){
temp=temp.next;
}
Node temp2=temp.next;
temp.next=temp.next.next;
return temp2.inp_val;
this.size—;
}
// plug a node
public void plug(int val, int idx){
Node temp=inp_val;
for (int i=1; i <idx-1; i++){
temp=temp.next;
}
Node temp2=temp.next;
temp.next=new Node(val);
temp.next.next=temp2;
this.size++;
}
}
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Node first = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
Node fourth = new Node(4);
Node fifth = new Node(5);
FbLL myll = new FbLL(first);
myll.add(second);
myll.add(third);
myll.add(fourth);
myll.add(fifth);
System.out.println("before shuffle");
System.out.println(myll.head.inp_val);
System.out.println(myll.head.next.inp_val);
System.out.println(myll.head.next.next.inp_val);
System.out.println(myll.head.next.next.next.inp_val);
System.out.println(myll.head.next.next.next.next.inp_val);
int val = myll.pop(4);
myll.plug(val, 2);
val = myll.pop(4);
myll.plug(val, 5);
System.out.println("after shuffle");
System.out.println(myll.head.inp_val);
System.out.println(myll.head.next.inp_val);
System.out.println(myll.head.next.next.inp_val);
System.out.println(myll.head.next.next.next.inp_val);
System.out.println(myll.head.next.next.next.next.inp_val);
}
}
apple
- rashmi25a December 29, 2016dimple
triple
staple
given string atdjfpstjpwietle..
word, index position, string length of the words.. now start taking characters from string match the char with word @ index position. after first character we increment the index position.
apple 0 5
dimple 0 6
triple 0 6
staple 0 6
a-tdjfpstjpwietle
apple 1 5
dimple 0 6
triple 0 6
staple 0 6
at-djfpstjpwietle
apple 1 5
dimple 0 6
triple 1 6
staple 0 6
atd-jfpstjpwietle
apple 1 5
dimple 1 6
triple 1 6
staple 0 6
atdjf-pstjpwietle
apple 1 5
dimple 1 6
triple 1 6
staple 0 6
atdjfp-stjpwietle
apple 2 5
dimple 1 6
triple 1 6
staple 0 6
atdjfpst-jpwietle
apple 2 5
dimple 1 6
triple 1 6
staple 2 6
atdjfpstjp-wietle
apple 3 5
dimple 1 6
triple 1 6
staple 2 6
atdjfpstjp-wietle
apple 3 5
dimple 1 6
triple 1 6
staple 2 6
atdjfpstjpwietle
apple 5 5
dimple 1 6
triple 1 6
staple 2 6