0% found this document useful (0 votes)
15 views9 pages

Hackertest

Uploaded by

leena swarnaker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views9 pages

Hackertest

Uploaded by

leena swarnaker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

BufferedReader reader =new BufferedReader(new

InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine()) ;

Reading data:
BufferedReader reader =new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter your name: ");
String name = reader.readLine();
System.out.println("Enter your age: ");
int age = Integer.parseInt(reader.readLine());
System.out.println("Enter your Id: ");
int id = Integer.parseInt(reader.readLine());

class Solution {
public int myAtoi(String s) {
char ch[]=s.toCharArray();
long sum=0;
int zeroAscii=(int)'0';
for(char c:ch)
{
int tmpAscii=(int)c;
sum=sum*10+ (tmpAscii-zeroAscii) ;
}
//System.out.println(sum);
return (int)sum;
}
}
=======================================================

public static Set<String> getPermutation(String str) {

// create a set to avoid duplicate permutation


Set<String> permutations = new HashSet<String>();

// check if string is null


if (str == null) {
return null;
} else if (str.length() == 0) {
// terminating condition for recursion
permutations.add("");
return permutations;
}
// get the first character
char first = str.charAt(0);

// get the remaining substring


String sub = str.substring(1);

// make recursive call to getPermutation()


Set<String> words = getPermutation(sub);

// access each element from words


for (String strNew : words) {
for (int i = 0;i<=strNew.length();i++){
// insert the permutation to the set
permutations.add(strNew.substring(0, i) + first + strNew.substring(i));
}
}
return permutations;
}
==========================================================
class Solution {
public void permuteHelper(List<List<Integer>> list,List<Integer> resultList,
int[] ar )
{
if(resultList.size()==ar.length)
{
list.add(new ArrayList<Integer>());
}else
{
for(int i=0;i<ar.length;i++)
{
if(resultList.contains(ar[i]))
continue;
resultList.add(ar[i]);
permuteHelper(list,resultList,ar);
//resultList.remove(resultList.size()-1);
}
}
}
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> list=new ArrayList<>();
permuteHelper(list,new ArrayList<>(),nums);
return list;
}
}

class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
if (nums.length ==1 ) return 1;
if (nums.length ==2 ) return 2;

int i = 0 ;
int j = 0 ;
int count =0;

while(j < nums.length)


{
while(j < nums.length-1 && nums[j] == nums[j+1] )
{ j++;
nums[i++] = nums[j++];
}

return i;
}
}

static String pangrams(String s)


{
int count=0;
HashSet<Integer> hs=new HashSet<Integer>();
for(int i=0;i<s.length();i++ )
{
if(s.charAt(i)!=' ')
{
if( ( (int)s.charAt(i)>=65 && (int)s.charAt(i)<=90) ||
( (int)s.charAt(i)>=97 && (int)s.charAt(i)<=122) )
{
hs.add((int)s.charAt(i));
count++;
}
}
}
if(hs.size()==26)
return "pangram";
else
return "not pangram";
}
===========================================================================

static int[] missingNumbers(int[] arr, int[] brr)


{ int count=0;
int []missing=new int[brr.length-arr.length];
int t=0;
ArrayList<Integer> a=new ArrayList<Integer>(); // Chota
ArrayList<Integer> b=new ArrayList<Integer>(); // Bada
for(int i=0;i<arr.length;i++)
a.add(arr[i]) ;
for(int i=0;i<brr.length;i++)
b.add(brr[i]) ;

for (int i = 0; i < a.size(); i++)


{
b.remove(new Integer(a.get(i)));
}
for(int i=0;i<b.size();i++ )
missing[t++]=b.get(i);

Arrays.sort(missing);
return missing ;
}

==================================================================
int count = 0;
HashSet<Integer> set = new HashSet<>();
for(int num : arr)
{
set.add(num);
}

for(int number : arr)


