Menu Close

Determine the maximum sequence weight

Hassan gets a job in a software company in Hyderabad. The training period for the first three months is 20000 salary. Then incremented to 25000 salaries.  Training is great but they will give you a programming task every day in three months. Hassan must finish it in the allotted time. His teammate Jocelyn gives him a task. 

What will be the value of the weight of a given number sequence?  if Each number has a numeric index equal to its sum of digits, the weight of the sequence is equal to its sum of the product of the position of the number and its numeric index.

For Example sequence of 6 nos is as follows: 12 31 45 13 56 67

Hassan was given a sequence S with unique elements, can you help him to write a code to help him determine the sequence S1, formed from S whose weight is maximum ?. 

As multiple sequences can be formed when numbers have the same numeric index. 
In this case, a maximum of 2 numbers will be given whose number index is the same. 
Print all the sequence, the first number should be the smallest number that occurs before greater.

Input:
First input represents a number of elements 
Second input represents elements 

Output:
First line indicates the total weights of a given sequence

The second and third line indicates the possible sequence to get the highest weight.

The fourth line indicates Maximum sequence weight.
#include <stdio.h>
int numind(int n){
    int len=0;
    while(n!=0){
        n=n/10;
        len++;
    }
    return len;
}
int numind_1(int arr[],int N);
int main()
{
    int n;
    int arr[15];
    scanf("%d",&n);
    int i=0,c=0,temp=0;
    for(i=0;i<n;i++){scanf("%d",&arr[i]);}
    printf("Weight of given input sequence=%d",numind_1(arr,n));
	for(i=0;i+1<n;i++){
	    int j=0;
	    if(arr[i]>arr[i+1]){temp=arr[i];
	        arr[i]=arr[i+1];
	        arr[i+1]=temp;
	        printf("\n");
	    for(j=0;j<n;j++){
	        printf("%d ",arr[j]);}i=0;}
	   if(c<numind_1(arr,n)){c=numind_1(arr,n);}
	}
	printf("\nMaximum sequence weight=%d",c);
	return 0;
}
int numind_1(int arr[],int N){
    int i,j,C=0;
    for(i=0;i<N;i++){
        int t=0,rem,len=0,n=arr[i],num=arr[i];
        len=numind(n);
        for(j=0;j<len;j++){
            rem=num%10;
            num=num/10;
            t=t+rem;}
            C=C+(t*(i+1));
        }
        return C;
}

INPUT_1:
 5
 12  31  45  13  78

OUTPUT:
Weight of given input sequence=129
12  31  13  45  78
12  13  31  45  78
Maximum sequence weight=134


INPUT_2:
 5
 45  13  78  88  98

OUTPUT:
Weight of given input sequence=211
13  45  78  88  98
Maximum sequence weight=216


ILLUSTRATION

Executed using gcc

Morae! Q