Posts

Showing posts with the label hackerrank

Hackerrank Modified Kaprekar Numbers Solution

Image
This is the solution to the Modified Kaprekar Numbers found in the implementation section of the algorithm domain in Hackerrank. A modified Kaprekar number is a positive whole number n with d digits, such that when we split its square into two pieces - a right hand piece r with d digits and a left hand piece l that contains the remaining d or d−1 digits, the sum of the pieces is equal to the original number (i.e. l + r = n) . Screenshot The Code def sendKap(x,s): y=x*x y=str(y) s1=y[:len(y)/2:] s2=y[len(y)/2::] s3=int(s1)+int(s2) if(s3==x): s.append(str(x)) st=input() en=input() s=[] for i in range(st,en+1): if(i>9): sendKap(i,s) elif(i==1): s.append(str(i)) elif(i==9): s.append(str(i)) if(len(s)==0): print 'INVALID RANGE' else: print ' '.join(s) Got any problems feel free to comment them here !

Hackerrank Cavity Map Solution

Image
This is the solution to the Cavity Map problem found in the the implementation section of the Algorithm domain in Hackerrank. The bellow solution is in Python2. In this you are given a square map of size n×n . Each cell of the map has a value denoting its depth. We will call a cell of the map a cavity if and only if this cell is not on the border of the map and each cell adjacent to it has strictly smaller depth. Two cells are adjacent if they have a common side (edge). You need to find all the cavities on the map and depict them with the uppercase character X. Screenshot: The Code: def mapCavity(arr, n): for i in range(1, n-1): for j in range(1, n-1): if arr[i-1][j] != 'X' and int(arr[i-1][j]) < int(arr[i][j]) and \ arr[i+1][j] != 'X' and int(arr[i+1][j]) < int(arr[i][j]) and \ arr[i][j-1] != 'X' and int(arr[i][j-1]) < int(arr[i][j]) and \ arr[i][j+1] != 'X' and ...

Hackerrank Utopian Tree Solution

Image
This is the solution to the program in Hackerrank. This program can be found in the Algorithm Domain. Basically the Utopian Tree goes through 2 cycles of growth every year. The first growth cycle occurs during the spring, when it doubles in height. The second growth cycle occurs during the summer, when its height increases by 1 meter.   Screenshot The Code: #include<stdio.h> int main() { int t, n, a; scanf("%d", &t); while(t--) { a=1; scanf("%d", &n); for(int i=1; i<=n; i++) { if(i%2) a<<=1; else a++; } printf("%d\n", a); } return 0; } Found bugs, report them !

Hackerrank Time Conversion Solution

Image
This is the solution to the problem found in the Warmup section of algorithm domain in Hackerrank. This program is done using Python 2. In this you are given time in AM/PM format. Convert this into a 24 hour format. Screenshot The Code x=raw_input() t=x.split(':') if(t[2][2]=='P' or t[2][2]=='p'): if(t[0]!='12'): t[0]=str(12+int(t[0])) elif(t[0]=='12'): t[0]='00' t=':'.join(t) print t[:-2:] Found bugs or got anything to share, do comment here !

Hackerrank Library Fine Solution

Image
This is the solution is in python to the problem found in the warmup section in algorithm domain, in Hackerrank. Screenshot The Code: import sys x=raw_input() y=raw_input() x=x.split() y=y.split() if(int(x[2])>=int(y[2])): if(int(x[2])==int(y[2]) and int(x[1])<int(y[1])): print '0' raise SystemExit if(int(x[1])==int(y[1]) and int(x[0])<int(y[0])): print '0' raise SystemExit if(int(x[2])-int(y[2])!=0): print 10000 elif(int(x[1])-int(y[1])!=0): print 500*(int(x[1])-int(y[1])) elif(int(x[0])-int(y[0])!=0): print 15*(int(x[0])-int(y[0])) else: print '0' else: print '0' Got any suggestions, drop them here !

Hackerrank Sherlock and array solution

Image
The below program is the solution of Sherlock and array in hackerrank. Screenshot[Demo] :  Code : #include<iostream> using namespace std; int main() { int t; cin>>t; while(t--) { int x,l=0,r=0,flag=0; cin>>x; int a[x]; for(int i=0;i<x;i++) { cin>>a[i]; r+=a[i]; } for(int i=0;i<x;i++) { l=l+a[i-1]; r=r-a[i]; cout<<l<<" "<<r<<" "<<i<<endl; if(l==r) { flag=1; break; } } if(flag==1) cout<<"YES"<<endl; else cout<<"NO"<<endl; } }

Matrix Rotation Hackerrank Solution

