Skip to main content

A Competitive Programming Questions List In Java

Competitive Programming Questions List

  1. Is Number Prime?
  2. Print All Primes.
  3. Print Fibonacci Numbers Till N.
  4. Count Digits In A Number.
  5. Digits Of A Number.
  6. Reverse A Number.
  7. Inverse Of A Number.
  8. Rotate A Number.
  9. GCD And LCM.
  10. Prime Factorisation Of A Number.

Comments

Popular posts from this blog

Tic Tac Toe Python 3: The Standard Tic-Tac-Toe Game in Python 3

I just finished Tic tac toe game as my first python project. This is a very simple classic game, for beginners in programming tic-tac-toe game is the best choice. I am using Jupyter notebook, you can use any python ide like Pycharm, Spyder etc. Scenario of tic-tac-toe game: Your task is to write a simple program which pretends to play  Tic-tac-toe game with the user.  To make it easier for you. We have decided to simplify the game. Here are our assumptions. the computer should play a game using 'X' the user should play a game using 'O' the first move belongs to the computer, it always 'X' in the middle of the board.  all squares are numbered row by row, start with '1'. the user move is to enter the number of the square they choose, the number must be valid, that is it must be an integer, it must be greater than  0 and less than 10, and it cannot point to a field which is already occupied. the program checks the ga

Fibonacci Java

Question You are given a number n. You are required to print the nth element of the Fibonacci sequence. Note -> Notice precisely how we have defined the Fibonacci sequence 0th element -> 0 1st element -> 1 2nd element -> 1 3rd element -> 2 4th element -> 3 5th element -> 5 6th element -> 8 Input Format A number n Constraints 0 <= n <= 45 Sample Input 10 Sample Output 55 Program import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { // write your code here Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int ans = fib(n, new int[n + 1]); System.out.println(ans); } public static int fib(int n, int[] qb){ if(n == 0 || n == 1){ return n; } if(qb[n] != 0){ return qb[n]; } int fib1 = fib(n - 1, qb);

Knapsack Problem Java

Question You are given a number n, representing the count of items. You are given n numbers, representing the values of n items. You are given n numbers, representing the weights of n items. You are given a number "cap", which is the capacity of a bag you've. You are required to calculate and print the maximum value that can be created in the bag without overflowing its capacity. Note -> Each item can be taken 0 or 1 number of times. You are not allowed to put the same item again and again. Input Format A number n v1 v2 .. n number of elements w1 w2 .. n number of elements A number cap Output Format A number representing the maximum value that can be created in the bag without overflowing its capacity Constraints 1 <= n <= 20 0 <= v1, v2, .. n elements <= 50 0 < w1, w2, .. n elements <= 10 0 < cap <= 10 Sample Input 5 15 14 10 45 30 2 5 1 3 4 7 Sample Output 75 Program import java.io.*;

Count Sort In Java

Question You are given an array(arr) of integers. You have to sort the given array in increasing order using count sort. Input Format An Integer n arr1 arr2.. n integers Constraints 1 <= N <= 10000 0 <= arr[i] <= 10^8 Sample Input 5 7 -2 4 1 3 Sample Output -2 1 3 4 7 Program import java.io.*; import java.util.*; public class Main { public static void countSort(int[] arr, int min, int max) { //write your code here int[] frqa = new int[max - min + 1]; for(int i = 0; i < arr.length; i++){ int val = arr[i]; int pos = val - min; frqa[pos]++; } for(int i = 1; i < frqa.length; i++){ frqa[i] = frqa[i] + frqa[i - 1]; } int[] ans = new int[arr.length]; for(int i = arr.length - 1; i >= 0; i--){ int val = arr[i]; int pos = val - min; int idx = frqa[pos] - 1; ans[idx] = val; frqa[pos]--; } for(int i = 0; i < arr.length;

Patterns: Java Program To Print Patterns Of Numbers And Stars

Patterns are most important for interview point of view. To understand pattern problem you know only the basic tactic of how to solve pattern problem. In Java, it becomes a little bit easier to code patterns problems. In this article we discuss 20 basic pattern problem in java. Java Program: To Check If A Number Is Prime In Java Lets get start :) Patterns In Java 1. Left Triangle Star Pattern : First, let us begin with the basic and the commonly asked pattern program in  Java  i.e This is the basic pattern in java to write its code . Their is only two loops are used one for row and another for column. Let’s write the java code: import java.util.*; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); // write ur code here int n = scn.nextInt(); for (int i = 1; i <= n; i++){ for(int j = 1; j <= i; j++){ System.out.print("*\t

Radix Sort In Java

Question You are given an array(arr) of integers. You have to sort the given array in increasing order using radix sort. Input Format An Integer n arr1 arr2.. n integers Constraints 1 <= N <= 10000 0 <= arr[i] <= 10^8 Sample Input 5 7 2 4 1 3 Sample Output After sorting on 1 place -> 1 2 3 4 7 1 2 3 4 7 Program import java.io.*; import java.util.*; public class Main { public static void radixSort(int[] arr) { // write code here int max = Integer.MIN_VALUE; for(int val : arr){ if(val > max){ max = val; } } int exp = 1; while(exp <= max){ countSort(arr, exp); exp = exp * 10; } } public static void countSort(int[] arr, int exp) { // write code here int[] frqa = new int[10]; for(int i = 0; i < arr.length; i++){ int val = arr[i] / exp % 10; i

Quick Sort In Java

Question You are given an array(arr) of integers. You have to sort the given array in increasing order using quicksort. Input Format An Integer n arr1 arr2.. n integers Constraints 1 <= N <= 100000 -10^9 <= arr[i] <= 10^9 Sample Input 5 7 -2 4 1 3 Sample Output pivot -> 3 Swapping -2 and 7 Swapping 1 and 7 Swapping 3 and 4 pivot index -> 2 pivot -> 1 Swapping -2 and -2 Swapping 1 and 1 pivot index -> 1 pivot -> -2 Swapping -2 and -2 pivot index -> 0 pivot -> 4 Swapping 4 and 7 pivot index -> 3 pivot -> 7 Swapping 7 and 7 pivot index -> 4 -2 1 3 4 7 Program import java.io.*; import java.util.*; public class Main { public static void quickSort(int[] arr, int lo, int hi) { //write your code here if(lo > hi){ return; } int pivot = arr[hi]; int pidex = partition(arr, pivot, lo, hi); quickSort(arr, lo, pidex - 1); quickSort(arr, pidex + 1, hi); } public

Java Program: To Check If A Number Is Prime In Java

In this java program, we have one positive input and check whether the given number is prime or not. Read what is Prime Number . The simple case to check prime number we do, if (number % 2 == 0){ print("Not prime");} else { print("is prime");} But its complexity is much high if we use for loop . To decrease the complexity of the program read below question and solution. Tic Tac Toe Python 3: The Standard Tic-Tac-Toe Game in Python 3 Java Program: Question You've to check whether a given number is prime or not. A number "t" as input representing a count of input numbers to be tested. Number "n" as input "t" number of times. For each input value of n, print "prime" if the number is prime and "not prime" otherwise. Explanation and sample input Input Format : t n A number n .. t number of times Output Format : prime not prime not prime .. t number of times Constraint