Answers for "find largest number"

1

largest number

Input: nums = [10,2]
Output: "210"

Input: nums = [3,30,34,5,9]
Output: "9534330"

__________________________________________________________

public String largestNumber(int[] nums) {

    List<String> list = new ArrayList<String>();
    
    for(int i=0;i<nums.length;i++){
        
        list.add(String.valueOf(nums[i]));
    }
    
    Collections.sort(list,new Comparator<String>(){
       
        public int compare(String A,String B){
            String AB = A+B;
            String BA = B+A;
            
            return AB.compareTo(BA) > 0 ? -1 : 1;
        }
        
    });
    
    String res = "";
     for(int i=0;i<list.size();i++){
         res += list.get(i);
    }
    
    if(res.charAt(0) == '0')
        return "0";
    
    return res;
}
Posted by: Guest on August-06-2021
6

find maximum number in array

#include <stdio.h>
int main() {
    int i, n;
    float arr[100];
    printf("Enter the number of elements (1 to 100): ");
    scanf("%d", &n);

    for (i = 0; i < n; ++i) {
        printf("Enter number%d: ", i + 1);
        scanf("%f", &arr[i]);
    }

    // storing the largest number to arr[0]
    for (i = 1; i < n; ++i) {
        if (arr[0] < arr[i])
            arr[0] = arr[i];
    }

    printf("Largest element = %.2f", arr[0]);

    return 0;
}
Posted by: Guest on April-28-2020

Code answers related to "find largest number"

Browse Popular Code Answers by Language