```class Solution {
public:
int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {
vector<vector<int>> gr(n+1);
vector<int> indegree(n+1, 0), prev_time(n+1, 0);
for(auto& edge : relations){
gr[edge[0]].push_back(edge[1]);
indegree[edge[1]]++;
}
time.insert(time.begin(), 0);
queue<int> q;
for(int i =1; i <= n; i++)
if(indegree[i] == 0)
q.push(i);
while(!q.empty()){
int node = q.front();q.pop();
}
}
int ans = 0;
for(int i =1 ; i < n + 1; ++i){
ans = max(ans, time[i]+prev_time[i]);
}
return ans;
}
};```
```const paginate = (array) => {
const itemsPerPage = 10;
const numberOfPages = Math.ceil( array.length / itemsPerPage);

// need to create an array of arrays [[10], [20], [30]] etc
return Array.from({ length: numberOfPages }, (_, pageIndex) => {
const startIndex = pageIndex * itemsPerPage;
return array.slice(startIndex, startIndex + itemsPerPage);
});

}

export default paginate```
```const winners = ["Jane", "Bob"];
const losers = ["Ronald", "Kevin"];

const players = [...winners, ...losers];
```
```## Instead of building a list with a loop:

b = []
for x in a:
b.append(10 * x)
foo(b)

## you can often build it much more concisely
## with a list comprehension:

foo([10 * x for x in a])

##or, if foo accepts an arbitrarily iterable (which it usually will), ## a generator expression:

foo(10 * x for x in a) ```
```class Solution
{
//Function to find median of the array elements.
public int median(int A[],int N)
{

Arrays.sort(A);
int k=N/2;
if(N%2!=0){
return A[k];
}
else{
return (A[k-1]+A[k])/2;
}
}
//Function to find median of the array elements.
public int mean(int A[],int N)
{
int sum=0;
for(int i=0;i<N;i++)
{
sum=sum+A[i];
}
return sum/N;
}

}```
```class Solution {
// Function to find element with more appearances between two elements in an
// array.
public int majorityWins(int arr[], int n, int x, int y)
{
int countX=0;
int countY=0;
for(int i=0;i<n;i++)
{
if(arr[i]==x)
countX++;

if(arr[i]==y)
countY++;
}

if(countX>countY)
return x;

else if(countX==countY && x<y)
return x;

else
return y;
}
}```
```class Solution{

// Function to find largest and second largest element in the array
public static ArrayList<Integer> largestAndSecondLargest(int sizeOfArray, int arr[])
{
//code here.
int largest=-1;
int sec_largest=-1;
for(int i=0;i<sizeOfArray;i++)
{
largest = Math.max(largest,arr[i]);
}
for(int i=0;i<sizeOfArray;i++)
{
if(arr[i]<largest)
{
sec_largest= Math.max(sec_largest,arr[i]);
}
}
ArrayList<Integer> al = new ArrayList<Integer>(2);
return al;
}
}
}```
```class StrongestNeighbour{

// Function to find maximum for every adjacent pairs in the array.
static void maximumAdjacent(int sizeOfArray, int arr[])
{
int sum=0;
for(int i=0; i<sizeOfArray-1; i++)
{
sum = Math.max(arr[i], arr[i+1]);
System.out.print(sum+" ");

}
}
}```
```class Solution{
//Function to reverse any part of the array.
void reverse(ArrayList<Integer> arr, int n,int left, int right)
{
//reversing the sub-array from left index to right index.
while (left < right) {
//swapping values at index stored at left and right index.
int temp = arr.get(left);
arr.set(left, arr.get(right));
arr.set(right, temp);

//updating values of left and right index.
left+=1;
right-=1;
}
}

//Function to reverse every sub-array group of size k.
void reverseInGroups(ArrayList<Integer> arr, int n, int k) {
for (int i = 0; i < n; i += k) {

//If (ith index +k) is less than total number of elements it means
//k elements exist from current index so we reverse k elements
//starting from current index.
if(i+k < n){
//reverse function called to reverse any part of the array.
reverse(arr,n,i,i+k-1);
}
//Else k elements from current index doesn't exist.
//In that case we just reverse the remaining elements.
else{
//reverse function called to reverse any part of the array.
reverse(arr,n,i,n-1);
}

}
}
}```
```class Solution{

//Function to find minimum adjacent difference in a circular array.
// arr[]: input array
// n: size of array
public static int minAdjDiff(int arr[], int n) {

int min=Math.abs(arr[0]-arr[n-1]),diff;
for(int i=0;i<n-1;i++)
{
diff=Math.abs(arr[i]-arr[i+1]);
if(diff<min)
min=diff;
}
return min;

}
}```
```class Solution{

//Function to swap two elements in the array.
public static void swap(int arr[], int x, int y){
//Use of a temporary variable to swap the elements.
int tmp = arr[x];
arr[x] = arr[y];
arr[y] = tmp;
}
//Function to sort the array into a wave-like array.
public static void convertToWave(int arr[], int n){

//Iterating over the array.
for(int i=0;i<n-1;i=i+2){
//Swapping two neighbouring elements at a time.
swap(arr, i, i+1);
}
return;
}

}```
```class Solution{
//Function to count the frequency of all elements from 1 to N in the array.
public static void frequencyCount(int arr[], int N, int P)
{
//Decreasing all elements by 1 so that the elements
//become in range from 0 to n-1.
int maxi = Math.max(P,N);
int count[] = new int[maxi+1];
Arrays.fill(count, 0);
for(int i=0;i<N;i++){
count[arr[i]]++;
}

for(int i=0;i<N;i++){
arr[i] = count[i+1];
}
}
}```
```class Solution {
// Function to find equilibrium point in the array.
public static int equilibriumPoint(long a[], int n) {

//We store the sum of all array elements.
long sum = 0;
for (int i = 0; i < n; i++)
sum += a[i];

//sum2 is used to store prefix sum.
long sum2 = 0;
int ans = -1;

for (int i = 0; i < n; i++) {

//Leaving out the value of current element from suffix sum.
sum = sum - a[i];

//Checking if suffix and prefix sums are same.
if (sum2 == sum) {
//returning the index or equilibrium point.
return (i + 1);
}

//Adding the value of current element to prefix sum.
sum2 = sum2 + a[i];
}
return -1;
}
}```
```class Solution{
//Function to find the leaders in the array.
static ArrayList<Integer> leaders(int arr[], int n){

int maxEle = arr[n-1];

ArrayList<Integer> res = new ArrayList<>();

//We start traversing the array from last element.
for(int i=n-1; i>=0; i--) {

//Comparing the current element with the maximum element stored.
//If current element is greater than max, we add the element.
if(arr[i] >= maxEle){
//Updating the maximum element.
maxEle = arr[i];
//Storing the current element in arraylist for leaders.
}
}

//Reversing the arraylist.
Collections.reverse(res);
//returning the arraylist.
return res;
}

}```
```class Solution
{
//Function that puts all non-positive (0 and negative) numbers on left
//side of arr[] and return count of such numbers.
static int segregate (int arr[], int size)
{
int j = 0, i;
for(i = 0; i < size; i++)
{
if (arr[i] <= 0)
{
int temp;
//changing the position of negative numbers and 0.
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
//incrementing count of non-positive integers.
j++;
}
}
return j;
}

//Finding the smallest positive missing number in an array
//that contains only positive integers.
static int findMissingPositive(int arr[], int size)
{
int i;
//marking arr[i] as visited by making arr[arr[i] - 1] negative.
//note that 1 is subtracted because indexing starts from 0 and
//positive numbers start from 1.
for(i = 0; i < size; i++)
{
if(Math.abs(arr[i]) - 1 < size && arr[Math.abs(arr[i]) - 1] > 0)
arr[Math.abs(arr[i]) - 1] = -arr[Math.abs(arr[i]) - 1];
}

for(i = 0; i < size; i++)
{
if (arr[i] > 0)
{
//returning the first index where value is positive.
// 1 is added because indexing starts from 0.
return i+1;
}
}
return size+1;
}

//Function to find the smallest positive number missing from the array.
static int missingNumber(int arr[], int size)
{
//first separating positive and negative numbers.
int shift = segregate (arr, size);

int arr2[] = new int[size-shift];
int j=0;
//shifting the array to access only positive part.
for(int i=shift;i<(size);i++)
{
arr2[j] = arr[i];
j++;
}

//calling function to find result and returning it.
return findMissingPositive(arr2, j);
}

}```
```class Solution{

//Function to rearrange  the array elements alternately.
public static void rearrange(long arr[], int n)
{
//Initialising index of first minimum and first maximum element.
int max_idx = n - 1, min_idx = 0;

//Storing maximum element of array.
long max_elem = arr[n - 1] + 1;

for (int i = 0; i < n; i++) {

//At even index, we have to put maximum elements in decreasing order.
if (i % 2 == 0) {
arr[i] += (arr[max_idx] % max_elem) * max_elem;
//Updating maximum index.
max_idx--;
}

//At odd index, we have to put minimum elements in increasing order.
else {
arr[i] += (arr[min_idx] % max_elem) * max_elem;
//Updating minimum index.
min_idx++;
}
}

//Dividing array elements by maximum element to get the result.
for (int i = 0; i < n; i++)
arr[i] = arr[i] / max_elem;

}

}```
```class Solution
{
//Function to rearrange an array so that arr[i] becomes arr[arr[i]]
//with O(1) extra space.
static void arrange(long arr[], int n)
{
int i = 0;

//Increasing all values by (arr[arr[i]]%n)*n to store the new element.
for(i = 0; i < n; i++)
arr[(int)i]+=(arr[(int)arr[(int)i]]%n)*n;

//Since we had multiplied each element with n.
//We will divide by n too to get the new element at that
//position after rearranging.
for(i = 0; i < n; i++)
arr[(int)i] = arr[(int)i]/n;
}
}```
```class Solution{
// Function to find the maximum index difference.
static int maxIndexDiff(int arr[], int n) {
if(n==1){
return 0;
}
int RMax[] = new int[n];
int LMin[] = new int[n];

//Constructing LMin[] such that LMin[i] stores the minimum value
//from (arr[0], arr[1], ... arr[i]).
LMin[0] = arr[0];
for (int i = 1; i < n; ++i)
LMin[i] = Integer.min(arr[i], LMin[i - 1]);

//Constructing RMax[] such that RMax[j] stores the maximum value
//from (arr[j], arr[j+1], ..arr[n-1]).
RMax[n - 1] = arr[n - 1];
for (int j = n - 2; j >= 0; --j)
RMax[j] = Integer.max(arr[j], RMax[j + 1]);

int i = 0, j = 0, maxDiff = -1;
//Traversing both arrays from left to right to find optimum j-i.
//This process is similar to merge() of MergeSort.
while (j < n && i < n) {
if (LMin[i] <= RMax[j]) {
//Updating the maximum difference.
maxDiff = Integer.max(maxDiff, j - i);
j++;
} else
i++;
}
//returning the maximum difference.
return maxDiff;
}
}```
```class Interval {
int sell;
}

class Solution{
//Function to find the days of buying and selling stock for max profit.
ArrayList<ArrayList<Integer> > stockBuySell(int A[], int n) {

ArrayList<ArrayList<Integer> > result = new ArrayList<ArrayList<Integer> >();
//Prices must be given for at least two days else return the empty result.
if(n==1){
return result;
}

//Creating solution vector.
ArrayList<Interval> sol = new ArrayList<Interval>();
int i=0, cnt=0;
//Traversing through given price array.
while (i < n-1) {
//Finding Local Minima. Note that the limit of loop is (n-2)
//as we are comparing present element to the next element.
while ((i < n-1) && (A[i+1] <= A[i])){
i++;
}
//If we reach the end, we break loop as no further
//solution is possible.
if (i == n-1){
break;
}
Interval e = new Interval();
//Storing the index of minima which gives the day of buying stock.

//Finding Local Maxima. Note that the limit of loop is (n-1)
//as we are comparing present element to previous element.
while ((i < n) && (A[i] >= A[i-1]))
i++;

//Storing the index of maxima which gives the day of selling stock.
e.sell = i-1;
cnt++;
}
if(cnt==0){
return result;
} else {
//Storing the buy/sell pairs in a list.
for(int j=0; j<sol.size(); j++){
}
}
//returning the result.
return result;
}

}
```
```import java.io.*;

class GFG {

// Function to check if an array is
// Sorted and rotated clockwise
static boolean checkIfSortRotated(int arr[], int n)
{
// Initializing two variables x,y as zero.
int x = 0, y = 0;

// Traversing array 0 to last element.
// n-1 is taken as we used i+1.
for (int i = 0; i < n - 1; i++) {
if (arr[i] < arr[i + 1])
x++;
else
y++;
}

// If till now both x,y are greater
// then 1 means array is not sorted.
// If both any of x,y is zero means
// array is not rotated.
if (x == 1 || y == 1) {
// Checking for last element with first.
if (arr[n - 1] < arr[0])
x++;
else
y++;

// Checking for final result.
if (x == 1 || y == 1)
return true;
}
// If still not true then definitely false.
return false;
}

// Driver code
public static void main(String[] args)
{
int arr[] = { 5, 1, 2, 3, 4 };

int n = arr.length;

// Function Call
boolean x = checkIfSortRotated(arr, n);
if (x == true)
System.out.println("YES");
else
System.out.println("NO");
}
}
```
```1.) Maximum subarray size, such that all subarrays of that size have sum less than k:
-------------------------------------------------------------------------------------
Given an array of n positive integers and a positive integer k, the task is to find the maximum 	subarray size such that all subarrays of that size have the sum of elements less than k.

https://www.geeksforgeeks.org/maximum-subarray-size-subarrays-size-sum-less-k/

2.) Find the prime numbers which can written as sum of most consecutive primes:
-------------------------------------------------------------------------------
Given an array of limits. For every limit, find the prime number which can be written as the 	 sum of the most consecutive primes smaller than or equal to the limit.

https://www.geeksforgeeks.org/find-prime-number-can-written-sum-consecutive-primes/

3.) Longest Span with same Sum in two Binary arrays :
-----------------------------------------------------
Given two binary arrays, arr1[] and arr2[] of the same size n. Find the length of the longest       common span (i, j) where j >= i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] +           arr2[i+1] + …. + arr2[j].

https://www.geeksforgeeks.org/longest-span-sum-two-binary-arrays/

3.) Maximum subarray sum modulo m:
----------------------------------
Given an array of n elements and an integer m. The task is to find the maximum value of the sum 	of its subarray modulo m i.e find the sum of each subarray mod m and print the maximum value of 	this modulo operation.

https://www.geeksforgeeks.org/maximum-subarray-sum-modulo-m/

4.) Maximum subarray size, such that all subarrays of that size have sum less than k:
-------------------------------------------------------------------------------------
Given an array of n positive integers and a positive integer k, the task is to find the maximum 	subarray size such that all subarrays of that size have sum of elements less than k.

https://www.geeksforgeeks.org/maximum-subarray-size-subarrays-size-sum-less-k/

5.) Minimum cost for acquiring all coins with k extra coins allowed with every coin:
---------------------------------------------------------------------------------
You are given a list of N coins of different denominations. you can pay an amount equivalent to 	any 1 coin and can acquire that coin. In addition, once you have paid for a coin, we can choose 	at most K more coins and can acquire those for free. The task is to find the minimum amount 	required to acquire all the N coins for a given value of K.

https://www.geeksforgeeks.org/minimum-cost-for-acquiring-all-coins-with-k-extra-coins-allowed-with-every-coin/

6.) Random number generator in arbitrary probability distribution fashion:
----------------------------------------------------------------------
Given n numbers, each with some frequency of occurrence. Return a random number with a 			probability proportional to its frequency of occurrence.

https://www.geeksforgeeks.org/random-number-generator-in-arbitrary-probability-distribution-fashion/```
```// Efficient : (HASHING Using Prefix Sum) : Time Complexity: O(n), Auxiliary Space: O(n)

class LargestSubArray {

// This function Prints the starting and ending
// indexes of the largest subarray with equal
// number of 0s and 1s. Also returns the size
// of such subarray.

int findSubArray(int arr[], int n)
{
int sum = 0;
int maxsize = -1, startindex = 0;
int endindex = 0;

// Pick a starting point as i

for (int i = 0; i < n - 1; i++) {
sum = (arr[i] == 0) ? -1 : 1;

// Consider all subarrays starting from i

for (int j = i + 1; j < n; j++) {
if (arr[j] == 0)
sum += -1;
else
sum += 1;

// If this is a 0 sum subarray, then
// compare it with maximum size subarray
// calculated so far

if (sum == 0 && maxsize < j - i + 1) {
maxsize = j - i + 1;
startindex = i;
}
}
}
endindex = startindex + maxsize - 1;
if (maxsize == -1)
System.out.println("No such subarray");
else
System.out.println(startindex + " to " + endindex);

return maxsize;
}

/* Driver program to test the above functions */

public static void main(String[] args)
{
LargestSubArray sub;
sub = new LargestSubArray();
int arr[] = { 1, 0, 0, 1, 0, 1, 1 };
int size = arr.length;

sub.findSubArray(arr, size);
}
}
```
```// A Java program to find
// if there is a zero sum subarray
import java.util.HashSet;
import java.util.Set;

class ZeroSumSubarray
{
// Returns true if arr[]
// has a subarray with sero sum
static Boolean subArrayExists(int arr[])
{
// Creates an empty hashset hs
Set<Integer> hs = new HashSet<Integer>();

// Initialize sum of elements
int sum = 0;

// Traverse through the given array
for (int i = 0; i < arr.length; i++)
{
// Add current element to sum
sum += arr[i];

// Return true in following cases
// a) Current element is 0
// b) sum of elements from 0 to i is 0
// c) sum is already present in hash map
if (arr[i] == 0
|| sum == 0
|| hs.contains(sum))
return true;

// Add sum to hash set
}

// We reach here only when there is
// no subarray with 0 sum
return false;
}

// Driver code
public static void main(String arg[])
{
int arr[] = { -3, 2, 3, 1, 6 };
if (subArrayExists(arr))
System.out.println(
"Found a subarray with 0 sum");
else
System.out.println("No Such Sub Array Exists!");
}
}
```
```// Java program to determine if array arr[]
// can be split into three equal sum sets.

// Time Complexity: O(n), Auxiliary Space: O(1)

import java.io.*;
import java.util.*;

public class GFG {

// Function to determine if array arr[]
// can be split into three equal sum sets.
static int findSplit(int []arr, int n)
{
int i;

// variable to store prefix sum
int preSum = 0;

// variables to store indices which
// have prefix sum divisible by S/3.
int ind1 = -1, ind2 = -1;

// variable to store sum of
// entire array.
int S;

// Find entire sum of the array.
S = arr[0];
for (i = 1; i < n; i++)
S += arr[i];

// Check if array can be split in
// three equal sum sets or not.
if(S % 3 != 0)
return 0;

// Variables to store sum S/3
// and 2*(S/3).
int S1 = S / 3;
int S2 = 2 * S1;

// Loop until second last index
// as S2 should not be at the last
for (i = 0; i < n-1; i++)
{
preSum += arr[i];

// If prefix sum is equal to S/3
// store current index.
if (preSum == S1 && ind1 == -1)
ind1 = i;

// If prefix sum is equal to 2*(S/3)
// store current index.
else if(preSum == S2 && ind1 != -1)
{
ind2 = i;

// Come out of the loop as both the
// required indices are found.
break;
}
}

// If both the indices are found
// then print them.
if (ind1 != -1 && ind2 != -1)
{
System.out.print("(" + ind1 + ", "
+ ind2 + ")");
return 1;
}

return 0;
}

// Driver code
public static void main(String args[])
{
int []arr = { 1, 3, 4, 0, 4 };
int n = arr.length;
if (findSplit(arr, n) == 0)
System.out.print("-1");
}
}

// Output: (1, 2)```
```import java.util.*;
import java.io.*;

class GFG
{
static int maxOcc(int L[], int R[], int n, int maxx)
{
int arr[] = new int[1000000];

for(int i = 0; i < n; i++)
{
arr[L[i]]++;

arr[R[i] + 1]-=1; // arr[R[i] + 1]--;
}

int maxm = arr[0], res = 0;

for(int i = 1; i < maxx; i++) // maxx = 1000000
{
arr[i] += arr[i - 1];

if(maxm < arr[i])
{
maxm = arr[i];

res = i;
}
}

return res;
}
public static void main(String args[])
{
int L[] = {1, 2, 3}, R[] = {3, 5, 7}, n = 3;

System.out.println(maxOcc(L, R, n));
}
}```
```// Efficient Method : Time Complexity : O(n), Auxilliary space : O(1)

import java.util.*;
import java.io.*;

class GFG
{
static boolean checkEquilibrium(int arr[], int n)
{
int sum = 0;

for(int i = 0; i < n; i++)
{
sum += arr[i];
}

int l_sum = 0;

for(int i = 0; i < n; i++)
{
if(l_sum == sum - arr[i])
return true;

l_sum += arr[i];

sum -= arr[i];
}

return false;
}

public static void main(String args[])
{
int arr[] = {3, 4, 8, -9, 20, 6}, n = 6;

System.out.println(checkEquilibrium(arr, n));
}
}

// Naive Method : Time Complexity : O(n^2)

static boolean checkEquilibrium(int arr[], int n)
{
for(int i  = 0; i < n; i++)
{
int l_sum = 0, r_sum = 0;

for(int j = 0; j < i; j++)
l_sum += arr[j];

for(int j = i + 1; j < n; j++)
r_sum += arr[j];

if(l_sum == r_sum)
return true;
}

return false;
}```
```import java.util.*;
import java.io.*;

class GFG
{
// For preprocessing : O(n)
static int[] preSum(int arr[], int n)
{
int prefix_sum[] = new int[n];

prefix_sum[0] = arr[0];

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

return prefix_sum;
}

// For answer queries : O(1)
static int getSum(int prefix_sum[], int l, int r)
{
if(l != 0)
return prefix_sum[r] - prefix_sum[l - 1];
else
return prefix_sum[r];
}

public static void main(String args[])
{
int arr[] = {2, 8, 3, 9, 6, 5, 4}, n = 7;

int prefix_sum[] = preSum(arr, n);

System.out.println(getSum(prefix_sum, 1, 3));

System.out.println(getSum(prefix_sum, 0, 2));

}

}```
```// Time Complexity : O(n), Auxiliary space : O(1)

import java.util.*;
import java.io.*;

class GFG
{

static void printGroups(int arr[], int n)
{
for(int i = 1; i < n; i++)
{
if(arr[i] != arr[i - 1])
{
if(arr[i] != arr[0])
System.out.print("From " + i + " to ");
else
System.out.println(i - 1);
}
}

if(arr[n - 1] != arr[0])
System.out.println(n-1);
}

public static void main(String args[])
{
int arr[] = {0, 0, 1, 1, 0, 0, 1, 1, 0}, n = 9;

printGroups(arr, n);
}
}```
```// Efficient Solution : Time Complexity : O(n)

import java.util.*;
import java.io.*;

class GFG
{
static int findMajority(int arr[], int n)
{
int res = 0, count = 1;

for(int i = 1; i < n; i++)
{
if(arr[res] == arr[i])
count++;
else
count --;

if(count == 0)
{
res = i; count = 1;
}
}

count = 0;

for(int i = 0; i < n; i++)
if(arr[res] == arr[i])
count++;

if(count <= n /2)
res = -1;

return res;
}

public static void main(String args[])
{
int arr[] = {8, 8, 6, 6, 6, 4, 6}, n = 7;

System.out.println(findMajority(arr, n));  // Output : 3
}
}

// Naive Solution : Time Complexity : O(n^2)

static int findMajority(int arr[], int n)
{
for(int i = 0; i < n; i++)
{
int count = 1;

for(int j = i + 1; j < n; j++)
{
if(arr[i] == arr[j])
count++;
}

if(count > n / 2)
return i;
}

return -1;
}```
```// Efficient Method (Based on KADANE's ALGORITHM) : Time Complexity : O(n)

import java.util.*;
import java.io.*;

class GFG
{
static int normalMaxSum(int arr[], int n)
{
int res = arr[0];

int maxEnding = arr[0];

for(int i = 1; i < n; i++)
{
maxEnding = Math.max(maxEnding + arr[i], arr[i]);

res = Math.max(maxEnding, res);
}

return res;
}

static int overallMaxSum(int arr[], int n)
{
// Normal Sum
int max_normal = normalMaxSum(arr, n);

if(max_normal < 0)
return max_normal;

int arr_sum = 0;
// Circular Sum
for(int i = 0; i < n; i++)
{
arr_sum += arr[i];

arr[i] = -arr[i];
}

int max_circular = arr_sum + normalMaxSum(arr, n);

return Math.max(max_circular, max_normal);
}

public static void main(String args[])
{
int arr[] = {8, -4, 3, -5, 4}, n = 5;

System.out.println(overallMaxSum(arr, n));
}
}

// Naive Method : Time Complexity : O(n^2)

static int maxCircularSum(int arr[], int n)
{
int res = arr[0];

for(int i = 0; i < n; i++)
{
int curr_max = arr[i];
int curr_sum = arr[i];

for(int j = 1; j < n; j++)
{
int index = (i + j) % n;

curr_sum += arr[index];

curr_max = Math.max(curr_max, curr_sum);
}

res = Math.max(res, curr_max);
}
return res;
}```
```// Efficient Method : Time Complexity : O(n)

import java.util.*;
import java.io.*;

class GFG
{
static int maxEvenOdd(int arr[], int n)
{
int res = 1;
int curr = 1;

for(int i = 1; i < n; i++)
{
if((arr[i] % 2 == 0 && arr[i - 1] % 2 != 0)
||(arr[i] % 2 != 0 && arr[i - 1] % 2 == 0))
{
curr++;

res = Math.max(res, curr);
}
else
curr = 1;
}

return res;
}

public static void main(String args[])
{
int arr[] = {5, 10, 20, 6, 3, 8}, n = 6;

System.out.println(maxEvenOdd(arr, n));
}
}

// Naive : Time Complexity : O(n^2)

static int maxEvenOdd(int arr[], int n)
{
int res = 1;

for(int i = 0; i < n; i++)
{
int curr = 1;

for(int j = i + 1; j < n; j++)
{
if((arr[j] % 2 == 0 && arr[j - 1] % 2 != 0)
||(arr[j] % 2 != 0 && arr[j - 1] % 2 == 0))
curr++;
else
break;
}

res = Math.max(res, curr);
}

return res;
}```
```// Efficient Method (KADANE's ALGORITHM) : Time Complexity : O(n)

import java.util.*;
import java.io.*;

class GFG
{
static int maxSum(int arr[], int n)
{
int res = arr[0];

int maxEnding = arr[0];

for(int i = 1; i < n; i++)
{
maxEnding = Math.max(maxEnding + arr[i], arr[i]);

res = Math.max(maxEnding, res);
}

return res;
}

public static void main(String args[])
{
int arr[] = {1, -2, 3, -1, 2}, n = 5;

System.out.println(maxSum(arr, n));
}
}

// Naive : Time Complexity : O(n^2)

static int maxSum(int arr[], int n)
{
int res = arr[0];

for(int i = 0; i < n; i++)
{
int curr = 0;

for(int j = i; j < n; j++)
{
curr = curr + arr[j];

res = Math.max(res, curr);
}
}

return res;
}```
```// Efficient Method : Time Complexity : O(n), Auxiliary space : O(1)

import java.util.*;
import java.io.*;

class GFG
{
static int maxConsecutiveOnes(int arr[], int n)
{
int res = 0, curr = 0;

for(int i = 0; i < n; i++)
{
if(arr[i] == 0)
curr = 0;
else
{
curr++;

res = Math.max(res, curr);
}
}

return res;
}

public static void main(String args[])
{
int arr[] = {0, 1, 1, 0, 1, 1, 1}, n = 7;

System.out.println(maxConsecutiveOnes(arr, n)); // Output : 3
}
}

// Naive Method : Time Complexity : O(n^2), Auxiliary space : O(1)

static int maxConsecutiveOnes(int arr[], int n)
{
int res = 0;

for(int i = 0; i < n; i++)
{
int curr = 0;

for(int j = i; j < n; j++)
{
if(arr[j] == 1) curr++;
else break;
}

res = Math.max(res, curr);
}

return res;
}```
```// Efficient Method : Time Complexity : O(n), Auxilliary Space : O(n)

import java.util.*;
import java.io.*;

class GFG
{
static int getWater(int arr[], int n)
{
int res = 0;

int lMax[] = new int[n];
int rMax[] = new int[n];

lMax[0] = arr[0];
for(int i = 1; i < n; i++)
lMax[i] = Math.max(arr[i], lMax[i - 1]);

rMax[n - 1] = arr[n - 1];
for(int i = n - 2; i >= 0; i--)
rMax[i] = Math.max(arr[i], rMax[i + 1]);

for(int i = 1; i < n - 1; i++)
res = res + (Math.min(lMax[i], rMax[i]) - arr[i]);

return res;
}

public static void main(String args[])
{
int arr[] = {5, 0, 6, 2, 3}, n = 5;

System.out.println( getWater(arr, n)); // Output : 6
}
}

// Naive Method : Time Complexity : O(n^2)

static int getWater(int arr[], int n)
{
int res = 0;

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

for(int j = 0; j < i; j++)
lMax = Math.max(lMax, arr[j]);

int rMax = arr[i];

for(int j = i + 1; j < n; j++)
rMax = Math.max(rMax, arr[j]);

res = res + (Math.min(lMax, rMax) - arr[i]);
}

return res; // Output : 6
}```
```import java.util.*;
import java.io.*;

class GFG
{
static int maxProfit(int price[], int n)
{
int profit = 0;

for(int i = 1; i < n; i++)
{
if(price[i] > price[i - 1])
profit += price[i] - price[i -1];
}

return profit;
}

public static void main(String args[])
{
int arr[] = {1, 5, 3, 8, 12}, n = 5;

System.out.println(maxProfit(arr, n));
}
}```
```import java.util.*;
import java.io.*;

class GFG
{
static int maxProfit(int price[], int start, int end)
{
if(end <= start)
return 0;

int profit = 0;

for(int i = start; i < end; i++)
{
for(int j = i + 1; j <= end; j++)
{
if(price[j] > price[i])
{
int curr_profit = price[j] - price[i]
+ maxProfit(price, start, i - 1)
+ maxProfit(price, j + 1, end);

profit = Math.max(profit, curr_profit);
}
}
}

return profit;
}

public static void main(String args[])
{
int arr[] = {1, 5, 3, 8, 12}, n = 5;

System.out.println(maxProfit(arr, 0, n-1));
}
}```
```import java.util.*;
import java.io.*;

class GFG
{
static void printFreq(int arr[], int n)
{
int freq = 1, i = 1;

while(i < n)
{
while(i < n && arr[i] == arr[i - 1])
{
freq++;
i++;
}

System.out.println(arr[i - 1] + " " + freq);

i++;
freq = 1;
}
}

public static void main(String args[])
{
int arr[] = {10, 10, 20, 30, 30, 30}, n = 6;

printFreq(arr, n);
}
}```
```import java.util.*;

public class Main {

public static void main(String[] args) {

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

boolean ans = sorted(arr, 0);

System.out.print(ans);

}

public static boolean sorted(int[] arr,int index) {
if(index == arr.length-1){
return true;
}

return arr[index]<arr[index+1] && sorted(arr, index+1);

}
}```
```let cars = [
{
"color": "purple",
"type": "minivan",
"registration": new Date('2017-01-03'),
"capacity": 7
},
{
"color": "red",
"type": "station wagon",
"registration": new Date('2018-03-03'),
"capacity": 5
},
{
...
},
...
]

const car = {
"color": "red",
"type": "cabrio",
"registration": new Date('2016-05-02'),
"capacity": 2
}
cars.unshift(car);

const car = {
"color": "red",
"type": "cabrio",
"registration": new Date('2016-05-02'),
"capacity": 2
}
cars.push(car);

// add somewhere in the middle
Array.splice(
{index where to start},
{how many items to remove},
);

// place the car after the 4th element in the array
let car = {
"color": "red",
"type": "cabrio",
"registration": new Date('2016-05-02'),
"capacity": 2
}
cars.splice(4, 0, car);
```
```function palindrome(str) {
const alphanumericOnly = str
// 1) Lowercase the input
.toLowerCase()
// 2) Strip out non-alphanumeric characters
.match(/[a-z0-9]/g);

// 3) return string === reversedString
return alphanumericOnly.join('') ===
alphanumericOnly.reverse().join('');
}

palindrome("eye");

```
```var str = "How are you doing today?";

var res = str.split(" ");
```
```const compact = arr => arr.filter(Boolean);

compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34]); ```
```const arrAvg = arr => arr.reduce((a,b) => a + b, 0) / arr.length
// arrAvg([20, 10, 5, 10]) -> 11.25```
```const arrSum = arr => arr.reduce((a,b) => a + b, 0)
// arrSum([20, 10, 5, 10]) -> 45```
```const arrMax = arr => Math.max(...arr);
// arrMax([20, 10, 5, 10]) -> 20```
```const arrMin = arr => Math.min(...arr);
// arrMin([20, 10, 5, 10]) -> 5

```
```import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;

class Pair {
int x, y;

public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}

class FloodFill
{
// Below arrays details all 8 possible movements
private static final int[] row = { -1, -1, -1, 0, 0, 1, 1, 1 };
private static final int[] col = { -1, 0, 1, -1, 1, -1, 0, 1 };

// check if it is possible to go to pixel (x, y) from
// current pixel. The function returns false if the pixel
// has different color or it is not a valid pixel
public static boolean isSafe(char[][] M, int m, int n,
int x, int y, char target)
{
return x >= 0 && x < m && y >= 0 && y < n
&& M[x][y] == target;
}

// Flood fill using BFS
public static void floodfill(char[][] M, int x, int y, char replacement)
{
int m = M.length;
int n = M[0].length;

// create a queue and enqueue starting pixel
Queue<Pair> q = new ArrayDeque<>();

// get target color
char target = M[x][y];

// run till queue is not empty
while (!q.isEmpty())
{
// pop front node from queue and process it
Pair node = q.poll();

// (x, y) represents current pixel
x = node.x;
y = node.y;

// replace current pixel color with that of replacement
M[x][y] = replacement;

// process all 8 adjacent pixels of current pixel and
// enqueue each valid pixel
for (int k = 0; k < row.length; k++)
{
// if adjacent pixel at position (x + row[k], y + col[k]) is
// a valid pixel and have same color as that of current pixel
if (isSafe(M, m, n, x + row[k], y + col[k], target))
{
q.add(new Pair(x + row[k], y + col[k]));
}
}
}
}

public static void main(String[] args)
{
// matrix showing portion of the screen having different colors
char[][] M = {
"YYYGGGGGGG".toCharArray(),
"YYYYYYGXXX".toCharArray(),
"GGGGGGGXXX".toCharArray(),
"WWWWWGGGGX".toCharArray(),
"WRRRRRGXXX".toCharArray(),
"WWWRRGGXXX".toCharArray(),
"WBWRRRRRRX".toCharArray(),
"WBBBBRRXXX".toCharArray(),
"WBBXBBBBXX".toCharArray(),
"WBBXXXXXXX".toCharArray()
};

// start node
int x = 3, y = 9;   // target color = "X"

// replacement color
char replacement = 'C';

// replace target color with replacement color
floodfill(M, x, y, replacement);

// print the colors after replacement
for (int i = 0; i < M.length; i++) {
System.out.println(Arrays.toString(M[i]));
}
}
}```
```# Python3 implementation of the approach

# Function to sort the array such that
# negative values do not get affected
def sortArray(a, n):

# Store all non-negative values
ans=[]
for i in range(n):
if (a[i] >= 0):
ans.append(a[i])

# Sort non-negative values
ans = sorted(ans)

j = 0
for i in range(n):

# If current element is non-negative then
# update it such that all the
# non-negative values are sorted
if (a[i] >= 0):
a[i] = ans[j]
j += 1

# Print the sorted array
for i in range(n):
print(a[i],end = " ")

# Driver code

arr = [2, -6, -3, 8, 4, 1]

n = len(arr)

sortArray(arr, n)

```
```int maxSubArraySum(int a[], int size)
{
int max_so_far = 0, max_ending_here = 0;
for (int i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if (max_ending_here < 0)
max_ending_here = 0;

/* Do not compare for all elements. Compare only
when max_ending_here > 0 */
else if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
return max_so_far;
}
```
star