Image
This is the solution for the problem Matrix Rotation  in Python, found in the algorithm domain of Hackerrank. You are given a 2D matrix, A , of dimension MxN and a positive integer R . You have to rotate the matrix R times and print the resultant matrix. Rotation should be in anti-clockwise direction. Screenshot[Demo] The Code def rotateMat(N,M,l,rot,rin,mat): # rotate each layer for level in range(l): r = rot % len(rin[level]) rin[level] = rin[level][r:] + rin[level][:r] # fill the array with the rotated elements for level in range(l): top = (N-1) - 2 * level side = (M-1) - 2 * level for i in range(top): mat[level][level + i] = rin[level].pop(0) # right for j in range(side): mat[level + j][level + top] = rin[level].pop(0) # down for i in range(top): mat[lev...

Hackerrank Sherlock and Anagram Solution

Image
This is the solution to the Sherlock and Anagram problem found in the strings section of the algorithm domain. Screenshot The Code import collections def solve(x): d = collections.defaultdict(int) lst = list(x) n = len(lst) for i in xrange(n): for l in xrange(1, n-i+1): sub = lst[i: i+l] sub.sort() d["".join(sub)] += 1 s = 0 for v in d.values(): s += v*(v-1)/2 return s for t in xrange(input()): print solve(map(str,raw_input())) Have any Feedback, drop them here !

Hackerrank String Reduction Solution

