/* @ToDo If a Prime Number 9 => Not a Prime Number 7 => Prime Number */ #include <iostream> using namespace std ; int main (){ #ifndef ONLINE_JUDGE freopen ( "../asset/input.txt" , "r" , stdin ); freopen ( "../asset/output.txt" , "w" , stdout ); #endif // Code here!! int n ; cin >> n ; bool flag = true ; for ( int i = 2 ; i * i <= n ; i ++) if ( n % i == 0 ){ flag = false ; break ; ...
1. You are given a number n. 2. You are required to print the number of binary strings of length n with no consecutive 0's. Note: In this problem, you are given a number n. All we need to print is the number of binary strings of length n with no consecutive 0's For example: Sample Input: 3 Sample Output: 5 How 5? We have a total of eight binary numbers for length 3, out of which we have 5 numbers in which there are no consecutive zeros. Input Format A number n Output Format A number representing the number of binary strings of length n with no consecutive 0's. Constraints 0 < n <= 45 Sample Input 6 Sample Output 21 Solution: import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) throws Exception { // write your code here Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int dp[][] = new int[n+1][2]; dp[1][0] = 1; dp[1][1] ...
Write a java program to find the given number from the array of elements and display its position. If the number is not present in an array then display it as 0. Assume the position starts from 1. Sample Input 1 Enter the array size 4 Enter the values 9 32 17 4 Enter the number to find 17 Sample Output 1 3 Sample Input 2 Enter the array size 3 Enter the values 29 53 11 Enter the number to find 49 Sample Output 2 0 Result Description Summary of tests *Note: All the test cases might not have same weightage +------------------------------+ |4 tests run / 4 tests passed | +------------------------------+ TEST CASE PASSED
1. You are given a number n, which represents the length of a road. The road has n plots on it's each side. 2. The road is to be so planned that there should not be consecutive buildings on either side of the road. 3. You are required to find and print the number of ways in which the buildings can be built on both side of roads. Input Format A number n Output Format A number representing the number of ways in which the buildings can be built on both side of roads. Constraints 0 < n <= 45 Sample Input 6 Sample Output 441 Solution: 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); long n = scn.nextInt(); long ob = 1; long os = 1; for (int i = 2; i <= n; i++) { long nb = os; long ns = os + ob; ob = nb; ...
Comments
Post a Comment