Wed Oct 18 2023 10:50:28 GMT+0000 (Coordinated Universal Time)

#parallelcourses3 #graphs #bfs #dynamicprogramming #topologicalsort #arrays
star

Mon Aug 28 2023 21:14:16 GMT+0000 (Coordinated Universal Time)

#javascript #pagination #array #of #arrays
star

Wed Apr 05 2023 16:05:45 GMT+0000 (Coordinated Universal Time) https://codetogo.io/how-to-concatenate-arrays-in-javascript/

#javascript #array #arrays #join
star

Mon Feb 28 2022 08:37:07 GMT+0000 (Coordinated Universal Time) https://www.quora.com/What-are-some-cool-Python-tricks

#python #lists #arrays
star

Tue Feb 08 2022 06:38:10 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/mean-and-median-1587115620/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #mean #median
star

Tue Feb 08 2022 06:33:11 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/who-has-the-majority/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #majority
star

Tue Feb 08 2022 06:26:48 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/max-and-second-max/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #max #secondmax
star

Tue Feb 08 2022 06:22:48 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/strongest-neighbour/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #strongestneighbour
star

Tue Feb 08 2022 06:17:15 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #arrays #practice #reversearray
star

Tue Feb 08 2022 06:10:45 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/minimum-absloute-difference-between-adjacent-elements-in-a-circular-array-1587115620/1/?track=DSASP-Arrays&batchId=190

