Untitled
raw download clone
TEXT
views 27
,
size 1018 b
# Python3 implementation of the approach 
  
# Function to return the sum 
# of the digits of n 
def sumOfDigit(n) : 
      
    sum = 0; 
    while (n > 0) :
          
        sum += n % 10; 
        n = n // 10; 
          
    return sum; 
  
# Function to sort the array according to 
# the sum of the digits of elements 
def sortArr(arr, n) :
      
    # Vector to store digit sum with 
    # respective element 
    vp = []; 
  
    # Inserting digit sum with element
    # in the vector pair 
    for i in range(n) :
        vp.append([sumOfDigit(arr[i]), arr[i]]); 
  
    # Quick sort the vector, this will 
    # sort the pair according to sum 
    # of the digits of elements 
    vp.sort()
  
    # Print the sorted vector content 
    for i in range(len(vp)) :
        print(vp[i][1], end = " "); 
  
# Driver code 
if __name__ == "__main__" : 
  
    arr = [14, 1101, 10, 35, 0]; 
    n = len(arr);
    sortArr(arr, n); 
  
# This code is contributed by Ryuga
close fullscreen
Login or Register to edit or fork this paste. It's free.