Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- static class Node {
- int data;
- Node next;
- Node prev;
- public Node(int data) {
- this.data = data;
- }
- }
- static class CustomQueue {
- Node head, tail;
- public CustomQueue() {
- head = null;
- tail = null;
- }
- void enqueue(int x) {
- Node newNode = new Node(x);
- if (tail == null) {
- head = tail = newNode;
- return;
- }
- tail.next = newNode;
- newNode.prev = tail;
- tail = newNode;
- }
- int dequeue() {
- if (head == null) {
- return Integer.MIN_VALUE;
- }
- Node temp = head;
- head = head.next;
- if (head == null) {
- tail = null;
- } else {
- head.prev = null;
- }
- return temp.data;
- }
- boolean isEmpty() {
- return head == null;
- }
- }
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int N = scanner.nextInt();
- int searchElement = scanner.nextInt();
- int position = findPosition(N, searchElement);
- if (position == -1) {
- System.out.println("Element not found");
- } else {
- System.out.println("Element found at position " + position);
- }
- }
- public static int findPosition(int N, int searchElement) {
- CustomQueue queue = new CustomQueue();
- queue.enqueue(N);
- int index = 1;
- while (!queue.isEmpty()) {
- int element = queue.dequeue();
- if (element == searchElement) {
- return index;
- }
- index++;
- queue.enqueue(element + 1);
- queue.enqueue(element * 2);
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment