Number-X Largest Digits Ascending

 

C Code:

#include<stdio.h>
#include <stdlib.h>
int main()
{
long long int a,n,i=0,x=0,c[1000000],z=0,j=0,d,b[1000000];
scanf("%lld%lld",&a,&n);
while(a>0)
{
    b[j]=a%10;
    a=a/10;
    j++;
}
d=j;
for(i=9;i>=1,z<n;i--)
{
    for(j=0;j<=d;j++)
    {
        if(i==b[j])
        {
            c[x]=i;
            x++;
            z++;
            break;
        }
    }
}
for(i=n-1;i>=0;i--)
{
    printf("%lld",c[i]);
}
}

C++ Code: 

Java Code:

import java.util.*;
public class Hello 
{
    public static void main(String[] args) 
{
    Scanner s=new Scanner(System.in);
    String str=s.nextLine();
    char c[]=str.toCharArray();
    int n,l,a;
    n=s.nextInt();
    l=c.length;
    a=l-n;
    for(int i=0;i<l;i++)
    {
        for(int j=i+1;j<l;j++)
        {
        if(c[i]!='0')
        {
            if(c[i]==c[j])
            {
                c[j]='0';
            }
        }
        }
    }
    Arrays.sort(c);
    for(int i=a;i<l;i++)
    System.out.print(c[i]);
    }
}

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. #include
    #include
    using namespace std;
    int main(){
    long int n;
    int x,i=0,j,k;
    cin>>n>>x;
    int a[100000];
    while(n>0)
    {
    a[i]=n%10;
    n=n/10;
    i++;
    }
    sort(a,a+i);
    for(j=0;j<i;j++)
    {
    for(k=j+1;k<i;k++)
    {
    if(a[j]==a[k])
    {
    a[j]=0;
    }
    }
    }
    sort(a,a+i);
    int q=i-x;
    for(j=q;j<i;j++)
    {
    cout<<a[j];
    }
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Fisherman's Mantra

First & Last X Digits

N Integers – Sum S – All Combinations(Weekly Test)