• Study Materials – Company wise
    • Infosys Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Dell Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • TCS Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation – E Books
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
    • Quant for Placements
  • Register
  • Login
CODE OF GEEKS

We at CODE OF GEEKS, aim at providing best and quality content for our users at no extra cost.

    • Study Materials – Company wise
      • Infosys Archive
      • Accenture Archive
      • AMCAT Archive
      • Capgemini Archive
      • Cisco Archive
      • CoCubes Archive
      • Cognizant(CTS) Archive
      • Dell Archive
      • Deloitte Archive
      • DXC Archive
      • Goldman Sachs Archive
      • Hexaware Technologies Archive
      • LTI Archive
      • MindTree Archive
      • TCS Archive
      • Virtusa Archive
      • Wipro Archive
    • Interview Preparation – E Books
      • C Interview Questions
      • Data Structures Interview Questions
      • DBMS Interview Questions
      • HR Interview Questions
      • Java Interview Questions
      • Operating System Interview Questions
      • Python Interview Questions
      • SQL Query Interview Questions
    • Programming
      • C Programming MCQs
      • C Code Snippets – Output Questions
      • Python Code Snippets – Output Questions
    • Aptitude
      • Verbal Ability for Placements
      • Quant for Placements
    • Register
    • Login
  • [email protected]
  • ..
Login / Register
Apply Now
CODE OF GEEKS
CODE OF GEEKS
  • Study Materials – Company wise
    • Infosys Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Dell Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • TCS Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation – E Books
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
    • Quant for Placements
  • Register
  • Login
0

Cart

Weighted Strings

  • March 3, 2020
  • CODE OF GEEKS
  • 11

In Hackerland every character has a weight. The weight of an English uppercase alphabet A-Z is given below :

A = 1

B = 2*A + A

C = 3*B + B

D = 4*C + C

….

Z = 26*Y + Y

The weight made up of these characters is the summation of weights of each character. Given a total string weight, determine shortest string of given weight. If there is more than one solution, return the lexicographically smallest of them. For example, given weight = 25, and the weights of the first few characters of the alphabets are A=1, B=3, C=12, D=60 it is certain that no letter larger than C is required. Some of the strings with a total weight equal to the target are ABBBBC, ACC, AAAAAAABBBBBB. The shortest of these is ACC. While any permutation of these characters will have same weight, this is the lexicographically smallest of them.



Example

Input

20

Output

AABBC



Tags: hack with infy coding questionshackwithinfy coding problemshackwithinfy coding questions
  • Previous InfyTQ Screening Test Experience 2020 – 2
  • Next InfyTQ Screening Test Experience 2020 – 4

