Accenture Test 6 Hackerrank
Accenture Test 6 Hackerrank
Example 1:
Input:
N = 3
list = {feel, good, feel}
Output: 1
Explanation: 'feel' is the only word that
appears twice.
Example 2:
Input:
N = 8
list = {Tom, Jerry, Thomas, Tom, Jerry,
Courage, Tom, Courage}
Output: 2
Explanation: 'Jerry' and 'Courage' are the
only words that appears twice.
input:
n=3
ram Ram ramesh
0
input:
n=4
ramu ramu kishore kishore
2
*/
//HashMap Simple Example
import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
HashMap<String,Integer>map=new HashMap<>();
for(int i=0;i<n;i++){
String str=s.next();
if(map.containsKey(str)){
map.put(str,map.get(str)+1);
}else{
map.put(str,1);
}
}
int count=0;
for(String str:map.keySet()){
if(map.get(str)==2)
count++;
}
System.out.println(count);
}
}
--------------------------------
Given an array containing 0s and 1s. Find the number of subarrays having equal
number of 0s and 1s.
Example 1:
Input:
n = 7
A[] = {1,0,0,1,0,1,1}
Output: 8
Explanation: The index range for the 8
sub-arrays are: (0, 1), (2, 3), (0, 3), (3, 4),
(4, 5) ,(2, 5), (0, 5), (1, 6)
Example 2:
Input:
n = 5
A[] = {1,1,1,1,0}
Output: 1
Explanation: The index range for the
subarray is (3,4).
import java.util.*;
import java.lang.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) {
int n = in.nextInt();
int [] a = new int[n];
for(int i=0;i<n;i++) {
a[i] = in.nextInt();
}
System.out.println(new
countsubArray().countSubarrWithEqualZeroAndOne(a, n));
}
}
class countsubArray
{
// arr[] : the input array
// N : size of the input array
sum+=arr[i];
if(sum==0)
count++;
if(map.containsKey(sum))
count+=map.get(sum);
if(!map.containsKey(sum))
map.put(sum,1);
else
map.put(sum,map.get(sum)+1);
}
return count;
}
}
-----------------------------------------------------------------------------------
-
Given an array arr[ ] of N positive integers, the task is to find the greatest
element on the left of every element in the array which is strictly smaller than
itself, if this element does not exist for an index print "-1".
Example 1:
Input:
N = 5
arr[] = {2, 3, 4, 5, 1}
Output:
-1 2 3 4 -1
Explanation:
Greatest element on the left of 3 smaller
than itself is 2, for 4 it is 3 and for 5
it is 1. Since 2 is the first element and
no element on its left is present, so it's
greatest smaller element will be -1 and for
1 no element smaller than itself is present
on its left, so it's greatest smaller element
is -1.
N=7
arr[]=8 9 6 4 7 5 2
output:
-1 8-1-1 6 4 -1
Example 2:
Input:
N = 3
arr[] = {1, 2, 3}
Output:
-1 1 2
Input:
N=8
arr[]=9 8 7 5 6 4 1 2
output:
-1 -1 -1 -1 5 -1 -1 1
Your Task:
This is a function problem. The input is already taken care of by the driver code.
You only need to complete the function Smallestonleft() that takes an array arr[ ]
and sizeOfArray N, and return the required answer. The driver code takes care of
the printing.
Constraints:
1 ≤ N ≤ 106
1 ≤ arr[i] ≤ 108
-------------------------------
import java.lang.*;
import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int []a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=in.nextInt();
}
TreeSet<Integer>m=new TreeSet<Integer>();
m.add(a[0]);
System.out.print("-1 ");
for(int i=1;i<n;i++)
{
if(m.lower(a[i])==null)
{
System.out.print("-1 ");
}
else
{
System.out.print(m.lower(a[i])+" ");
}
m.add(a[i]);
}
System.out.println();
}
}