Skip to main content

Posts

Showing posts with the label CodingDare

Arrange Buildings

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;       os = ns;     }     long total = ob + os;     System.out.println(total * total);  } }

Fractional Knapsack

1. You are given a number n, representing the count of items. 2. You are given n numbers, representing the values of n items. 3. You are given n numbers, representing the weights of n items. 3. You are given a number "cap", which is the capacity of a bag you've. 4. You are required to calculate and print the maximum value that can be created in the bag without overflowing it's capacity. Note1: Items can be added to the bag even partially. But you are not allowed to put same items again and again to the bag. Input Format A number n v1 v2 .. n number of elements w1 w2 .. n number of elements A number cap Output Format A decimal number representing the maximum value that can be created in the bag without overflowing it's capacity 1. You are given a number n, representing the count of items. 2. You are given n numbers, representing the values of n items. 3. You are given n numbers, representing the weights of n items. 3. You are given a number "cap", which i

Unbounded Knapsack

1. You are given a number n, representing the count of items. 2. You are given n numbers, representing the values of n items. 3. You are given n numbers, representing the weights of n items. 3. You are given a number "cap", which is the capacity of a bag you've. 4. You are required to calculate and print the maximum value that can be created in the bag without      overflowing it's capacity. Note: Each item can be taken any number of times. You are 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 it's 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 100 Solution: import java.io.*; import java.util.*; pu

Zero One Knapsack | Recursion

 1. You are given a number n, representing the count of items. 2. You are given n numbers, representing the values of n items. 3. You are given n numbers, representing the weights of n items. 3. You are given a number "cap", which is the capacity of a bag you've. 4. You are required to calculate and print the maximum value that can be created in the bag without       overflowing it's 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 it's 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 Solution: import java.io.*; import java.util.*; public class Main {

Climb Stairs

1. You are given a number n, representing the number of stairs in a staircase. 2. You are on the 0th step and are required to climb to the top. 3. In one move, you are allowed to climb 1, 2 or 3 stairs. 4. You are required to print the number of different paths via which you can climb to the top. Input Format A number n Output Format A number representing the number of ways to climb the stairs from 0 to top. Constraints 0 <= n <= 20 Sample Input 4 Sample Output 7 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();         System.out.println(WaysUp(n));     }     public static int WaysUp(int n){         if(n == 0)  return 0;         else if(n<0)    return -1;         int[] A = new int[n+1];         A[0] = 1;         for(int i=1;i<=n;i++){             A[i] = A[i-1];             if(i>= 2)    

Fibonacci-dp

 1. You are given a number n. 2. You are required to print the nth element of fibonnaci sequence. Note -> Notice precisely how we have defined the fibonnaci 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 Output Format A number representing the nth element of fibonnaci sequence Constraints 0 <= n <= 45 Sample Input 10 Sample Output 55 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[] A = new int[n+1];     A[0] = 0;     A[1] = 1;     for(int i=2;i<=n;i++){         A[i] = A[i-1] + A[i-2];     }     System.out.println(A[n]);  } }

Bubble Sort