{
if(set.contains(number + k))
{
count++;
}
}
return count;
==========================================================
static String funnyString(String s)
{
for (int i = 0; i < s.length() / 2; i++)
{
int l = (s.length() - 1) - i;
int a = (int)(s.charAt(i));
int b = (int)(s.charAt(i+1));
int c = (int)(s.charAt(l));
int d = (int)(s.charAt(l-1));

if (Math.abs(a - b) != Math.abs(c - d))


return "Not Funny";
}
return "Funny";

=================================================

class Solution {
private boolean isVowel(char c)
{
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A'
|| c == 'E'
|| c == 'I' || c == 'O' || c == 'U';
}
public String reverseVowels(String s)
{
char[] chrs = s.toCharArray();
int i = 0, j = chrs.length - 1;
while (i < j) {
if (isVowel(chrs[i]) && isVowel(chrs[j])) {
char t = chrs[i];
// remember moving the index after the exchange of values
chrs[i++] = chrs[j];
chrs[j--] = t;
} else if (isVowel(chrs[i])) {
j--;
} else i++;
}

return String.valueOf(chrs);
}
}

public int minSwapsCouples(int[] a) {


int n = a.length;
int cnt = 0;
int[] pos = new int[n];
for(int i=0;i<n;i++) pos[a[i]]=i;
for(int i=0;i<n;i+=2){
int f = Math.min(a[i], a[i+1]);
int s = Math.max(a[i], a[i+1]);
if(!(s-f==1 && s%2==1)){
int j = (f&1)==1 ? pos[f-1] : pos[f+1];
a[j]=s;
pos[s]=j;
cnt++;
}
}
return cnt;
===================================================================================
===
public int minSwapsCouples(int[] row) {
int swapNeeded = 0;
if(row == null || row.length == 0) return swapNeeded;

for (int i = 0; i < row.length; i+=2) {


if(row[i]/2 != row[i+1]/2){
for (int j = i+2; j < row.length; j++) {
if(row[i]/2 == row[j]/2){
int temp = row[i+1];
row[i+1] = row[j];
row[j] = temp;
swapNeeded++;

break;
}
}
}
}

return swapNeeded;
}
}
===================================================================================
=========

class Solution {
public int firstMissingPositive(int[] nums) {
Set<Integer> s = new HashSet<Integer>();
for(int j:nums){
if(j>=0 && !s.contains(j))
s.add(j);
}
for(int i=1;i<=s.size();i++){
if(!s.contains(i)){
return i;
}
}
return s.size()+1;
}
}
===================================================================================
=====

public String[] findRestaurant(String[] list1, String[] list2) {

ArrayList<String> list = new ArrayList<>();

HashMap<String, Integer> map = new HashMap<>();

for(int i = 0; i<list1.length; i++){


for(int j = 0; j < list2.length;j++){

if(list1[i].equals(list2[j])){

map.put(list1[i], i+j);

}
}
}

int sum_min_index = Collections.min(map.values());

for(Map.Entry<String, Integer> set: map.entrySet()){

if(set.getValue() == sum_min_index){

list.add(set.getKey());
}
}

return list.toArray(new String[0]);


}
================================================================

public int subarraySum(int[] nums, int k)


{
int count=0;

for(int i=0;i<nums.length;i++ )
{
int sum=0;
for(int j=i;j<nums.length;j++)
{ sum+=nums[j];
if(sum==k)
count++;
}
}
return count;
}
===========================================================
public int subarraysDivByK(int[] A, int K)
{
int sum=0,result=0;
int map[]=new int[K];
map[0]=1;
for(int i=0;i<A.length;i++)
{
sum+=A[i];
int temp=sum%K;
if(temp<0) temp+=K;
result+=map[temp]++ ;
}
return result;
}
====================================================

class Solution {
public int subarraysDivByK(int[] nums, int k) {
int[] map = new int[k];
int ans = 0,sum = 0;
map[0] = 1;
for(int i=0;i<nums.length;i++){
sum += nums[i];
int temp = sum % k;
if(temp < 0) temp += k;
ans += map[temp]++;
}

return ans;
}
}
=====================================================

public static int height(Node root) {

int leftHeight = 0;
int rightHeight = 0;

if (root.left != null) {
leftHeight = 1 + height(root.left);
}

if (root.right != null) {
rightHeight = 1 + height(root.right);
}

return leftHeight > rightHeight ? leftHeight : rightHeight;


}
======================================================
void top_view(Node root)
{
left_view(root.left);
System.out.print(root.data + " ");
right_view(root.right);
}

void left_view(Node root) {


if (root == null) return;
left_view(root.left);
System.out.print(root.data + " ");
}

void right_view(Node root) {


if (root == null) return;
System.out.print(root.data + " ");
right_view(root.right);
}
====================================================

public boolean isPalindrome(String s)


{
s=s.replaceAll("[^A-Za-z0-9]","");
if(s==null || s.length()==0)
return true;
StringBuffer sb=new StringBuffer();
sb.append(s);
sb.reverse();
if(s.equalsIgnoreCase(sb.toString()))
return true;
else
return false;
}
=================================================

public boolean validPalindrome(String s) {


int i = 0, j = s.length() - 1;
while(i < j){
if(s.charAt(i) != s.charAt(j))
return isValid(s.substring(i + 1, j + 1)) || isValid(s.substring(i,
j));
i++; j--;
}
return true;
}
private boolean isValid(String s){
int i = 0, j = s.length() - 1;
while(i < j){
if(s.charAt(i) != s.charAt(j)) return false;
i++; j--;
}
return true;
}
======================================================================

class Solution {
public boolean isSymmetric(TreeNode root)
{
if(root==null ) return false;

TreeNode invertTreeFlag=invertTree(root);
if(root==invertTreeFlag )
return true;
else
return false;
}

TreeNode invertTree(TreeNode node)


{
if(node==null ) return node;

TreeNode temp=invertTree(node.left);
node.left=invertTree(node.right);
node.right=temp;
return node;
}
}
========================================================================

static int[] countingSort(int[] arr)


{
int[] res=new int[100];
ArrayList<Integer> a=new ArrayList<Integer>();
for(int j=0;j<arr.length;j++)
a.add(Integer.valueOf(arr[j]));
for(int w=0;w<100;w++)
{
res[w]=Collections.frequency(a,w);
}
return res;
}

You might also like