Diagonal Difference Solution in Java | HackerRank | Diagonal Difference HackerRank Solution
Staircase Solution in JavaStaircase HackerRank Solution in Java
Problem:
Consider a staircase of size :
#
##
###
####
Observe that its base and height are both equal to , and the image is drawn using #
symbols and spaces. The last line is not preceded by any spaces.
Write a program that prints a staircase of size .
Function Description
Complete the staircase function in the editor below. It should print a staircase as described above.
staircase has the following parameter(s):
- n: an integer
Input Format
A single integer, , denoting the size of the staircase.
Constraints
.
Output Format
Print a staircase of size using #
symbols and spaces.
Note: The last line must have spaces in it.
Sample Input
6
Sample Output
#
##
###
####
#####
######
Explanation
The staircase is right-aligned, composed of #
symbols and spaces, and has a height and width of .
https://www.hackerrank.com/challenges/staircase/problem
Staircase HackerRank Solution in Java
Staircase Solution in Java
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { // Complete the plusMinus function below. static void plusMinus(int[] arr) { int plus=0,minus=0,zero=0; for(int i=0;i<arr.length;i++){ if(arr[i]==0){ zero+=1; } else if(arr[i]>0){ plus+=1; } else minus+=1; } System.out.println((float)plus/arr.length); System.out.println((float)minus/arr.length); System.out.println((float)zero/arr.length); } private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int n = scanner.nextInt(); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); int[] arr = new int[n]; String[] arrItems = scanner.nextLine().split(" "); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); for (int i = 0; i < n; i++) { int arrItem = Integer.parseInt(arrItems[i]); arr[i] = arrItem; } plusMinus(arr); scanner.close(); } }
0 Comments