star

Tue Feb 08 2022 05:46:24 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/wave-array-1587115621/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #wavearray
star

Tue Feb 08 2022 05:43:55 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/frequency-of-array-elements-1587115620/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #frequencycount #frequencies #limitedrange
star

Tue Feb 08 2022 05:40:29 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/equilibrium-point-1587115620/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #equilibriumpoint
star

Tue Feb 08 2022 05:35:58 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/leaders-in-an-array-1587115620/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #leaders
star

Tue Feb 08 2022 05:32:27 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/smallest-positive-missing-number-1587115621/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #smallestpositive #missingnumber
star

Tue Feb 08 2022 05:22:26 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/-rearrange-array-alternately-1587115620/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #rearrange #alternately
star

Tue Feb 08 2022 05:08:33 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/rearrange-an-array-with-o1-extra-space3142/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #rearrange
star

Tue Feb 08 2022 05:04:45 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/maximum-index-1587115620/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #maximumindex #maximumdifference
star

Tue Feb 08 2022 04:43:41 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/stock-buy-and-sell-1587115621/1/?track=DSASP-Arrays&batchId=190#

star

Tue Feb 08 2022 04:33:48 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/check-if-array-is-sorted-and-rotated-clockwise-1587115620/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #sorted #rotated
star

Mon Feb 07 2022 16:14:27 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #arrays #prefixsum #subarrays #moreproblems
star

