Arrays Codes
Arrays Codes
Brace yourself as we delve into the captivating world of Arrays. Prepare to unlock the power of organized
data, where each element holds a unique story waiting to be explored.
What is an ArrayList??
Java ArrayList is a part of the Java collection framework and it is a class of java.util package.
It provides us with dynamic arrays in Java. It has various advantages over normal fixed-size
arrays.
What is ArrayList
Advantages of ArrayList over normal Arrays
Constructors in ArrayList for creating an ArrayList
Codes:
Codes C#:
DescriptionComments
Brace yourself as we delve into the captivating world of Arrays. Prepare to unlock the power
of organized data, where each element holds a unique story waiting to be explored.
In this video, you will learn: Operations of Arrays (Search Operation and Insert Operation) !!!
Codes:
Insert
C: https://ide.geeksforgeeks.org/jQp3TGd2FD
CPP: https://ide.geeksforgeeks.org/8WAeXcymsH
Java: https://ide.geeksforgeeks.org/7qP8h4Uvfm
C#: https://ide.geeksforgeeks.org/721f9d07-9cb7-4c17-adfe-ecb252ae65c1
Search(Unsorted Array)
C: https://ide.geeksforgeeks.org/250nusxi3G
CPP: https://ide.geeksforgeeks.org/G44GBZ1H7k
Java: https://ide.geeksforgeeks.org/G5RUNYH2kY
C#: https://ide.geeksforgeeks.org/fe37cb74-d197-4d2d-8dd7-920cb1abb99a
DescriptionComments
Brace yourself as we delve into the captivating world of Arrays. Prepare to unlock the power
of organized data, where each element holds a unique story waiting to be explored.
In this video, you will learn: Operations of Arrays (Delete Operation) !!!
Codes:
Deletion
C: https://ide.geeksforgeeks.org/MGz943EITw
CPP: https://ide.geeksforgeeks.org/uzR0nDeFt9
Java: https://ide.geeksforgeeks.org/r4jtWDSX3b
C#: https://ide.geeksforgeeks.org/a713337c-1c79-4130-a657-a209bf1284a3
894
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is to, Find the
Largest Number in an Array !!!
Our Task: Given an array arr[ ] of size n, the task is to find the largest element in the given
array.
Example:
Output: 20
Output: 100
We have 2 approaches to solve the problem: Naive Approach and Efficient Approach
1) Naive Method:
In this approach, we traverse the array for each element present in the array. We compare all
elements with each other to find the largest element in the array.
Time Complexity: O(n2)
2) Efficient Method:
One of the most simplest and basic approach to solve this problem is to simply traverse the
whole list and find the maximum among them.
Create a local variable max to store the maximum among the list
Initialize max with the first element initially, to start the comparison.
Then traverse the given array from second element till end, and for each element:
Compare the current element with max
If the current element is greater than max, then replace the value of max with the
current element.
At the end, return and print the value of the largest element of array stored in max.
Codes:
C# Code: https://ide.geeksforgeeks.org/0b694eea-f8fa-4884-a125-8f64b0b82a16
1049
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is to, Find the
Second Largest Number in an Array !!!
Our Task: Given an array arr[ ] of size n, the task is to find the Second largest element in the
given array.
Example:
Input: arr[] = {12, 35, 1, 10, 34, 1}
largest element is 34
largest element is 5
1) Naive Approach:
In this approach, we find the 2nd largest element in the array, by ignoring the largest element.
It requires two traversals of the array
2) Efficient Approach:
In this approach, we find the 2nd largest element in the array, by a single traversal through
the array. We maintain the record of the Largest and Second Largest element, which
traversing through the array.
Codes:
Naive approach:
C: https://ide.geeksforgeeks.org/GSL56poBwL
C#: https://ide.geeksforgeeks.org/46350654-70c7-433b-a9e4-1a478646c26c
C++: https://ide.geeksforgeeks.org/online-cpp-compiler/2f19c35d-315d-43ab-be57-
c66cec235e58
Java: https://ide.geeksforgeeks.org/online-java-compiler/70b83565-00ce-4a71-bdb5-
df21026a1c99
Efficient approach:
C: https://ide.geeksforgeeks.org/2fsu6XqWn2
C#: https://ide.geeksforgeeks.org/357d06cc-6f99-4492-ace3-bddb34ede84f
C++14: https://ide.geeksforgeeks.org/online-cpp14-compiler/f3b5f728-c3ba-41f9-aa4c-
33ac62bab411
Java: https://ide.geeksforgeeks.org/online-java-compiler/2c9e7e5c-db16-484e-a853-
eaf27f1043e1
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is to, Check if
the Array is Sorted !!!
Our Task: We are given an array containing n elements. Our task is to check whether the array
is sorted in ascending (non-decreasing/increasing) order or not.
Examples:
Output: Yes // Since all elements are in ascending order (elements may be repeated)
We have 2 approaches to solve the problem: Naive Approach & Efficient Approach
1) Naive Approach
2) Efficient Approach
Codes:
Efficient Method
CPP: https://ide.geeksforgeeks.org/WexgfRhvUP
Java: https://ide.geeksforgeeks.org/pQDTIm17BW
C#: https://ide.geeksforgeeks.org/0c7bff0c-bce2-4244-a5e9-66a45bbbfc3c
Naive Method
CPP: https://ide.geeksforgeeks.org/38F8vszDpa
Java: https://ide.geeksforgeeks.org/ovxubbMppg
C#: https://ide.geeksforgeeks.org/d076619d-2b21-4fb1-994b-2d3a6626ff94
Reverse an Array
641
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is to, Reverse an
Array !!!
Our Task: Given an array (or string), the task is to reverse the array.
Examples :
Codes:
C: https://ide.geeksforgeeks.org/TP4E1UXCoZ
CPP: https://ide.geeksforgeeks.org/Q18XrGjaH0
Java: https://ide.geeksforgeeks.org/qkfXgy68Yc
c#: https://ide.geeksforgeeks.org/7c41ae9d-df2c-4242-9d33-d3e7272969c2
913
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is to, Remove
Duplicates from a Sorted Array !!!
Our Task: Given a sorted array, the task is to remove the duplicate elements from the array.
Examples:
new size = 1
We have 2 approaches to solve the problem: Naive approach and Efficient approach
1) Naive Approach
In this approach, we traverse the array and find the distinct elements, which are then copied
to a new array. After all the distinct elements are copied to the new array, the elements from
the new array are transferred back to the original array.
2) Efficient Approach:
In this approach we compare the current element with the previous distinct element and
check if the current element is a distinct element or not.
Codes:
Efficient Method
CPP: https://ide.geeksforgeeks.org/A1Q1TOct6d
Java: https://ide.geeksforgeeks.org/izwCGdV1Jq
Javascript: https://ide.geeksforgeeks.org/tryit.php/242f8428-b542-4a1b-b8a1-
c2938dc7ed22
C#: https://ide.geeksforgeeks.org/daf9f074-761f-43d8-b344-4f4afeb20577
Naive Method
CPP: https://ide.geeksforgeeks.org/aEelSCQWbn
Java: https://ide.geeksforgeeks.org/KZtvikbUxh
Javascript: https://ide.geeksforgeeks.org/tryit.php/dc5c5a74-cb9f-47eb-a7a2-
b546a92ccf0f
C#: https://ide.geeksforgeeks.org/8dfe8ddb-30fc-4dcf-b17f-5d2a20879346
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is to, Move
Zeros to End !!!
Our Task: Given a sorted array, the task is to remove the duplicate elements from the array.
Examples:
Output: 1 2 3 6 0 0 0
Output: 1 9 8 4 2 7 6 9 0 0 0 0 0
We have 2 approaches to solve the problem: Naive Solution and Efficient Solution
1) Naive Approach:
In this approach, we traverse the array and, as soon as we find a 0 element, we traverse to the
right of the 0, and swap it with the 1st non-0 element found.
2) Efficient Approach
In this approach, we maintain a count of the non-0 elements traversed. When a 0 element is
found, we place the value of the non-0 element at the element place of arr[count].
Codes:
C:- https://ide.geeksforgeeks.org/7975c5e5-7f23-4e75-836b-743719385fe2
CPP:- https://ide.geeksforgeeks.org/065bc525-b5cb-495e-8189-305367818588
JAVA:- https://ide.geeksforgeeks.org/1be10b6f-8151-4210-a940-10e91c00f1de
JAVASCRIPT: https://ide.geeksforgeeks.org/tryit.php/7a215ead-abeb-4c25-88ea-
b4790769a8d6
C#: https://ide.geeksforgeeks.org/9170b16a-a21a-4cf6-84b3-df6139db10db
582
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is to, Left
Rotate an Array by One !!!
Our Task: Given an array, the task is to Left Rotate an Array by One.
Example:
Codes:
C: https://ide.geeksforgeeks.org/cNKe1oIksJ
CPP: https://ide.geeksforgeeks.org/e4yl4vr66h
Java: https://ide.geeksforgeeks.org/UMRjB1yGTP
Javascript: https://ide.geeksforgeeks.org/tryit.php/333d9b19-1d04-4449-b123-
3911bb584c9c
C#: https://ide.geeksforgeeks.org/47635e95-4ab5-4f62-ba1c-bfb42e5b45d0
891
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is to, Left
Rotate an Array by D Places !!!
Our Task: Given an array, the task is to Left Rotate an Array by D places.
Examples:
Output: 3 4 5 6 7 1 2
We have 3 approaches to solve the problem: Naive Approach, Better Approach, and Reversal
Approach(Efficient Approach)
1) Naive Approach:
2) Better Approach:
In this approach, we store the first D elements of the array in a new array, and shift the
remaining elements by D positions to the left. Finally we copy back the D elements from the
new array to the end of the original array.
Codes:
Reversal Method
C: https://ide.geeksforgeeks.org/xg5ff2Tvww
CPP: https://ide.geeksforgeeks.org/S3ZxpVUzFr
Java: https://ide.geeksforgeeks.org/adWW3ZzXjn
Javascript: https://ide.geeksforgeeks.org/tryit.php/50df2adb-c9dc-46d7-b9c1-
a06ae32e73af
C#: https://ide.geeksforgeeks.org/f0746c74-257d-44a0-b1d3-1d1bf0f4265a
Efficient Code
C: https://ide.geeksforgeeks.org/0M2T5V4GVy
CPP: https://ide.geeksforgeeks.org/bLMbGJtChv
Java: https://ide.geeksforgeeks.org/ZQiGeV2Vfk
Javascript: https://ide.geeksforgeeks.org/tryit.php/0bcdef2a-143f-4382-af8b-
c30963a76be0
C#: https://ide.geeksforgeeks.org/35337d6f-176c-4483-8c75-1fb4c0fb8b18
Naive Method
C: https://ide.geeksforgeeks.org/xVbBYEn41o
CPP: https://ide.geeksforgeeks.org/KVinCgnCa7
Java: https://ide.geeksforgeeks.org/wbT7F7RytD
Javascript: https://ide.geeksforgeeks.org/tryit.php/8aea2354-dbac-400b-bf2d-
9e88a5c9cc6d
C#: https://ide.geeksforgeeks.org/ad13b4f8-48ed-474b-b8fd-1b58cb77697a
717
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is to, Find the
Leaders in an Array !!!
Our Task: Given an array, the task is to write a program to print all the LEADERS in the array.
What is a Leader?
An element is a leader if it is greater than all the elements to its right side. And the rightmost
element is always a leader.
For example:
Output: 17, 5, 2
Output: 5, 2
We have 2 approaches to solve the problem: Naive Approach and Efficient Approach
1) Naive Approach:
We use two loops in this approach. The outer loop runs from 0 to size – 1 and one by one
picks all elements from left to right. The inner loop compares the picked element to all the
elements on its right side. If the picked element is greater than all the elements to its right
side, then the picked element is the leader.
2) Efficient Approach:
The idea is to scan all the elements from right to left in an array and keep track of the
maximum till now. When the maximum changes its value, print it.
Codes:
Efficient Method
CPP: https://ide.geeksforgeeks.org/cY8KYEJmka
Java: https://ide.geeksforgeeks.org/kZNeol7GKJ
C#: https://ide.geeksforgeeks.org/71da38df-f534-4383-a4f3-9f8a0a85ff9d
Naive Method
CPP: https://ide.geeksforgeeks.org/wkM5LEqJfW
Java: https://ide.geeksforgeeks.org/Pj0jk4V0Vd
C#: https://ide.geeksforgeeks.org/e452061f-6e52-41f0-ba58-8b44b364613b
721
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is to, Maximum
Difference Problem with Order !!!
Our Task: Given an array arr[] of integers, find out the maximum difference between any two
elements such that the larger element appears after the smaller number.
Examples :
Input : arr = {2, 3, 10, 6, 4, 8, 1}
Output : 8
Output : 2
We have 2 approaches to solve the problem: Naive Approach and Efficient Approach
1) Naive Approach:
We use two loops. In the outer loop, pick elements one by one and in the inner loop calculate
the difference of the picked element with every other element in the array and compare the
difference with the maximum difference calculated so far. Below is the implementation of the
above approach :
2) Efficient Approach:
In this method, instead of taking difference of the picked element with every other element,
we take the difference with the minimum element found so far. So we need to keep track of 2
things:
Codes:
Efficient Method
CPP: https://ide.geeksforgeeks.org/eKkmrh9wSh
Java: https://ide.geeksforgeeks.org/pEGlK0DRaR
Javascript: https://ide.geeksforgeeks.org/tryit.php/9b1fc6a4-27b2-42b1-b138-
c040af905327
C#:https://ide.geeksforgeeks.org/2ae53b67-f351-4126-a1de-b85c657ddc90
C: https://ide.geeksforgeeks.org/online-c-compiler/1a48f952
Naive Method
CPP: https://ide.geeksforgeeks.org/bwEXxyI6VQ
Java: https://ide.geeksforgeeks.org/eHUBgtf6EI
Javascript: https://ide.geeksforgeeks.org/tryit.php/fdbd1ae5-6db3-4bf7-a61b-
139570e89e33
C#: https://ide.geeksforgeeks.org/3860eda4-9d91-4e7d-8842-b998ab2e59d8
C: https://ide.geeksforgeeks.org/online-c-compiler/d2d3820
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is to, Find the
Frequencies in a Sorted Array !!!
Our Task: Given a sorted array, arr[] consisting of N integers, the task is to find the
frequencies of each array element.
Examples:
Frequency of 2 is: 1
Frequency of 3 is: 2
Frequency of 5 is: 2
Frequency of 8 is: 3
Frequency of 9 is: 2
Frequency of 10 is: 1
Frequency of 6 is: 2
Frequency of 7 is: 3
Frequency of 11 is: 1
The idea is to maintain a variable to keep track of the frequency of elements while traversing
the array. Follow the steps below to solve the problem:
Codes:
CPP: https://ide.geeksforgeeks.org/996fe899-43fb-431b-b5e2-780661bedb1a
Java: https://ide.geeksforgeeks.org/585972b4-8611-428f-aae9-f8df1a78bd92
C#: https://ide.geeksforgeeks.org/34d5f355-dd71-42ec-ba2d-4788d538cc9f
Javascript: https://ide.geeksforgeeks.org/tryit.php/4ac2e625-efb9-420a-acf3-
9f340554fdff
535
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is, Stock Buy
and Sell Problem !!!
Our Task: The cost of a stock on each day is given in an array. Find the maximum profit that
you can make by buying and selling on those days. If the given array of prices is sorted in
decreasing order, then profit cannot be earned at all.
Examples:
Input: arr[] = {100, 180, 260, 310, 40, 535, 695}
Output: 865
Explanation: Buy the stock on day 0 and sell it on day 3 => 310 – 100 = 210
Buy the stock on day 4 and sell it on day 6 => 695 – 40 = 655
Output: 2
Maximum Profit = 2
We have 2 approaches to solve the problem: Naive Approach and Efficient Approach
In the next part of the video, the Efficient Approach will be discussed.
1) Naive Approach
A simple approach is to try buying the stocks and selling them every single day when
profitable and keep updating the maximum profit so far.
Time Complexity: O(n2), Trying to buy every stock and exploring all possibilities.
Auxiliary Space: O(1)
Codes:
CPP: https://ide.geeksforgeeks.org/F0zCflXr4A
Java: https://ide.geeksforgeeks.org/EPIIxZg2gG
Javascript: https://ide.geeksforgeeks.org/tryit.php/68ceeba4-5f05-4f8a-aa38-
f69911b7a941
C#: https://ide.geeksforgeeks.org/ea2b3d5d-1328-49c0-bda3-5f65d3953aff
Stock Buy and Sell problem (Part 2)
551
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is, Stock Buy
and Sell Problem !!!
Our Task: The cost of a stock on each day is given in an array. Find the maximum profit that
you can make by buying and selling on those days. If the given array of prices is sorted in
decreasing order, then profit cannot be earned at all.
Examples:
Output: 865
Explanation: Buy the stock on day 0 and sell it on day 3 => 310 – 100 = 210
Buy the stock on day 4 and sell it on day 6 => 695 – 40 = 655
Output: 2
Maximum Profit = 2
We have 2 approaches to solve the problem: Naive Approach and Efficient Approach
The Naive solution to the problem has been discussed in the previous video
In this approach, we just need to find the next greater element and subtract it from the
current element so that the difference keeps increasing until we reach a minimum. If the
sequence is a decreasing sequence, so the maximum profit possible is 0.
maxProfit = 0
if price[i] > price[i – 1]
maxProfit = maxProfit + price[i] – price[i – 1]
Codes:
CPP: https://ide.geeksforgeeks.org/XDa3wfa9Sf
Java: https://ide.geeksforgeeks.org/2DwByHrLiX
Javascript: https://ide.geeksforgeeks.org/tryit.php/7badfe7f-c288-4b67-aa7e-
7e5ce6e50cdf
C#: https://ide.geeksforgeeks.org/07165de6-e7a1-469e-8400-c4666b3543e4
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is, Trapping
Rain Water !!!
Our Task: Given an array of N non-negative integers arr[ ] representing an elevation map
where the width of each bar is 1, compute how much water it is able to trap after rain.
Examples:
1)Naive Approach
Complexity Analysis:
Time Complexity: O(n2). There are two nested loops traversing the array.
Space Complexity: O(1). No extra space is required.
2) Efficient Approach
Complexity Analysis:
Time Complexity: O(N). Only one traversal of the array is needed, So time Complexity is
O(N).
Space Complexity: O(N). Two extra arrays are needed, each of size N.
Codes:
Efficient Method
CPP: https://ide.geeksforgeeks.org/uf1ZkWjsqn
Java: https://ide.geeksforgeeks.org/rKR6wcZcdF
Javascript: https://ide.geeksforgeeks.org/tryit.php/7badfe7f-c288-4b67-aa7e-
7e5ce6e50cdf
C#: https://ide.geeksforgeeks.org/tryit.php/107d1522-8ff6-47e9-aba2-5e8202d5fdd5
Naive Method
CPP: https://ide.geeksforgeeks.org/owf5GaW3jq
Java: https://ide.geeksforgeeks.org/Csr6VM2nU4
Javascript: https://ide.geeksforgeeks.org/tryit.php/5d4525a8-b67a-4a75-b64d-
cd53b14ec68e
C#: https://ide.geeksforgeeks.org/tryit.php/5d4525a8-b67a-4a75-b64d-cd53b14ec68e
Maximum consecutive 1s
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is, To Find
Maximum Consecutive 1s !!!
Our Task: Given a binary array, find the count of the maximum number of consecutive 1's
present in the array.
Examples :
Output: 4
Output: 1
We have 2 approaches to solve the problem: Naive Approach and Efficient Approach
1) Naive Approach:
In this approach, we use two looks and traverse the array from start to end. At each element,
we check if it is 1 or 0. If the element is 1, we increment the count variable and count the
number of 1s along with it if any. After each completion of the inner loop, we find the
maximum between the current count and the previous count.
2) Efficient Approach:
An efficient solution is to traverse the array from left to right. If we see a 1, we increment the
count and compare it with the maximum so far. If we see a 0, we reset the count to 0.
Codes:
Efficient Method
CPP: https://ide.geeksforgeeks.org/DomO12di9m
Java: https://ide.geeksforgeeks.org/CB2KT9XypS
Javascript: https://ide.geeksforgeeks.org/tryit.php/15bc4594-c0a9-4608-b143-
dcc0c6fee524
C#: https://ide.geeksforgeeks.org/d191ea0b-0227-499d-a7b9-fc9ccfb2d073
Naive Method
CPP: https://ide.geeksforgeeks.org/CSkbeeIHOE
Java: https://ide.geeksforgeeks.org/QmflrSLDGP
Javascript: https://ide.geeksforgeeks.org/tryit.php/cc2923aa-4255-41cd-ad61-
65cf5af01fdd
C#: https://ide.geeksforgeeks.org/986540e7-9ca3-47c7-9d19-f94e2a3ff849
766
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is, Maximum
Subarray Sum !!!
Our Task: Given an array arr[], the task is to find the elements of a contiguous subarray of
numbers that has the largest sum.
Examples:
Explanation:
In the above input, the maximum contiguous subarray sum is 7 and the elements of the
subarray are [4, -1, -2, 1, 5]
Explanation:
In the above input, the maximum contiguous subarray sum is 7 and the elements
We have 2 approaches to solve the problem: Naive Approach and Efficient Approach
1)Naive Approach
The naive approach is to generate all the possible subarrays and print that subarray that has
the maximum sum.
Time complexity: O(n2)
Auxiliary Space: O(1)
2) Efficient Approach:
The idea is to maintain a variable max_ending that stores the maximum sum contiguous
subarray ending at current index and a variable res stores the maximum sum of contiguous
subarray found so far. Everytime there is a positive-sum value in max_ending compare it with
res and update res if it is greater than res.
Codes:
JAVASCRIPT: https://ide.geeksforgeeks.org/tryit.php/41403112-bb64-408b-8e5b-
4cb47d6c044e
c#: https://ide.geeksforgeeks.org/090e8134-5134-4b64-b720-56b3fa98267d
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is, Longest Even
Odd Subarray !!!
Our Task: Given an array a[ ] of N integers, the task is to find the length of the longest
Alternating Even Odd subarray present in the array.
Examples:
We have 2 approaches to solve the problem: Naive Approach and Efficient Approach
1)Naive Approach
The idea is to consider every subarray and find the length of even and odd subarrays.
Time Complexity: O(n2), Iterating over every subarray therefore N2 are possible
Auxiliary Space: O(1)
2)Efficient Approach
By simply storing the nature of the previous element we encounter( odd or even) and
comparing it with the next element.
Time Complexity: O(n), Since we need to iterate over the whole array once
Auxiliary Space: O(1)
Codes:
Efficient Method
CPP: https://ide.geeksforgeeks.org/Uz3vWPUUNM
Java: https://ide.geeksforgeeks.org/Sp7XlX1yZ8
Javascript: https://ide.geeksforgeeks.org/tryit.php/cfca63ad-dc2d-4f7e-bd6b-
e6dfba24a775
C#: https://ide.geeksforgeeks.org/c93378f5-021d-488a-bcca-19db2da80a7e
Naive Method
CPP: https://ide.geeksforgeeks.org/BZOgWE7H8z
Java: https://ide.geeksforgeeks.org/RZs6tLQKxz
Javascript: https://ide.geeksforgeeks.org/tryit.php/a14b9939-b040-4953-a179-
882b526f16a0
C#: https://ide.geeksforgeeks.org/13345d90-75e8-4729-8629-46aa356512d8
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is, Maximum
Circular Sum Subarray !!!
Our Task: Given a circular array of size n, find the maximum subarray sum of the non-empty
subarray.
An array is called circular if we consider the first element as the next of the last element.
The subarrays of this circular array can be: {1} {2} {3} {1, 2} {2, 3} {3, 1} {1, 2, 3} {2, 3, 1} {3, 1, 2}
Examples:
We have 2 approaches to solve the problem: Naive Approach and Efficient Approach
1)Naive Approach:
The approach is to use two loops and iterate thought-out the array in a circular manner. After
each iteration, take the maximum of the current maximum and previous maximum
2) Efficient Approach:
The idea is to modify Kadane's algorithm to find a minimum contiguous subarray sum and the
maximum contiguous subarray sum, then check for the maximum value between the
max_value and the value left after subtracting min_value from the total sum.
Codes:
Efficient Method
CPP: https://ide.geeksforgeeks.org/5OjvBfI5Ql
Java: https://ide.geeksforgeeks.org/h8wjdmXCTd
Javascript: https://ide.geeksforgeeks.org/tryit.php/905d2be1-5251-451b-80bc-
a0fe8642a182
C#: https://ide.geeksforgeeks.org/5ea9b685-b1a6-4744-8767-9c9800e75f6d
Naive Method
CPP: https://ide.geeksforgeeks.org/gLVLZRROoJ
Java: https://ide.geeksforgeeks.org/K5MU1mmyUs
Javascript: https://ide.geeksforgeeks.org/tryit.php/72fc7355-1545-43af-84ba-
ce776565c52e
C#: https://ide.geeksforgeeks.org/5ad97f2a-f3a1-4313-bbad-cdb67251bef0
Majority Element
632
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is, Majority
Element !!!
Our Task: Find the majority element in the array. A majority element in an array A[] of size n is
an element that appears more than n/2 times (and hence there is at most one such element).
A majority element in an array A[] of size n is an element that appears more than n/2 times
(and hence there is at most one such element).
Examples :
Input : {3, 3, 4, 2, 4, 4, 2, 4, 4}
Output : 4
Explanation: The frequency of 4 is 5 which is greater than the half of the size of the array
size.
Input : {3, 3, 4, 2, 4, 4, 2, 4}
Output : No Majority Element
Explanation: There is no element whose frequency is greater than the half of the size of the
array size.
We have 2 approaches to solve the problem: Naive Approach and Efficient Approach
1)Naive Approach:
The basic solution is to have two loops and keep track of the maximum count for all different
elements. If the maximum count becomes greater than n/2 then break the loops and return
the element having the maximum count. If the maximum count doesn’t become more than
n/2 then the majority element doesn’t exist.
Time Complexity: O(n2), A nested loop is needed where both the loops traverse the array
from start to end.
Auxiliary Space: O(1), No extra space is required.
The first step gives the element that may be the majority element in the array. If there is a
majority element in an array, then this step will definitely return majority element,
otherwise, it will return candidate for majority element.
Check if the element obtained from the above step is the majority element. This step is
necessary as there might be no majority element.
Time Complexity: O(n), As two traversal of the array, is needed, so the time complexity is
linear.
Auxiliary Space: O(1), As no extra space is required.
Codes:
Efficient Solution
CPP: https://ide.geeksforgeeks.org/AAU0PbQvy4
Java: https://ide.geeksforgeeks.org/GiGZlUePfF
JAVASCRIPT: https://ide.geeksforgeeks.org/tryit.php/c5f0ba1f-538a-443b-a183-
0cabb32368f6
C#: https://ide.geeksforgeeks.org/7aa9a27c-68ee-4da0-93cc-b354141279ab
Naive Solution
CPP: https://ide.geeksforgeeks.org/C6aveb7Lcc
Java: https://ide.geeksforgeeks.org/uD30hi4IFJ
Javascript:https://ide.geeksforgeeks.org/tryit.php/a5d528ce-0158-4546-9c26-
157d34f86024
C#: https://ide.geeksforgeeks.org/bc799745-f404-4634-b6eb-388c614bd300
Minimum Consecutive Flips
575
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is, Minimum
Consecutive Flips!!!
Our Task: Given a binary array, we need to convert this array into an array that either contains
all 1s or all 0s. We need to do it using the minimum number of group flips.
Examples :
We have 2 approaches to solve the problem: Naive Approach and Efficient Approach
1) Naive Approach:
A Naive Solution is to
2) Efficient Approach:
There are only two types of groups (groups of 0s and groups of 1s)
Either the counts of both groups are same or the difference between counts is at most 1.
For example, in {1, 1, 0, 1, 0, 0} there are two groups of 0s and two groups of 1s. In
example, {1, 1, 0, 0, 0, 1, 0, 0, 1, 1}, count of groups of 1 is one more than the counts of 0s.
Based on the above facts, we can conclude that if we always flip the second group and other
groups that of the same type as the second group, we always get the correct answer.
Codes:
CPP: https://ide.geeksforgeeks.org/cPQnrtDbjm
Java: https://ide.geeksforgeeks.org/FKwu2LcxRe
Javascript: https://ide.geeksforgeeks.org/tryit.php/eaf83866-756b-4bc1-a454-
349710007178
C#: https://ide.geeksforgeeks.org/7ff10b32-7768-4dd1-b060-4e4381036a5a
Python: https://ide.geeksforgeeks.org/eb10a382
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is, Sliding
Window Technique!!!
Our Task: Given an array of integers of size 'n'. Our aim is to calculate the maximum sum of 'k'
consecutive elements in the array.
Examples :
k=2
Output : 700
k=4
Output : 39
of size 4.
k=3
Output : Invalid
array is 2.
We have 2 approaches to solve the problem: Naive Approach and Efficient Approach
1) Naive Approach:
The Naive Approach to solve this problem is to calculate sum for each of the blocks of K
consecutive elements and compare which block has the maximum sum possible.
The above problem can be solved in Linear Time Complexity by using Window Sliding
Technique by avoiding the overhead of calculating sum repeatedly for each block of k
elements.
Time Complexity is O(n).
Codes:
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is, Subarray
with given Sum !!!
Our Task: Given an array arr[ ] of non-negative integers and an integer sum, find a subarray
that adds to a given sum.
Examples:
1) Naive Approach:
The idea is to consider all subarrays one by one and check the sum of every subarray. The
following program implements the given idea.
Run two loops: the outer loop picks a starting point i and the inner loop tries all subarrays
starting from i.
Time Complexity: O(n2), Trying all subarrays from every index, used nested loop for the same
Auxiliary Space: O(1).
The idea is simple as we know that all the elements in subarray are positive so, If a subarray
has sum greater than the given sum then there is no possibility that adding elements to the
current subarray will be equal to the given sum. So the Idea is to use a similar approach to a
sliding window.
Codes:
Prefix Sum
0
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is, Prefix Sum
Technique !!!
Our Task: Given an array arr[] of size n, its prefix sum array is another array prefixSum[ ] of the
same size, such that the value of prefixSum[i] is arr[0] + arr[1] + arr[2] … arr[i].
We need to create the prefix Array so that getsum() queries can be resolved in O(1) time.
Examples :
To fill the prefix sum array, we run through index 1 to last and keep on adding the present
element with the previous value in the prefix sum array.
Codes:
Naive Solution
Efficient Solution
Equilibrium Point
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is, Finding the
Equilibrium Point !!!
Our Task: Given a sequence arr[ ] of size n, Write a function int equilibrium(int[] arr, int n) that
returns an equilibrium index (if any) or -1 if no equilibrium index exists.
The equilibrium index of an array is an index such that the sum of elements at lower indexes
is equal to the sum of elements at higher indexes.
Examples:
We have 2 approaches to solve the problem: Naive Approach and Efficient Approach
1) Naive Approach:
To solve the problem follow the below idea: Use two loops. The Outer loop iterates through
all the element and inner loop finds out whether the current index picked by the outer loop is
equilibrium index or not.
2) Efficient Approach
The idea is to get the total sum of the array first. Then Iterate through the array and keep
updating the left sum which is initialized as zero. In the loop, we can get the right sum by
subtracting the elements one by one.
Codes:
C++: https://ide.geeksforgeeks.org/online-cpp14-compiler/77d1d798-c381-4723-9c41-
8c81ae178937
Java: https://ide.geeksforgeeks.org/online-cpp14-compiler/cfc9b730-6b4d-434c-8d67-
813cc3feada9
Report
DescriptionComments
In a realm where numbers hold secrets, a captivating challenge awaits, which is, Finding
Maximum Appearing Element !!!
Our Task: Given two arrays L[ ] and R[ ] of size N where L[i] and R[i] (0 ? L[i], R[i] < 106)
denotes a range of numbers, the task is to find the maximum occurred integer in all the
ranges. If more than one such integer exists, print the smallest one.
Examples:
We have 2 approaches to solve the problem: Naive Approach and Efficient Approach
1) Naive Approach:
Time Complexity: O(N*M). Here N is the number of ranges and M is the maximum number of
elements in any of the ranges.
Auxiliary Space: O(M). For Hash table.
2) Efficient Approach:
The idea is to use the Difference array technique. Create a vector initialized with value zero.
Iterate through every range and mark the presence of the beginning of every range by
incrementing the start of the range with one i.e. arr[L[i]]++ and mark the end of the range by
decrementing at index one greater than the end of range by one i.e. arr[R[i]+1]–.
Now when computing the prefix sum, Since the beginning is marked with one, all the values
after beginning will be incremented by one. Now as increment is only targeted only till the
end of the range, the decrement on index R[i]+1 prevents that for every range i.
Javascript: https://ide.geeksforgeeks.org/tryit.php/8942326b-b4d0-4411-b8be-750f53ca3a80
C#: https://ide.geeksforgeeks.org/75c8c2ad-4aae-450a-8054-d01b47bf7d56