Arrays(DSA)
1.Peak element
/*Complete the function below*/
class Solution
{
// Function to find the peak element
// arr[]: input array
// n: size of array a[]
public int peakElement(int[] arr,int n)
{
//add code here.
if(n==1)
return 0;
if(arr[0]>=arr[1])
return 0;
if(arr[n-1]>=arr[n-2])
return n-1;
for(int i=1;i<n-1;i++){
if(arr[i]>=arr[i-1] && arr[i]>=arr[i+1]){
return i;
}
}
return 0;
}
}
2.Find minimum and maximum element in an array
class Compute
{
static pair getMinMax(long a[], long n)
{
//Write your code here
long min = a[0];
long max = a[0];
for (int i = 1; i < n; i++) {
if (a[i] < min) {
min = a[i];
}
if (a[i] > max) {
max = a[i];
}
}
return new pair(min, max);
}
}
3.Reverse a String
class Reverse
{
// Complete the function
// str: input string
public static String reverseWord(String str)
{
// Reverse the string str
char arr[]=str.toCharArray();
int n=arr.length;
int i=0;
int j=n-1;
while(i<j){
char temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
String result = Arrays.toString(arr);
String newStr = result.replace(",", "").replaceAll("[^a-zA-Z0-9]", "");
newStr=String.join("",newStr.split(""));
return newStr;
}
}
3.Sort The Array
//User function Template for Java
class Solution
{
int[] sortArr(int[] arr, int n)
{
// code here
Arrays.sort(arr);
return arr;
}
}
//learn all the sorting algorithms
4.Kth smallest element
class Solution{
public static int kthSmallest(int[] arr, int l, int r, int k)
{
//Your code here
Arrays.sort(arr);
return arr[k-1];
}
}
5.Find the Frequency
class Solution{
int findFrequency(int A[], int x){
int n=A.length;
int count=0;
for(int i=0;i<n;i++){
if(A[i]==x){
count++;
}
}
return count;
}
}
6.Move all negative elements to end
class Solution {
public void segregateElements(int arr[], int n)
{
// Your code goes here
if(n==0||n==1){
return;
}
int a[]=new int[n];
int i=0;
for(int x=0;x<n;x++){
if(arr[x]>=0){
a[i]=arr[x];
i++;
}
}
for(int x=0;x<n;x++){
if(arr[x]<0){
a[i]=arr[x];
i++;
}
}
for(int x=0;x<n;x++){
arr[x]=a[x];
}
}
}
7. Union of two arrays
class Solution{
public static int doUnion(int a[], int n, int b[], int m)
{
//Your code here
Set<Integer> unionSet = new HashSet<>();
for(int i=0; i<n; i++) {
unionSet.add(a[i]);
}
for(int i=0; i<m; i++) {
unionSet.add(b[i]);
}
return unionSet.size();
}
}
8.Cyclically rotate an array by one
class Compute {
public void rotate(int arr[], int n)
{
int temp=arr[n-1];
for(int i=n-2;i>=0;i--){
arr[i+1]=arr[i];
}
arr[0]=temp;
}
}
9.Common elements
class Solution
{
ArrayList<Integer> commonElements(int A[], int B[], int C[], int n1, int n2, int n3)
{
ArrayList<Integer> arr = new ArrayList<>();
int i = 0, j = 0, k = 0;
// Iterate through all three arrays while they have elements
while (i < n1 && j < n2 && k < n3) {
// If the current element is common to all three arrays, add it to the result
if (A[i] == B[j] && B[j] == C[k]) {
// To handle duplicates, we need to check if the element we're adding to the
// result is the same as the last element we added.
if (arr.isEmpty() || arr.get(arr.size() - 1) != A[i]) {
arr.add(A[i]);
}
i++;
j++;
k++;
}
// If the current element is smaller than the element in the other arrays,
// move to the next element in the current array
else if (A[i] < B[j] && A[i] < C[k]) {
i++;
} else if (B[j] < A[i] && B[j] < C[k]) {
j++;
} else {
k++;
}
}
if (arr.isEmpty()) {
arr.add(-1);
}
return arr;
}
}
10.First Repeating Element
class Solution {
// Function to return the position of the first repeating element.
public static int firstRepeated(int[] arr, int n) {
// Your code here
HashMap<Integer, Integer> map = new HashMap<>();
int firstIndex = Integer.MAX_VALUE;
for (int i = n - 1; i >= 0; i--) {
int num = arr[i];
if (map.containsKey(num)) {
firstIndex = Math.min(firstIndex, map.get(num));
} else {
map.put(num, i);
}
}
return (firstIndex == Integer.MAX_VALUE) ? -1 : firstIndex + 1;
}
}
Comments
Post a Comment