0% found this document useful (0 votes)
7 views

Proof

Uploaded by

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

Proof

Uploaded by

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

1- Hashing bruteforce

import java.util.*;

public class Main


{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int[]arr=new int[num];
for(int i=0;i<num;i++)
{
arr[i]=sc.nextInt();

int q=sc.nextInt();

for(int i=0;i<q;i++)
{
int query=sc.nextInt();

int count=0;
for(int j=0;j<num;j++)
{
if(arr[j]==query)
{
count++;
}
}
System.out.print(count);
}
}
}
2. hash approach extra space
import java.util.*;

public class Main


{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int[]arr=new int[num];
int[]hash=new int[51];
for(int i=0;i<num;i++)
{
arr[i]=sc.nextInt();
hash[arr[i]]=hash[arr[i]]+1;

int q=sc.nextInt();

for(int i=0;i<q;i++)
{
int query=sc.nextInt();

int count=hash[query];

System.out.print(count);
}
}
}

3.hashMap approach
import java.util.*;

public class Main


{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int[]arr=new int[num];
HashMap<Integer,Integer> hash=new HashMap<>();
for(int i=0;i<num;i++)
{
arr[i]=sc.nextInt();
int g=hash.getOrDefault(arr[i],0);
hash.put(arr[i],g+1);
}
int q=sc.nextInt();

for(int i=0;i<q;i++)
{
int query=sc.nextInt();

int count=hash.getOrDefault(query,0);

System.out.print(count);
}
}
}

4.Check if the given array is subset of another array

import java.util.*;
public class Main
{
public static void main(String[] args) {
int[] arr1 = {6, 7, 3, 2};
int[] arr2 = {2, 3,6,1};
int n1=arr1.length;
int n2=arr2.length;
Set<Integer>hset=new HashSet<>();
for(int i=0;i<n1;i++)
{
hset.add(arr1[i]);
}

for(int i=0;i<n2;i++)
{
if(!hset.contains(arr2[i]))
{
System.out.println("not a subset");
return;
}
}
System.out.println("subset");
}
}
5.Check if the given array is subset of another array with duplicates present) || Hashing

import java.util.*;
public class Main
{
public static void main(String[] args) {
int[] arr1 = {6, 7, 3,3};
int[] arr2 = {6,};
int n1=arr1.length;
int n2=arr2.length;
if(n1<n2)
{
System.out.println("false");
return;
}

HashMap<Integer,Integer>hmap=new HashMap<>();
for(int i=0;i<n1;i++)
{
int g=hmap.getOrDefault(arr1[i],0);
hmap.put(arr1[i],g+1);
}

for(int i=0;i<n2;i++)
{
if(!hmap.containsKey(arr2[i]))
{
System.out.println("false");
return;
}
if(hmap.get(arr2[i])==0)
{
System.out.println("false");
return;
}
int count=hmap.get(arr2[i]);
hmap.put(arr2[i],count-1);
}
System.out.println("true");
return;
}
}

6.Minimum operations to make all elements equal in an array

import java.util.*;
public class Main
{
public static void main(String[] args) {
int[] arr = {6, 7,2,3};
int n1=arr.length;
HashMap<Integer,Integer>hmap=new HashMap<>();
for(int i=0;i<n1;i++)
{
if(hmap.containsKey(arr[i]))
{
int g= hmap.getOrDefault(arr[i],0);
hmap.put(arr[i],g+1);

}
else{
hmap.put(arr[i],1);
}
}

int maxFreq=0;
for(Map.Entry<Integer,Integer>ent:hmap.entrySet())
{
int num=ent.getValue();
if(num>maxFreq)
{
maxFreq=num;
}
}

System.out.println(n1-maxFreq);
}
}

7.Check if the given array contains duplicate elements within k distance from each other

import java.util.*;
public class Main
{
public static void main(String[] args) {
int arr[] = {10, 5, 3, 6};
int k=3;
int n1=arr.length;
HashMap<Integer,Integer>hmap=new HashMap<>();
for(int i=0;i<n1;i++)
{
if(!hmap.containsKey(arr[i]))
{
hmap.put(arr[i],i);

}
else{
if( i-hmap.get(arr[i])<=k)
{
System.out.println("true " + "num "+ arr[i]);
return;

}
else{
hmap.put(arr[i],i);
}
}
}
System.out.println("false");

}
}

Find out the pair from array whose sum forms the number 'x' when both the numbers of the
pair are added
import java.util.*;
public class Main
{
public static void main(String[] args) {
int arr[] = {0, -1, 2, -3, 1}; //custom
int x = 0;
int n1=arr.length;
HashSet<Integer>hset=new HashSet<>();
for(int i=0;i<n1;i++)
{
int temp=x-arr[i];
if(hset.contains(temp))
{
System.out.println("found at "+ temp);
return;
}
hset.add(arr[i]);
}

System.out.println("not found");
}
}

