-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolname.cpp
More file actions
58 lines (53 loc) · 1.3 KB
/
colname.cpp
File metadata and controls
58 lines (53 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
Excel columns has a pattern like A, B, C, … ,Z, AA, AB, AC,…. ,AZ, BA, BB, … ZZ, AAA, AAB …..
etc. In other words, column 1 is named as “A”, column 2 as “B”, column 27 as “AA” and so on.
Example 1:
Input:
N = 28
Output: AB
Explanation: 1 to 26 are A to Z.
Then, 27 is AA and 28 = AB.
Example 2:
Input:
N = 13
Output: M
Explanation: M is the 13th character of
alphabet.
Your Task:
You don't need to read input or print anything. Your task is to complete the function colName()
which takes the column number N as input and returns the column name represented as a string.
Expected Time Complexity: O(LogN).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 1018*/
#include<bits/stdc++.h>
using namespace std;
string colName (long long int n)
{
if(n<=26)
{
string sd="";
sd=sd+char(64+n);
return sd;
}
string ans="";
int t;
while(n>0)
{
n--;
t=n%26;
ans=ans+char(65+(t));
n=n/26;
}
reverse(ans.begin(),ans.end());
cout<<ans;
return ans;
}
int main()
{
int n;
cout<<"\n enter the number:";
cin>>n;
colName(n);
}