Skip to main content

Posts

Showing posts from July, 2022

How to write a program in Java by taking user input using user controlled loop?

Java program for taking user input using Scanner and Store into Variables and Array  When you start learning Java programming, many times you need to get input from user and store into some variables or array. We can use Scanner class that presents in " java.util.Scanner " for taking user inputs. We can not store String value to int or any other data type value to other one, so Scanner class have particular methods for getting specific user input. Scanner class have many methods for taking user input i.e,  For taking int value = nextInt() For taking String value = next() For taking Float value = nextFloat() You can see all methods on Java doc : Java Scanner Class and its Methods Program 1 : Getting User Input using Scanner Class import java.util.Scanner; public class ScannerDemo {     public static void main(String[] args) {                         // Creating Scanner class object and initialize it           Scanner sc = new Scanner(System.in);         System.out.println(&quo

Find the Winner of the Circular Game Solution using Recursion | Josephus Problem

Find the Winner of the Circular Game Solution using Recursion with Explanation   Problem Description : There are n friends that are playing a game. The friends are sitting in a circle and are numbered from 1 to n in clockwise order . More formally, moving clockwise from the i th friend brings you to the (i+1) th friend for 1 <= i < n , and moving clockwise from the n th friend brings you to the 1 st friend. The rules of the game are as follows: Start at the 1 st friend. Count the next k friends in the clockwise direction including the friend you started at. The counting wraps around the circle and may count some friends more than once. The last friend you counted leaves the circle and loses the game. If there is still more than one friend in the circle, go back to step 2 starting from the friend immediately clockwise of the friend who just lost and repeat. Else, the last friend in the circle wins the game. Given the number of friends, n , and an integer k , re

How to Create Custom Linked List Class, Insert Values and Print it in Java

Implementing a Custom Linked List Class from Scratch in Java   In this article, we will seen How to create Node Class Create Insert method and Add data in Custom Linked list Class Print All elements of Linked list Linked list is linear data structure, means it stores values in linear manner but like Arrays it does not store elements at contiguous location. Linked list class represent as node and node contains value and reference of next and previous node. You can learn about Linked list class in details : List interface and Linked list class   Lets start with creating singly custom linked list class. Step 1 : Create Node class class Node {          //Data in the current node     int value;     //Reference for the next node     Node next;     Node(int value) {         this.value = value;     } } Step 2 : Create Custom Linked list class, Add insert() and printList() methods class CustomLinkedList {          Node head;     public void insert(int value) {         Node newNode = new Node(v

How to Check Given LinkedList is Palindrom or Not? | Recursive and Two Pointer Approach with Explanation

Check Palindrome Linked List using Recursive and Two Pointer Approach in Java with Explanation Problem Description : Given the head of a singly linked list, return true if it is a palindrome. Example 1 : Input : head = [1, 2, 2, 1] Output : true Example 2 : Input : head = [1, 2, 3, 2, 1] Output : true Example 3 : Input : head = [1, 2, 3, 4, 5, 6] Output : false Example 4 : Input : head = [6, 5, 4, 4, 6, 5] Output : false Solution 1 : Find LinkedList is Palindrome or not using Recursion in Java import java.util.Scanner; public class PalindromeLinkedList {     public static void main(String[] args) {                  Scanner sc = new Scanner(System.in);         System.out.println("Enter Size of LinkedList");         int size = sc.nextInt();                  System.out.println("Enter Data in LinkedList");                  // Creating CustomLinkedList object and storing Node         CustomLinkedList list = new CustomLinkedList();         for (int i = 0; i < size; i

How to Check if Given Number is Power of Two or Not | Java and Python

4 Ways to find Given Number is Power of Two or Not in Java and Python Problem Description : Given an integer n , return true if it is a power of two. Otherwise, return false . An integer n is a power of two, if there exists an integer x such that n == 2 x . Example 1: Input: n = 1 Output: true Explanation: 2 0 = 1 Example 2: Input: n = 16 Output: true Explanation: 2 4 = 16 Example 3: Input: n = 3 Output: false Lets jump on solution. we will see 4 solutions for this problem. Solution 1 : Check Number is Power of Two or Not using Recursive Approach in Java import java.util.Scanner; public class PowerOfTwo {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);                  System.out.println("Enter Number");         int number = sc.nextInt();                  boolean ans = checkPowerOfTwo(number);         System.out.println(ans);     }     private static boolean checkPowerOfTwo(int number) {         if (number == 1)