Linear Search in Go

 HackerEarth Problem Solution

link HackerEarth Problem

Will she accept him?

Problem

A love Guru determines "whether a guy's proposal is going to be accepted by his crush or not" just by doing some mysterious calculation on their names, but it takes too much time for him. So, he hired you as a programmer and now your task is to write a program that helps the Guru.

He reveals his mystery with you and that is:

If the guy's name is a subsequence of his crush's name, then she is going to accept him, otherwise she will reject him.

Input

First line contains T, the number of test cases.
Next T lines contain two strings S1 and S2 which are the guy's name and his crush's name respectively.

Output

For each test case, according to Guru's mystery, if the crush is going to accept the guy then print "Love you too", otherwise print "We are only friends".

Constraints

strings contain only lowercase Latin letters. 

        

1T60

    1len(S1),len(S2)100000

 

1T60

    1len(S1),len(S2)100000

 Sample Input

4
kalyan nobodyintheworld
rahul allgirlsallhunontheplanet
physicsguy nobodynobodylikesitbetter
lal llaggyel
Sample Output
We are only friends
Love you too
We are only friends
Love you too
Time Limit: 1
Memory Limit: 256
Source Limit:



// Write your code here
package main
import(
"fmt"
"bufio"
"os"
"strconv"
"strings"
)
func main(){
var i int64=0
reader := bufio.NewReader(os.Stdin)
    test, _ := reader.ReadString('\n')
t,_:=strconv.ParseInt(strings.TrimSpace(test), 10, 64)
    
for i=0;i<t;i++{
j:=0
k:=0
s1, _ := reader.ReadString(' ')
s2, _ := reader.ReadString('\n')
for j<len(s1){
for k<len(s2){
if s1[j]==s2[k]{
j=j+1
}
k=k+1
}
if k>=len(s2)-1{
break;
}
}
if j>=len(s1)-1{
fmt.Println("Love you too")
}else{
fmt.Println("We are only friends")
}
}
}


Hexadecimal numbers 

Problem

You are given a range [L, R]. You are required to find the number of integers X in the range such that GCD(X,F(X))>1 where F(X) is equal to the sum of digits of X in its hexadecimal (or base 16) representation.

For example, F(27)=1+B=1+11=12 (27 in hexadecimal is written as 1B). You are aksed T such questions. 

Input format

  • The first line contains a positive integer T denoting the number of questions that you are asked.
  • Each of the next T lines contain two integers L and R denoting the range of questions.

Output Format

For each test case, you are required to print exactly T numbers as the output. 

Constraints

1T501L, R105

Sample Input
3
1 3
5 8
7 12
Sample Output
2
4
6

Explanation

For the first test-case, numbers which satisfy the criteria specified are : 2,3. Hence, the answer is 2.

For the second test-case, numbers which satisfy the criteria specified are : 5,6,7 and 8. Hence the answer is 4.

For the third test-case, numbers which satisfy the criteria specified are : 7,8,9,10,11 and 12. Hence the answer is 6.



package main
import(
"fmt"
"bufio"
"os"
"strconv"
"strings"
)
func hexadigit_sum(n int64 ) int64 {
var sum int64 =0
for n>0{
r:=n%16
sum+=r
n=n/16
}
return sum
}
func gcd(fnum int64,snum int64) int64{
if fnum<snum{
temp:=fnum
fnum=snum
snum=temp
}
if fnum%snum==0{
return snum
}else{
return gcd(fnum%snum,snum)
}
}

func main(){
reader:=bufio.NewReader(os.Stdin)
t,_:=reader.ReadString('\n')
test,_:=strconv.ParseInt(strings.TrimSpace(t),10,64)
var i , j int64
for i=0;i<test;i++{
c:=0
l,_:=reader.ReadString(' ')
left,_:=strconv.ParseInt(strings.TrimSpace(l),10,64)
r,_:=reader.ReadString('\n')
right,_:=strconv.ParseInt(strings.TrimSpace(r),10,64)
for j=left;j<=right;j++{
if gcd(j,hexadigit_sum(j))>1{
c+=1
}
}
fmt.Println(c)
}
}



Golden rectangles

Problem

You have N rectangles. A rectangle is golden if the ratio of its sides is in between [1.6,1.7], both inclusive. Your task is to find the number of golden rectangles.

Input format

  • First line: Integer N denoting the number of rectangles
  • Each of the N following lines: Two integers W,H denoting the width and height of a rectangle

Output format

  • Print the answer in a single line.

Constraints

1N105

1W,H109

Sample Input
5
10 1
165 100
180 100
170 100
160 100

Sample Output
3

Explanation

There are three golden rectangles: (165, 100), (170, 100), (160, 100).

// Write your code here
package main
import (
"fmt"
"bufio"
"strconv"
"strings"
"os"
)
func main(){
var i ,c int64=0,0
reader:=bufio.NewReader(os.Stdin)
t,_:=reader.ReadString('\n')
test,_:=strconv.ParseInt(strings.TrimSpace(t),10,64)
for i=0;i<test;i++{
w,_:=reader.ReadString(' ')
l,_:=strconv.ParseFloat(strings.TrimSpace(w),64)
h,_:=reader.ReadString('\n')
r,_:=strconv.ParseFloat(strings.TrimSpace(h),64)
if (l/r>=1.6 && l/r<=1.7) || (r/l>=1.6 && r/l<=1.7){
c+=1
}

}
fmt.Println(c)
}
}



































 

 



Comments