11 comments on “Weighted Strings”

  1. Anurag Tiwari says:
    March 4, 2020 at 2:30 pm

    is this correct?
    d={“A”:1}
    for i in range(2,27):
    val=64+i
    t=chr(val)
    t1=i*d[chr(val-1)]+d[chr(val-1)]
    d[t]=t1
    s=int(input())
    i=”A”
    while i in d and d[i]0 and i>=”A”:
    if d[i]<=s:
    s=s-d[i]
    l.append(i)
    else:
    i=chr(ord(i)-1)
    print(*l[::-1],sep="")

    Reply
  2. Alfaij Mansuri says:
    March 8, 2020 at 3:49 pm

    n = int(input())
    di = {}
    li = [1,3]
    for i in range(4,29):
    li.append(i*li[-1])
    li = li[:27]
    x = 0
    for i in range(65,91):
    if li[x] not in di:
    di[li[x]] = chr(i)
    x += 1
    s = ”
    for i in range(26,-1,-1):
    # print(n//li[i])
    if n//li[i] > 0:
    s += di[li[i]]*(n//li[i])
    n = n%li[i]
    print(”.join(sorted(s)))

    Reply
  3. Shubham Kondekar says:
    March 11, 2020 at 9:59 am

    Data,ans,sume={“A”:1},[],0
    for i in range(2,27):
    Data[chr(64+i)]=i*Data[chr(63+i)]+Data[chr(63+i)]
    key=list(reversed([i for i in Data.keys()]))

    nu=int(input())

    #Lowest
    for i,x in enumerate(key):
    if nu>=Data[x]:
    idx=i
    break

    while sume<=nu:
    if (Data[key[idx]]+sume)<=nu:
    ans.append(key[idx])
    sume+=Data[key[idx]]
    #print(sume)
    elif idx==25:
    break
    else:
    idx+=1
    print(*ans[::-1],sep="")

    Reply
  4. saimanoj says:
    March 12, 2020 at 7:38 am

    l = [1]
    res = []
    s = “”
    for i in range(1,26):
    l.append((i+1)*l[i-1]+l[i-1])
    n = int(input())
    for i in range(len(l)):
    if(n == l[i]):
    print(chr(i+97))
    elif(n = 0):
    n-=res[-1]
    s += chr((len(res)-1)+97)
    else:
    res.pop()

    print(s[::-1])

    Reply
  5. K.Pragyna Nidhi says:
    March 12, 2020 at 4:13 pm

    n=int(input())
    s=’ABCDEFGHIJKLMNOPQRSTUVWXZ’
    c=4;res=”
    d={}
    d[‘A’]=1
    d[‘B’]=3
    for i in range(2,len(s)):
    d[s[i]]=d[s[i-1]]*c
    if d[s[i]]>n:
    del d[s[i]]
    break
    c+=1
    l=list(d.keys())
    print(l)
    for i in range(len(l)-1,-1,-1):
    r=n//d[l[i]]
    res+=l[i]*r
    n=n%d[l[i]]
    if n==0:
    break
    print(res[::-1])

    Reply
  6. K.Lavanya says:
    March 14, 2020 at 3:58 am

    dict={‘A’:1}
    for i in range(2,27):
    a=i+64
    dict[chr(a)]=i*dict[chr(a-1)]+dict[chr(a-1)]
    w=int(input())
    str=””
    for i in range(26,0,-1):
    if(dict[chr(i+64)]>w):
    continue
    else:
    n=w//dict[chr(i+64)]
    str=n*chr(i+64)+str
    w-=n*dict[chr(i+64)]
    print(str)

    Reply
  7. Shrutik Haribhau Gumate says:
    March 21, 2020 at 7:03 am

    def WeightedStrings(n):
    c=’A’
    key=1
    mf=3
    d={1:’A’}
    for i in range(66,91):
    key*=mf
    d[key]=chr(i)
    mf+=1
    keys=list(d.keys())
    res=””
    while n!=0:
    i=0
    while i<26:
    if n==keys[i]:
    res+=d[n]
    n=0
    break;
    elif n<keys[i]:
    res+=d[keys[i-1]]
    n-=keys[i-1]
    break;
    i+=1
    return res[::-1]
    print(WeightedStrings(int(input())))

    Reply
  8. PUTTI MOHAN says:
    March 26, 2020 at 5:28 am

    import java.util.*;
    public class WeightedStrings{
    static int values[]=new int[26];
    static void insertValues(){
    values[0]=1;
    int prev=1;
    for(int i=1;i<26;i++){
    values[i]=(i+1)*prev+prev;
    prev=values[i];
    }
    }
    static StringBuffer formedString(StringBuffer sb,int k){
    int low=0;
    int high=25;
    while(k!=0){
    int ind=findfloor(k,low,high);
    sb.insert(0,(char)(ind+'A'));
    k=k-values[ind];
    }
    return sb;

    }
    static int findfloor(int k,int low,int high){
    int ans=-1;
    while(low<=high){
    int mid=(low+high)/2;
    if(values[mid]<=k){
    ans=mid;
    low=mid+1;
    }
    else
    high=mid-1;
    }
    return ans;
    }
    public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
    insertValues();
    int k=sc.nextInt();
    StringBuffer sb=new StringBuffer();
    StringBuffer res=formedString(sb,k);
    System.out.println(res);

    }
    }

    Reply
    1. Bollineni Bhavana says:
      April 21, 2020 at 11:19 am

      n=int(input())
      dict={‘A’:1,’B’:3,’C’:12,’D’:60,’E’:360,’F’:2520,’G’:20160,’H’:181440}
      s=0
      l=[]
      for x,y in sorted(dict.items(),key=lambda x:x[1]):
      s=s+y
      if s<=n:
      l.append([x,y])
      l.sort(key=lambda x:x[1],reverse=True)
      print(l)
      s=""
      for i in range(len(l)):
      t=n//l[i][1]
      n=n-(t*l[i][1])
      s=s+t*l[i][0]
      l2=list(s)
      l2.reverse()
      print("".join(l2))

      Reply
  9. rishabh gupta says:
    May 28, 2020 at 6:49 pm

    d={“A”:1}
    for i in range(2,27):
    key=chr(i+64)
    d[key]=i*d[chr(i+63)]+d[chr(i+63)]

    def find_series(n):
    d1={}
    l=[]
    string=”ABCDEFGHIJKLMNOPQRSTUVWXYZ”
    for i in d:
    if d[i]=l[i]:
    a= n//l[i]
    n-=(l[i]*a)
    l1.append(string[len(l)-i-1]*a)
    if n==0:
    break
    string2=”.join(sorted(l1))
    return string2

    n=int(input())
    print(find_series(n))

    Reply
  10. sairaja says:
    May 6, 2021 at 9:48 am

    a=[‘A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’,’I’,’J’,’K’,’L’,’M’,’N’,’O’,’P’,’Q’,’R’,’S’,’T’,’U’,’V’,’W’,’X’,’Y’,’Z’]
    v=[0,1,3]
    for i in range(3,27):
    v.append(v[i-1]*(i+1))
    # print(v)
    d=dict()
    for i in range(len(a)):
    d[a[i]]=v[i+1]
    # print(d)
    num=int(input())
    str=””
    max=””
    while(num!=0):
    for i in a:
    if(d[i]>num):
    break
    max=i
    q=num//d[max]
    num=num%d[max]
    str=q*max+str
    print(str)

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

CODE OF GEEKS

Subscribe to Newsletter

CODE OF GEEKS

Learn | Code | Achieve

Reach us

[email protected]
We at CODE OF GEEKS, aim at providing quality content to our users at no cost.
CODE OF GEEKS

Important Pages

About us
Advertise
Privacy Policy
Terms and Conditions

Placements – Study Materials

TCS NQT     Wipro     CapGemini
Accenture     MindTree     CTS
DXC     Hexaware Technologies     AMCAT
CoCubes     Goldman Sachs     Dell
Cisco     Deloitte     Virtusa     LTI     Infosys   

Courses

  • Mail Us
  • Login/Register

Recent Posts

What is Cloud Computing | Cloud Computing Applications | Cloud Development Models| SaaS vs PaaS vs IaaS
  • October 23, 2021
What is Amazon Web Services | AWS Regions & AZs | Different AWS Services
  • October 19, 2021

Copyright 2021 CODE OF GEEKS. All Rights Reserved.

  • →