Image
This is the solution to the program, solved in python. The Question can be found in the Algorithm domain of Hackerrank. Problem Statement: Given a string consisting of letters, a, b and c, we can perform the following operation: Take any two adjacent distinct characters and replace them with the third character. For example, if 'a' and 'c' are adjacent, they can replaced by 'b'. Find the smallest string which we can obtain by applying this operation repeatedly? Screenshots[Demo] The Code: def cnt(x): m={} m['a']=0;m['b']=0;m['c']=0 for c in x: if c not in m: m[c]=1 else: m[c]+=1 return m def red(x): xm=cnt(x) print x,xm if((xm['a']==0 and xm['b']==0 ) or (xm['a']==0 and xm['c']==0 ) or (xm['b']==0 and xm['c']==0)): return len(x); if((xm['a']%2==0 and xm['b']%2==0 and xm['c']%2==0) or (...

Hackerrnak Common Child Solution

Image
This is the solution to the problem found in the algorithm domain. This solution is made using Python. Screenshots The Code def cc(x,y): z=[[0 for j in range(len(y)+1)] for i in range(len(x)+1)] for i, a in enumerate(x): for j, b in enumerate(y): if a == b: z[i+1][j+1] = z[i][j] + 1 else: z[i+1][j+1] = \ max(z[i+1][j], z[i][j+1]) return z[-1][-1] x = raw_input() y = raw_input() print cc(x,y) Got Bugs, Suggestions, Drop them here !

Hackerrank Palindrome Index Solution

Image
This is the solution to the problem Palindrome Index, which can be found in the Algorithm domain. The bellow solution is coded in python. Screenshots The Code def isPal(s): for i in xrange(len(s)/2): if s[i] != s[len(s)-i-1]: return False return True def solve(s): for i in xrange((len(s)+1)/2): if s[i] != s[len(s)-i-1]: if isPal(s[:i]+s[i+1:]): return i else: return len(s)-i-1 return -1 for i in range(0,input()): x=raw_input() print solve(x) Got any comments, drop them here !

Hackerrank Anagram solution

Image
The below program is the solution of Anagram in implementation sub-domain. You can refer the question from  HERE. Output : Code : #include<stdio.h> int main() { int t,i,x,c,flag; char s[10001]; int *a=(int*)malloc(sizeof(int)*26); int *b=(int*)malloc(sizeof(int)*26); memset(a,0,sizeof(int)*26); memset(b,0,sizeof(int)*26); scanf("%d",&t); while(t--) { scanf("%s",&s); flag=0;c=0; if(strlen(s)%2==1) { printf("-1\n"); continue; } for(i=0;i<26;i++) { a[i]=b[i]=0; } for(i=0;i<strlen(s)/2;i++) a[s[i]-'a']++; for(i=strlen(s)/2;i<strlen(s);i++) b[s[i]-'a']++; for(i=0;i<26;i++) { c+=abs(a[i]-b[i]); } printf("%d\n",c/2); } }

Hackerrank Angry Professor Solution

Image
The below program is the solution of Angry Professor in Implementation sub-domain. You can get the question  HERE . Output : Code : #include<iostream> using namespace std; int main() { int t,x,y,c=0,m; cin>>t; while(t--) { cin>>x>>y; while(x--) { cin>>m; if(m<=0) c++; } if(c>=y) cout<<"NO"<<endl; else cout<<"YES"<<endl; c=0; } return 0; }

Hackerrank Two Strings Solution

Image
This is the solution to the problem found in the Strings section of the Algorithm Domain. The problem statement is we need to print YES if there is at least a substring of length 1 in both the strings, else we need to print NO. The bellow program is done using python. Screenshots[Demo] The Code: def sen(x,y): cnt=0 j=0 for i in range(0,len(x)): for j in range(0,len(y)): if (x[i]==y[j]): cnt=cnt+1 break if(cnt!=0): print "YES" else: print "NO" for i in range(0,input()): x=map(str,set(raw_input())) y=map(str,set(raw_input())) sen(x,y) Found Bugs, Report Us !

Hackerrank Caeser Cipher Solution

Image
The below program is the solution of Caeser Cipher in implementation domain. You can get the program  HERE. Output :  Code : #include<iostream> using namespace std; int main() { int x,t,m; cin>>x; string s; cin>>s; cin>>t; while(t>26) { t=t-26; } for(int i=0;i<s.length();i++) { m=s[i]+t; if(islower(s[i])) { if(m>122) { m=m-122; m+=96; } } else if(isupper(s[i])) { if(m>90) { m=m-90; m+=64; } } //cout<<m; if(isalpha(s[i])) cout<<(char)m; else cout<<s[i]; } return 0; }

Time In Words Hackerrank Solution

Image
This is the solution to the Time in words in the Implementation section of the Algorithm domain. In this Given the time in numerals we may convert it into wods. Screenshots[Demo] The Code mins = { 1:"one minute", 2:"two minutes", 3:"three minutes", 4:"four minutes", 5:"five minutes", 6:"six minutes", 7:"seven minutes", 8:"eight minutes", 9:"nine minutes", 10:"ten minutes", 11:"eleven minutes", 12:"twelve minutes", 13:"thirteen minutes", 14:"fourteen minutes", 15:"quarter", 16:"sixteen minutes", 17:"seventeen minutes", 18:"eighteen minutes", 19:"nineteen minutes", 20:"twenty minutes", 21:"twenty one minutes", 22:"twenty two minutes", 23:"twenty three minutes",...

Encryption Hackerrank Solution

Image
This is the solution for the Encryption problem in the implementation section of the Algorithm domain. In this An English text needs to be encrypted using the following encryption scheme. First, the spaces are removed from the text. Let L be the length of this text. Then, characters are written into a grid, whose rows and columns have the following constraints: ⌊L−−√⌋≤rows≤column≤⌈L−−√⌉, where ⌊x⌋ is floor function and ⌈x⌉ is ceil function Screenshots[Demo] The Code: import math def perform(x,row,col): row=int(row) col=int(col) mat=[['' for i in range(0,col)]for j in range(0,row)] str='' k=0 for i in range(0,row): for j in range(0,col): #print mat if(k<len(x)): mat[i][j]=x[k] k+=1 k=0 for i in range(0,col): for j in range(0,row): str=str+mat[j][k] k+=1 str=str+' ' print str def calc(x): row=math.floor(math.sqrt(len(x))); col=math.ceil(math.sqrt(len(x))); while(1==1): if((row*col)>=len(x)): bre...

Hackerrank Chocolate Feast Solution

Image
This is the solution of Chocolate Feast under implementation sub domain. You can refer the question  Here. Output :   Code : #include<iostream> using namespace std; int main() { int t; cin>>t; while(t--) { int x,y,z; cin>>x>>y>>z; int w,c=x/y; w=c; while(w>=z) { int m=w/z; c+=m; w=w%z; w+=m; } cout<<c<<endl; } } Well this is it for now..! Thanks for stopping by.. I hope you have liked the program. In case if you have any query you can type it in the comment box or you can contact with me, the contact details are in my profile.      Please don't forget to subscribe.. The subscription is free ;)

Hackerrank Cut The Stick Solution

Image
This is the solution of cut the stick problem under implementation sub domain. You can refer the question Here . Output : Code : #include<iostream> #include<algorithm> using namespace std; int main() { int mi,n,c=0; cin>>n; int a[n]; for(int i=0;i<n;i++) cin>>a[i]; sort(a,a+n); int i=0; while(a[n-1]>0) { c=0; int x=a[i]; if(a[i]>0) { for(int i=0;i<n;i++) a[i]=a[i]-x; c=n-i;cout<<c<<endl; } else { i++; } //cout<<endl; } return 0; }

Hackerrank Taum and B'day Solution

Image
This is the solution for the Taum and B'day problem in python 2, which can be found in the implementation section of the Algorithm domain. In this problem you will have T test cases,There are two types of gifts that Diksha wants from Taum: one is black and the other is white. To make her happy, Taum has to buy B number of black gifts and W number of white gifts. The cost of each black gift is X units. The cost of every white gift is Y units. The cost of converting each black gift into white gift or vice versa is Z units. You need to help Taum by deducing the minimum amount he needs to spend on Diksha's gifts. Screenshots[Demo] The Code: def bday(x,y): if(y[2]+y[1]<y[0]): print (x[0]*(y[2]+y[1]))+(x[1]*y[1]) elif(y[2]+y[0]<y[1]): print (x[1]*(y[2]+y[0]))+(x[0]*y[0]) else: print (x[0]*y[0])+(x[1]*y[1]) for i in range(0,input()): x=map(int,raw_input().split()) y=map(int,raw_input().split()); bday(x,y) Found Bugs, Please Report ! We ...