Mon Feb 07 2022 15:51:57 GMT+0000 (Coordinated Universal Time) https://www.geeksforgeeks.org/largest-subarray-with-equal-number-of-0s-and-1s/

#java #gfg #geeksforgeeks #lecture #arrays #prefixsum #subarrays #hashing
star

Mon Feb 07 2022 15:44:47 GMT+0000 (Coordinated Universal Time) https://www.geeksforgeeks.org/find-if-there-is-a-subarray-with-0-sum/

#java #gfg #geeksforgeeks #lecture #arrays #prefixsum #subarrays #splitarray
star

Mon Feb 07 2022 15:33:54 GMT+0000 (Coordinated Universal Time) https://www.geeksforgeeks.org/split-array-three-equal-sum-subarrays/

#java #gfg #geeksforgeeks #lecture #arrays #prefixsum #subarrays #splitarray
star

Mon Feb 07 2022 15:07:44 GMT+0000 (Coordinated Universal Time) https://www.geeksforgeeks.org/maximum-occurred-integer-n-ranges/

#java #gfg #geeksforgeeks #lecture #arrays #prefixsum #maximumoccuredinteger
star

Mon Feb 07 2022 14:28:06 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #lecture #arrays #prefixsum
star

Mon Feb 07 2022 13:26:35 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #lecture #arrays #binaryarray #minimumconsecutiveflips
star