1. You are given an array(arr) of integers. 2. You have to sort the given array in increasing order using bubble sort. Input Format An Integer n  arr1 arr2.. n integers Output Format Check the sample ouput and question video. Constraints 1 <= N <= 10000 -10^9 <= arr[i] <= 10^9 Sample Input 5 7  -2  4  1  3 Sample Output Comparing -2 and 7 Swapping -2 and 7 Comparing 4 and 7 Swapping 4 and 7 Comparing 1 and 7 Swapping 1 and 7 Comparing 3 and 7 Swapping 3 and 7 Comparing 4 and -2 Comparing 1 and 4 Swapping 1 and 4 Comparing 3 and 4 Swapping 3 and 4 Comparing 1 and -2 Comparing 3 and 1 Comparing 1 and -2 -2 1 3 4 7 Solution: import java.io.*; import java.util.*; public class Main {   public static void bubbleSort(int[] arr) {     //write your code here     for(int i=0;i<arr.length;i++){         for(int j=1;j<arr.length-i;j++)             if(isSmaller(arr,j,j-1)){                 swap(arr,j,j-1);             }     }   }   // used for swapping ith and jth elements of array

Knights Tour

1. You are given a number n, the size of a chess board. 2. You are given a row and a column, as a starting point for a knight piece. 3. You are required to generate the all moves of a knight starting in (row, col) such that knight visits       all cells of the board exactly once. 4. Complete the body of printKnightsTour function - without changing signature - to calculate and       print all configurations of the chess board representing the route      of knight through the chess board. Use sample input and output to get more idea. Note -> When moving from (r, c) to the possible 8 options give first precedence to (r - 2, c + 1) and                 move in clockwise manner to                explore other options. Input Format A number n A number row A number col Output Format All configurations of the chess board representing route of knights through the chess board starting in (row, col) Use displayBoard function to print one configuration of the board. Constraints n = 5 0 <= row

Reverse a Number

  /* @ToDo     Reverse a Number    7325 => 5237    12345 => 54321 */ #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 ;      int   num  =  0 ;      while ( n > 0 ){          num  = ( num * 10 ) + ( n % 10 );          n  /=  10 ;     }       cout << num << endl ;      return   0 ; }

If a Prime Number

  /* @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 ;     }      if ( flag )          cout << "Prime Number" ;      else   cout << "Not a Prime Number" ;      return   0 ; }

Zig Zag Pattern

  /* @ToDo     Zig Zag Pattern         *               *                *       *       *       *        *               *               *        */ #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 ;      for ( int   i = 1 ; i <= 3 ; i ++){          for ( int   j = 1 ; j <= n ; j ++){              if ((( i + j )% 4  ==  0 ) || ( i == 2  &&  j % 4  ==  0 ))                  cout << "* \t " ;              else   cout << " \t " ;         }          cout << endl ;     }      return   0 ; }

Star Pattern

  /* @ToDo     Star Pattern                  *                *   *   *            *   *   *   *   *        *   *   *   *   *   *   *    *   *   *   *   *   *   *   *   *    *   *   *   *   *   *   *   *   *        *   *   *   *   *   *   *            *   *   *   *   *                *   *   *                    *            */ #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 ;      for ( int   i = 1 ; i <= n ; i ++){          for ( int   j = 0 ; j < n - i ; j ++)              cout << " \t " ;          for ( int   j = 1 ; j < 2 * i ; j ++)              cout << "* \t " ;          cout << endl ;     }      for ( int   i = n ; i > 0 ; i --){          for ( int   j = 0 ; j < n

Palindromic Pattern

  /* @ToDo     Palindromic Pattern                 1                2   1   2            3   2   1   2   3        4   3   2   1   2   3   4    5   4   3   2   1   2   3   4   5        */ #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 ;     for ( int   i = 1 ; i <= n ; i ++){          for ( int   j = 0 ; j < n - i ; j ++)              cout << " \t " ;          int   j = i ;          for (; j > 1 ; j --)              cout << j << " \t " ;                  for (; j <= i ; j ++)              cout << j << " \t " ;          cout << endl ;    }      return   0 ; }

Number Pattern

  /* @ToDo     Number Pattern                 1                    1       2                1       2       3            1       2       3       4        1       2       3       4       5            */ #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 ;     for ( int   i = 1 ; i <= n ; i ++){         for ( int   j = 0 ; j < n - i ; j ++)          cout << " \t " ;          for ( int   j = 1 ; j <= i ; j ++)              cout << j << " \t\t " ;          cout << endl ;      }      return   0 ; }

Rhumbus Pattern

  /* @ToDo     Rhumbus Pattern                 *   *   *   *   *                *   *   *   *   *            *   *   *   *   *        *   *   *   *   *    *   *   *   *   *        */ #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 ;      for ( int   i = 0 ; i < n ; i ++){          for ( int   j = 1 ; j < n - i ; j ++)              cout << " \t " ;          for ( int   j = 0 ; j < n ; j ++)              cout << "* \t " ;          cout << endl ;     }      return   0 ; }

0-1 Pattern

  /* @ToDo  0-1 Pattern     1     0   1     0   1   0     1   0   1   0     1   0   1   0   1 */ #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 ;      int   sign  =  1 ;      for ( int   i = 1 ; i <= n ; i ++){          for ( int   j = 1 ; j <= i ; j ++){              cout << " \t " << sign ;              sign  = ! sign ;         }          cout << endl ;     }      return   0 ; }

Inverted Pattern

  /* @ToDo  Inverted Pattern 1   2   3   4   5    1   2   3   4    1   2   3    1   2    1    */ #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 ;      for ( int   i = 0 ; i < n ; i ++){          for ( int   j = 1 ; j <= n - i ; j ++)              cout << j << " \t " ;          cout << endl ;     }      return   0 ; }

Butterfly Pattern

  /* @ToDo  Butterfly Pattern     *                                   *     *   *                           *   *     *   *   *                   *   *   *     *   *   *   *           *   *   *   *     *   *   *   *   *   *   *   *   *   *     *   *   *   *           *   *   *   *     *   *   *                   *   *   *     *   *                           *   *     *                                   * */ #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 ;      for ( int   i = 1 ; i <= n ; i ++){          for ( int   j = 1 ; j <= 2 * n ; j ++){              if ( j <= i  ||  j  >( 2 * n )- i )                  cout << " \t *" ;              else   cout << " \t " ;         }         

Floyd's Triangle

/*  To Print Floyd's Triangle 1    2   3    4   5   6    7   8   9   10   11  12  13  14  15   */ #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 ;      int   count  =  1 ;      for ( int   i = 0 ; i < n ; i ++){          for ( int   j = 0 ; j <= i ; j ++)              cout << count ++ << " \t " ;          cout << endl ;     }      return   0 ; }

Pyramid

/*  To Print  1    2   2    3   3   3    4   4   4   4    5   5   5   5   5    */ #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 ;      for ( int   i = 1 ; i <= n ; i ++){          for ( int   j = 1 ; j <=  i ; j ++){              cout << i << " \t " ;         }          cout << endl ;     }         return   0 ; }

Subscribe to Get's Answer by Email