Count All ((i,j) pairs such that b[i] - b[j] == k (count of


such pairs.) [i<j](pre storing )

import java.util.*;
public class Main
{
public static void main(String[] args) {
int[] arr = {1, 5, 3, 4, 2};
int k = 2;
int n1=arr.length;
HashMap<Integer,Integer>hmap=new HashMap<>();
int count=0;
for(int i=0;i<n1;i++)
{
int target=arr[i]+k;
if(hmap.containsKey(target))
{
count=count+hmap.get(target); //return the value for the key
}
hmap.put(arr[i],hmap.getOrDefault(arr[i],0) + 1 );
}
System.out.println(count);

}
}

Count all i,j pairs where i<j and abs(b[i]-b[j])


= k [k>=0] (pre storing)

import java.util.*;
public class Main
{
public static void main(String[] args) {
int[] arr = {1, 5, 3, 4, 2};
int k = 2;

int count=0;
// b[i]=b[j]+k b[i]=b[j]-k
HashMap<Integer,Integer>hmap=new HashMap<>();
for(int i=0;i<arr.length;i++)
{
if(hmap.containsKey(arr[i]+k))
{
count=count+hmap.get(arr[i]+k) ;
}
if(hmap.containsKey(arr[i]-k))
{
count=count+hmap.get(arr[i]-k) ;
}
hmap.put(arr[i],hmap.getOrDefault(arr[i],0)+1);
}
System.out.print(count);
Find Sum of Range [l……….r] where(l<=r) using Prefix
sum.
import java.util.*;

public class PrefixSum {

static int[] prefixSum(int[] nums) {


int n = nums.length;
int[] prefix = new int[n + 1];
for (int i = 1; i <= n; ++i) {
prefix[i] = prefix[i - 1] + nums[i-1];
}
return prefix;
}

static int optimizedSum(int[] prefix, int l, int r) {


return prefix[r] - prefix[l - 1];
}

public static void main(String[] args) {


int[] nums = {3,4,1,2,1,4};
int[] prefix = prefixSum(nums);

int l = 2, r = 5; // Example range [l, r]


System.out.println("Optimized Sum: " + optimizedSum(prefix, l, r));
}
}

Find count of number of subarrays with sum ==


k
Bruteforce approach1
import java.util.*;
public class Main
{
public static void main(String[] args) {
int []arr =new int[] {1, 2, 3, 4};
int k = 5;
int n=arr.length;
int count=0;
for(int j=1;j<=n;j++)
{
int currSum=0;
for(int i=j;i>=1;i--)
{
currSum=currSum+arr[i-1];
if(currSum==k)
{
count++;
}
}
}
System.out.println(count);

}
}

Bruteforce approach2
import java.util.*;
public class Main
{
public static void main(String[] args) {
int []arr =new int[] {1, 2, 3, 4,5};
int k = 5;
int n=arr.length;
int count=0;
int []prefixSum=new int [n+1];
//cal prefixSum

for(int i=1;i<=n;i++)
{
prefixSum[i]=prefixSum[i-1]+arr[i-1];
}

//count the subarrays


for(int j=1;j<=n;j++)
{
for(int i=0;i<j;i++)
{
if(prefixSum[i]==prefixSum[j]-k){ //i left part ,j right part
count++;
}
}
}
System.out.println( count);
}
}
Optimized approach
import java.util.*;
public class Main
{
public static void main(String[] args) {
int []arr =new int[] {1, 2, 3, 4,5};
int k = 5;
int n=arr.length;
int count=0;
int sum=0;
HashMap<Integer,Integer>hmap=new HashMap<>();
hmap.put(0,1);

for(int num:arr)
{
sum=sum+num;
if(hmap.containsKey(sum-k)) //checking i exists in the map
{
count=count+hmap.get(sum-k); //if yes get the previous frequency and count
}
hmap.put(sum,hmap.getOrDefault(sum,0)+1); //update frequency for every element
}
System.out.print(count);

}}

Find largest/smallest subarray with sum k in Given Array


import java.util.*;
public class Main
{
public static void main(String[] args) {
int[] arr = new int[]{3,1,3,-2,2};
int n=arr.length;

int k =4;

int maxLength=0;
int[]res={-1,-1};

for(int i=1;i<=n;i++)
{
int currSum=0;
for(int j=i;j<=n;j++)
{
currSum=currSum+arr[j-1];
if((currSum==k) && ((j-i+1) > maxLength))
{
maxLength=j-i+1;
res[0]=i;
res[1]=j;
}
}
}
System.out.print(maxLength+ " "+res[0]+" "+res[1]);

}}Optimized approach

import java.util.*;

public class Main

public static void main(String[] args) {


int[] arr = new int[]{1,2,3,4,5,-1,-4};

int n=arr.length;

int k =9;

int maxLength=0;

int res[]={-1,-1};

HashMap<Integer,Integer>hmap=new HashMap<>();

hmap.put(0,-1);

int sum=0;

for(int j=0;j<n;j++)

sum=sum+arr[j];

if(hmap.containsKey(sum-k))

int length=j-hmap.get(sum-k);

if(length>maxLength)

maxLength=length;

res[0]=hmap.get(sum-k)+1;

res[1]=j;

if(!hmap.containsKey(sum)){

hmap.put(sum,j);

}
}

System.out.print(maxLength + " "+res[0]+" "+ res[1]);

}}
Find count of shortest/largest subarrays with sum k in given array
BruteForce Approach
import java.util.*;

public class Main {


public static void main(String[] args) {

int res[]={-1,-1};

int[] arr = new int[]{1,2,3,4,2,5};


int n=arr.length;

int k =5;
int count=0;
int maxLength = 0;

for(int i=0;i<n;i++) //starting subarray


{
int sum=0;
for(int j=i;j<n;j++) //ending subarray
{
sum=sum+arr[j];
if(sum==k)
{
if((j-i+1)>maxLength) //comparing with index
{
maxLength=j-i+1;
count=1;
}
else if((j-i+1)==maxLength) //index length is same as length
{
count++;
}

}
}
}

System.out.print(count);

}
}
Optimized approach
import java.util.*;
public class Main
{
public static void main(String[] args) {
int[] arr = {3,1,2,3,6};
int n = arr.length;
int k = 6;
int maxLength=0;
int res[]={-1,-1};
HashMap<Integer,Integer>hmap=new HashMap<>();
hmap.put(0,-1);
int sum=0;

for(int j=0;j<n;j++)
{
sum=sum+arr[j];
if(hmap.containsKey(sum-k))
{
int length=j-hmap.get(sum-k);
if(length>maxLength)
{
maxLength=length;

}
}
if(!hmap.containsKey(sum)){
hmap.put(sum,j);
}
}

int count=0;
int windowSum=0;
for(int j=0;j<maxLength;j++)
{
windowSum=windowSum+arr[j];

if(windowSum==k)
{
count++;
}

for(int j=maxLength;j<n;j++)
{
windowSum=windowSum+arr[j]-arr[j-maxLength];
if(windowSum==k)
{
count++;
}
}

System.out.print(maxLength+" "+ count);

}}

valid-anagram
Given two strings s and t, return true if t is an
anagram of s, and false otherwise.

Example 1:
Input: s = "anagram", t = "nagaram"
Output: true

Optimized Approach
HashMap<Character,Integer>hmap=new HashMap<>();
int n=s.length();
if(s.length()!=t.length())
{
return false;
}
for(int i=0;i<n;i++)
{
hmap.put(s.charAt(i),hmap.getOrDefault(s.charAt(i),0)+1);
hmap.put(t.charAt(i),hmap.getOrDefault(t.charAt(i),0)-1);

}
for(int count:hmap.values())
{
if(count!=0)
{
return false;
}
}
return true;

Two-sum
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer>hmap=new HashMap<>();
int n=nums.length;
for(int i=0;i<n;i++)
{
int rem=target-nums[i];
if(hmap.containsKey(rem))
{
return new int[] {hmap.get(rem),i};
}
hmap.put(nums[i],i);
}
return new int []{};

}
}
Max distance between same elements