Mon Feb 07 2022 13:22:24 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #lecture #arrays #majorityelement
star

Mon Feb 07 2022 13:18:10 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/max-circular-subarray-sum-1587115620/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #lecture #arrays #subarray #kadane #circularsubarray #subarraysum #maximumsum
star

Mon Feb 07 2022 13:03:29 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/longest-subarray-of-evens-and-odds/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #lecture #arrays #subarray #kadane #longestevenoddsubarray #maximumlength
star

Mon Feb 07 2022 12:51:57 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/kadanes-algorithm-1587115620/1/?track=DSASP-Arrays&batchId=190#

#java #gfg #geeksforgeeks #lecture #arrays #maximumsubarraysum #subarray #kadane
star

Mon Feb 07 2022 12:22:59 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/trapping-rain-water-1587115621/1/?track=DSASP-Arrays&batchId=190#

#java #gfg #geeksforgeeks #lecture #arrays #stock #buysell
star

Mon Feb 07 2022 12:16:14 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #lecture #arrays #stock #buysell
star

Mon Feb 07 2022 12:13:11 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #lecture #arrays #stock #buysell
star

Mon Feb 07 2022 12:08:41 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #lecture #arrays #sortedarray #frequencies
star

Sat Jan 08 2022 18:00:23 GMT+0000 (Coordinated Universal Time)

