1. You are given an array and you need to find number of tripets of indices
such that the elements at those indices are in geometric progression for a given common ratio
and
.
For example,
. If
, we have
and
at indices
and
.
Function Description
Complete the countTriplets function in the editor below. It should return the number of triplets forming a geometric progression for a given
as an integer. countTriplets has the following parameter(s):
*
*
Complete the countTriplets function in the editor below. It should return the number of triplets forming a geometric progression for a given
*
arr
: an array of integers*
r
: an integer, the common ratio
Input Format
The first line contains two space-separated integers
and
, the size of
and the common ratio.
The first line contains two space-separated integers
The next line contains
space-seperated integers
.
Constraints
*
*
*![1 \le arr\left[i\right] \le 10^9 1 \le arr\left[i\right] \le 10^9](https://s0.wp.com/latex.php?latex=1+%5Cle+arr%5Cleft%5Bi%5Cright%5D+%5Cle+10%5E9&bg=ffffff&fg=000000&s=0)
*
*
*
Output Format
Return the count of triplets that form a geometric progression.
Return the count of triplets that form a geometric progression.
Sample Input 0
4 2
1 2 2 4
Sample Output 0
2
Explanation 0
There are
triplets in satisfying our criteria, whose indices are
and 
There are
Sample Input 1
6 3
1 3 9 9 27 81
Sample Output 1
6
Explanation 1
The triplets satisfying are index
,
,
,
,
and
.
The triplets satisfying are index
Solution
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Soru03 {
// Complete the countTriplets function below.
static long countTriplets(List<Long> arr, long r) {
Map<Long, Long> potential = new HashMap<>();
Map<Long, Long> counter = new HashMap<>();
long count = 0;
for (int i = 0; i < arr.size(); i++) {
long a = arr.get(i);
long key = a / r;
if (counter.containsKey(key) && a % r == 0) {
count += counter.get(key);
}
if (potential.containsKey(key) && a % r == 0) {
long c = potential.get(key);
counter.put(a, counter.getOrDefault(a, 0L) + c);
}
potential.put(a, potential.getOrDefault(a, 0L) + 1); // Every number can be the start of a triplet.
}
return count;
}
public static void main(String[] args) throws IOException {
long r = 3;
List<Long> arr = new ArrayList<Long>();
arr.add((long) 1);
arr.add((long) 3);
arr.add((long) 9);
arr.add((long) 9);
arr.add((long) 27);
arr.add((long) 81);
long ans = countTriplets(arr, r);
System.out.println(ans);
}
}
2. Harold is a kidnapper who wrote a ransom note, but now he is worried it will be traced back to him through his handwriting. He found a magazine and wants to know if he can cut out whole words from it and use them to create an untraceable replica of his ransom note. The words in his note are case-sensitive and he must use only whole words available in the magazine. He cannot use substrings or concatenation to create the words he needs.
Given the words in the magazine and the words in the ransom note, print
Yes
if he can replicate his ransom note exactly using whole words from the magazine; otherwise, print No
.
For example, the note is "Attack at dawn". The magazine contains only "attack at dawn". The magazine has all the right words, but there's a case mismatch. The answer is .
Function Description
Complete the checkMagazine function in the editor below. It must print if the note can be formed using the magazine, or .
checkMagazine has the following parameters:
- magazine: an array of strings, each a word in the magazine
- note: an array of strings, each a word in the ransom note
Input Format
The first line contains two space-separated integers, and , the numbers of words in the and the ..
The second line contains space-separated strings, each .
The third line contains space-separated strings, each .
The second line contains space-separated strings, each .
The third line contains space-separated strings, each .
Constraints
- .
- Each word consists of English alphabetic letters (i.e., to and to ).
Output Format
Print
Yes
if he can use the magazine to create an untraceable replica of his ransom note. Otherwise, print No
.
Sample Input 0
6 4
give me one grand today night
give one grand today
Sample Output 0
Yes
Sample Input 1
6 5
two times three is not four
two times two is four
Sample Output 1
No
Explanation 1
'two' only occurs once in the magazine.
Sample Input 2
7 4
ive got a lovely bunch of coconuts
ive got some coconuts
Sample Output 2
No
Explanation 2
Harold's magazine is missing the word .
Solution
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Soru04 {
static void checkMagazine(String[] magazine, String[] note) {
Hashtable<String, Integer> magazineMap = new Hashtable<String, Integer>();
for(String s : magazine){
if(magazineMap.containsKey(s)){
magazineMap.put(s, magazineMap.get(s) + 1);
}else{
magazineMap.put(s, 1);
}
}
boolean flag = true;
for(String s : note){
if(!magazineMap.containsKey(s)){
flag=false;
break;
}
int counter = magazineMap.get(s) - 1;
if(counter == 0){
magazineMap.remove(s);
}else{
magazineMap.put(s, counter);
}
}
if(flag==false) {
System.out.println("No");
}else {
System.out.println("Yes");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1[] = new String[] {"two", "times", "three", "is","not", "four"};
String s2[] = new String[] {"two", "times", "two", "is", "four"};
checkMagazine(s1, s2);
}
}
Hiç yorum yok:
Yorum Gönder