Form Biggest Number [codingblocks]
You are provided an array of numbers. You need to arrange them in a way that yields the largest value.
Input Format
First line contains integer t which is number of test case.
For each test case, you are given a single integer n in the first line which is the size of array A[] and next line contains n space separated integers denoting the elements of the array A .
Constraints
1<=t<=100
1<=m<=100
1<=A[i]<=10^5
Output Format
Print the largest value.
Sample Input
1
4
54 546 548 60
Sample Output
6054854654
Explanation
Upon rearranging the elements of the array , 6054854654 is the largest possible number that can be generated.
Java Code:
IDE Code: https://ide.geeksforgeeks.org/jwhvpJ4eNe
/* Amit Kumar 12-12-2020 IDE Code: https://ide.geeksforgeeks.org/jwhvpJ4eNe */ package String; import java.util.Scanner; public class String_PiyushAndMagicalPark { public static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int row = scanner.nextInt(); int col = scanner.nextInt(); int minStrength = scanner.nextInt(); int currentStrength = scanner.nextInt(); String[][] park = new String[row][col]; for (int i=0; i<row; i++) { for (int j=0; j<col; j++) { park[i][j] = scanner.next(); } } int finalStrength = getPiyushAndMagicalParkFinalStrength(park, row, col, minStrength, currentStrength); if (finalStrength > minStrength) { System.out.println("Yes"); System.out.println(finalStrength); } else { System.out.println("No"); } } private static int getPiyushAndMagicalParkFinalStrength(String[][] park, int row, int col, int minStrength, int currentStrength) { for (int i=0; i<row; i++) { for (int j=0; j<col; j++) { String currValue = park[i][j]; if (currentStrength < minStrength) { return currentStrength; } switch (currValue) { case "." : currentStrength = currentStrength - 2; break; case "*" : currentStrength = currentStrength + 5; break; case "#" : j = col - 1; break; } if (j != col - 1) { currentStrength--; } } } return currentStrength; } }
Comments
Post a Comment