#recursion #arrays
star

Thu Jun 17 2021 11:00:23 GMT+0000 (Coordinated Universal Time)

#javascript #objects #arrays
star

Tue Aug 04 2020 19:25:25 GMT+0000 (Coordinated Universal Time) https://www.freecodecamp.org/news/freecodecamp-palindrome-checker-walkthrough/

#javascript #strings #arrays
star

Tue Aug 04 2020 18:47:59 GMT+0000 (Coordinated Universal Time) https://www.google.com/search?sxsrf=ALeKk02DFlo7yH-yKNNl-feYqkhlegNMmg:1596506374374

#javascript #arrays
star

Fri Jan 17 2020 20:33:47 GMT+0000 (Coordinated Universal Time)

#javascript #arrays #jsfunctions
star

Sun Jan 05 2020 19:39:47 GMT+0000 (Coordinated Universal Time) https://codeburst.io/javascript-arrays-finding-the-minimum-maximum-sum-average-values-f02f1b0ce332

#javascript #webdev #arrays #math
star

Sun Jan 05 2020 19:37:35 GMT+0000 (Coordinated Universal Time) https://codeburst.io/javascript-arrays-finding-the-minimum-maximum-sum-average-values-f02f1b0ce332

#javascript #webdev #arrays #math
star

Sun Jan 05 2020 19:35:54 GMT+0000 (Coordinated Universal Time) https://codeburst.io/javascript-arrays-finding-the-minimum-maximum-sum-average-values-f02f1b0ce332

#javascript #webdev #javascript #arrays #math
star

Sun Jan 05 2020 19:31:48 GMT+0000 (Coordinated Universal Time) https://codeburst.io/javascript-arrays-finding-the-minimum-maximum-sum-average-values-f02f1b0ce332

#javascript #webdev #arrays #math
star

Thu Dec 26 2019 19:01:13 GMT+0000 (Coordinated Universal Time) https://www.techiedelight.com/flood-fill-algorithm/

#java #logic #algorithms #interesting #arrays
star

Thu Dec 26 2019 15:35:22 GMT+0000 (Coordinated Universal Time) https://www.geeksforgeeks.org/sort-an-array-without-changing-position-of-negative-numbers/

#python #interesting #arrays #sorting #interviewquestions
star

Wed Dec 25 2019 10:57:06 GMT+0000 (Coordinated Universal Time) https://www.geeksforgeeks.org/largest-sum-contiguous-subarray/

#c++ #algorithms #interviewquestions #arrays