class Solution {
public int maxDistance(int[] arr) {
HashMap<Integer,Integer>hmap=new HashMap <>();

int n=arr.length;

int maxLength=0;
for(int i=0;i<n;i++)
{
if(hmap.containsKey(arr[i]))
{
// int length= i-hmap.get(arr[i]);
// if(length>maxLength)
// {
// maxLength=length;
// }
maxLength=Math.max(maxLength,i-hmap.get(arr[i]));
}
else
{
hmap.put(arr[i],i);

}
}
return maxLength;
}
}

First Unique Character in a String

class Solution {
public int firstUniqChar(String s) {
int n=s.length();
HashMap<Character,Integer>hmap=new HashMap<>();
int count=1;
for(int i=0;i<n;i++)
{
char c=s.charAt(i);
hmap.put(c,hmap.getOrDefault(c,0)+1);
}

for(int i=0;i<n;i++)
{
char c=s.charAt(i);
if(hmap.get(c)==1)
{
return i;
}
}
return -1;
}
}

find-common-characters

class Solution {
public List<String> commonChars(String[] words) {
String str=words[0];
HashMap<Character,Integer>hmap=new HashMap<>();
for(int i=0;i<str.length();i++)
{
char c=str.charAt(i);
hmap.put(c,hmap.getOrDefault(c,0)+1);
}

for(int i=1;i<words.length;i++)
{
String newStr=words[i];
HashMap<Character,Integer>freqMap=new HashMap<>();
for(int j=0;j<newStr.length();j++)
{
char newC=newStr.charAt(j);
freqMap.put(newC,freqMap.getOrDefault(newC,0)+1);

for(Character chr:hmap.keySet())
{
if(freqMap.containsKey(chr))
{
hmap.put(chr,Math.min(hmap.get(chr),freqMap.get(chr)));
}
else{
hmap.put(chr,0);
}
}

}
List<String>lst=new ArrayList<>();
for(Character key:hmap.keySet())
{
if(hmap.get(key)>0)
{
for(int i=0;i<hmap.get(key);i++)
{
lst.add(key+"");
}
}
}
return lst;
}
}

You might also like