Skip to content

Commit aab3da5

Browse files
committed
Separated code from the Documentation repo
1 parent 1a6afaa commit aab3da5

File tree

406 files changed

+9531
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

406 files changed

+9531
-0
lines changed

Diff for: .DS_Store

8 KB
Binary file not shown.

Diff for: geeksForGeeks/.DS_Store

6 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package geeksForGeeks.arrays;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
import java.util.PriorityQueue;
6+
7+
public class MaxSumNonOverlappingInternalScheduling {
8+
9+
static int solve(int[][] interval){
10+
Arrays.sort(interval , Comparator.comparingInt(a -> a[0]));
11+
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
12+
13+
int max = 0;
14+
int result = 0;
15+
16+
for(int[] e : interval){
17+
while (!pq.isEmpty()){
18+
if(pq.peek()[0] >= e[0]){
19+
20+
System.out.println(Arrays.toString(e));
21+
break;
22+
}
23+
24+
25+
int[] peekElement = pq.remove();
26+
max = Math.max(max, peekElement[1]);
27+
}
28+
29+
result = Math.max(result, max+e[2]);
30+
pq.add(new int[]{e[1],e[2]});
31+
}
32+
return result;
33+
}
34+
public static void main(String[] args){
35+
int[][] interval
36+
= { { 1, 3, 2 },
37+
{ 4, 5, 2 },
38+
{ 1, 5, 5 } };
39+
int maxValue
40+
= MaxSumNonOverlappingInternalScheduling.solve(interval);
41+
System.out.println(maxValue);
42+
}
43+
}

Diff for: geeksForGeeks/arrays/RulingPair.java

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package geeksForGeeks.arrays;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Geek Land has a population of N people and each person's ability to rule the town is
7+
* measured by a numeric value arr[i]. The two people that can together
8+
* rule Geek Land must be compatible with each other i.e.,
9+
* the sum of digits of their ability arr[i] must be equal.
10+
* Their combined ability should be maximum amongst all the possible pairs
11+
* of people. Find the combined ability of the Ruling Pair.
12+
*
13+
* Example 1:
14+
*
15+
* Input:
16+
* N = 5
17+
* arr[] = {55, 23, 32, 46, 88}
18+
*
19+
* Output:
20+
* 101
21+
*
22+
* Explanation:
23+
* All possible pairs that are
24+
* compatible with each other are- (23, 32)
25+
* with digit sum 5 and (55, 46) with digit
26+
* sum 10. Out of these the maximum combined
27+
* ability pair is (55, 46) i.e. 55 + 46 = 101
28+
*
29+
* Example 2:
30+
*
31+
* Input:
32+
* N = 4
33+
* arr[] = {18, 19, 23, 15}
34+
*
35+
* Output:
36+
* -1
37+
*
38+
* Explanation:
39+
* No two people are compatible with each other.
40+
*/
41+
public class RulingPair {
42+
43+
public int solve(int arr[], int n){
44+
45+
int maxSum = -1;
46+
HashMap<Integer, Integer> digitMaxMap = new HashMap<>();
47+
for(int num:arr) {
48+
int digitSum = 0;
49+
int tmp = num;
50+
while(tmp>0) {
51+
digitSum += tmp%10;
52+
tmp=tmp/10;
53+
}
54+
Integer maxVal = digitMaxMap.get(digitSum);
55+
if( maxVal == null) {
56+
digitMaxMap.put(digitSum, num);
57+
}else {
58+
int sum = maxVal+num;
59+
maxSum = Math.max(maxSum, sum);
60+
if(num>maxVal) {
61+
digitMaxMap.put(digitSum, num);
62+
}
63+
}
64+
}
65+
return maxSum;
66+
}
67+
68+
public static void main(String[] args) {
69+
int n = 5;
70+
int arr[] = new int[]{55,23,41,32,46,88};
71+
System.out.println(new RulingPair().solve(arr, n));
72+
}
73+
}

Diff for: geeksForGeeks/arrays/insertAtEnd.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package geeksForGeeks.arrays;
2+
3+
/**
4+
* Array insert at end Basic Accuracy: 71.42% Submissions: 10035 Points: 1 Insertion is a basic but
5+
* frequently used operation. Arrays in most languages can not be dynamically shrinked or expanded.
6+
* Here, we will work with such arrays and try to insert an element at the end of the array.
7+
*
8+
* <p>You are given an array arr. The size of the array is given by sizeOfArray. You need to insert
9+
* an element at the end.
10+
*
11+
* <p>Example 1:
12+
*
13+
* <p>Input: sizeOfArray = 6 arr[] = {1, 2, 3, 4, 5} element = 90 Output: 1 2 3 4 5 90 Explanation:
14+
* After inserting 90 at the end, we have array elements as 1 2 3 4 5 90. Example 2:
15+
*
16+
* <p>Input: sizeOfArray = 4 arr[] = {1, 2, 3} element = 50 Output: 1 2 3 50 Explanation: After
17+
* inserting 50 at the end, we have array elements as 1 2 3 50. Your Task: You don't need to read
18+
* input or print anything. You only need to complete the function insertAtEnd() that takes arr,
19+
* sizeOfArray, element as input and modifies arr as per requirements. The driver code takes care of
20+
* the printing.
21+
*
22+
* <p>Expected Time Complexity: O(1). Expected Auxiliary Space: O(1).
23+
*
24+
* <p>Constraints: 2 <= sizeOfArray <= 1000 0 <= element, arri <= 10^6
25+
*/
26+
public class insertAtEnd {
27+
static void insert(int arr[],int sizeOfArray,int element)
28+
{
29+
arr[sizeOfArray-1] = element;
30+
}
31+
public static void main(String[] args) {
32+
33+
}
34+
}

Diff for: geeksForGeeks/arrays/insertAtIndex.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package geeksForGeeks.arrays;
2+
3+
/**
4+
* Array insert at index Basic Accuracy: 51.49% Submissions: 14523 Points: 1
5+
* Insertion is a basic
6+
* but frequently used operation. Arrays in most languages cannnot be dynamically shrinked or
7+
* expanded. Here, we will work with such arrays and try to insert an element at some index.
8+
*
9+
* <p>You are given an array arr(0-based index). The size of the array is given by sizeOfArray. You
10+
* need to insert an element at given index and print the modified array.
11+
*
12+
* <p>Example 1:
13+
*
14+
* <p>Input: sizeOfArray = 6 arr[] = {1, 2, 3, 4, 5} index = 5, element = 90 Output: 1 2 3 4 5 90
15+
* Explanation: 90 is inserted at index 5(0-based indexing). After inserting, array elements are
16+
* like 1, 2, 3, 4, 5, 90. Example 2:
17+
*
18+
* <p>Input: sizeOfArray = 6 arr[] = {1, 2, 3, 4, 5} index = 2, element = 90 Output: 1 2 90 3 4 5
19+
* Explanation: 90 is inserted at index 2(0-based indexing). After inserting, array elements are
20+
* like 1, 2, 90, 3, 4, 5. Your Task: You don't need to read input or print anything.. The input is
21+
* already taken care of by the driver code. You only need to complete the function insertAtIndex()
22+
* that takes arr, sizeOfArray, index, element as input and modifies the array arr as per
23+
* requirements. The printing is done by driver code.
24+
*
25+
* <p>Expected Time Complexity: O(N). Expected Auxiliary Space: O(1).
26+
*
27+
* <p>Constraints: 2 <= sizeOfArray <= 1000 0 <= element, arri <= 10^6 0 <= index <= sizeOfArray-1
28+
*/
29+
public class insertAtIndex {
30+
void insert(int arr[], int sizeOfArray, int index, int element)
31+
{
32+
for(int i=sizeOfArray;i>index;i--){
33+
arr[i] = arr[i-1];
34+
}
35+
arr[index] = element;
36+
}
37+
}

Diff for: geeksForGeeks/arrays/largestAndSecondLargest.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package geeksForGeeks.arrays;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
6+
/**
7+
* Max and Second Max Easy Accuracy: 49.94% Submissions: 19600 Points: 2 Given an array arr[] of
8+
* size N of positive integers which may have duplicates. The task is to find the maximum and second
9+
* maximum from the array, and both of them should be distinct, so If no second max exists, then the
10+
* second max will be -1.
11+
*
12+
* <p>Example 1:
13+
*
14+
* <p>Input: N = 3 arr[] = {2,1,2} Output: 2 1 Explanation: From the given array elements, 2 is the
15+
* largest and 1 is the second largest. Example 2:
16+
*
17+
* <p>Input: N = 5 arr[] = {1,2,3,4,5} Output: 5 4 Explanation: From the given array elements, 5 is
18+
* the largest and 4 is the second largest. Your Task: The task is to complete the function
19+
* largestAndSecondLargest(), which should return maximum and second maximum element from the array
20+
* with first element as maximum element and second element as second maximum(if there is no second
21+
* maximum the second element should be -1)
22+
*
23+
* <p>Expected Time Complexity: O(N). Expected Auxiliary Space: O(1).
24+
*
25+
* <p>Constraints: 1 <= N <= 10^6 1 <= arr[i] <= 10^6
26+
*/
27+
public class largestAndSecondLargest {
28+
public static ArrayList<Integer> largestAndSecondLargest(int sizeOfArray, int arr[]) {
29+
ArrayList<Integer> res = new ArrayList<>();
30+
Arrays.sort(arr);
31+
res.add(arr[sizeOfArray - 1]);
32+
for (int i = sizeOfArray - 1; i >= 0; i--) {
33+
if (res.get(0) != arr[i]) {
34+
res.add(1, arr[i]);
35+
break;
36+
} else {
37+
res.add(1, -1);
38+
}
39+
}
40+
return res;
41+
}
42+
}

Diff for: geeksForGeeks/arrays/majorityWins.java

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package geeksForGeeks.arrays;
2+
3+
/**
4+
* Who has the majority? Basic Accuracy: 49.73% Submissions: 21797 Points: 1 We hope you are
5+
* familiar with using counter variables. Counting allows us to find how may times a certain element
6+
* appears in an array or list.
7+
*
8+
* <p>You are given an array arr[] of size N. You are also given two elements x and y. Now, you need
9+
* to tell which element (x or y) appears most in the array. In other words, return the element, x
10+
* or y, that has higher frequency in the array. If both elements have the same frequency, then just
11+
* return the smaller element.
12+
*
13+
* <p>NOTE : We need to return the elements, not their counts.
14+
*
15+
* <p>Example 1:
16+
*
17+
* <p>Input: N = 11 arr[] = {1,1,2,2,3,3,4,4,4,4,5} x = 4, y = 5 Output: 4 Explanation: n=11;
18+
* elements = {1,1,2,2,3,3,4,4,4,4,5}; x=4; y=5 x frequency in arr is = 4 times y frequency in arr
19+
* is = 1 times x has higher frequency, so we print 4. Example 2:
20+
*
21+
* <p>Input: N = 8 arr[] = {1,2,3,4,5,6,7,8} x = 1, y = 7 Output: 1 Explanation: n=8; elements =
22+
* {1,2,3,4,5,6,7,8}; x=1; y=7 x frequency in arr is 1 times y frequency in arr is 1 times both have
23+
* same frequency, so we look for the smaller element. x=1 is smaller than y=7, so print 1. Your
24+
* Task: You don't need to read input or print anything. You don't need to take any input, as it is
25+
* already accomplished by the driver code. You just need to complete the function majorityWins()
26+
* that takes array, n, x, y as parameters and return the element with highest frequency.
27+
*
28+
* <p>Expected Time Complexity: O(N). Expected Auxiliary Space: O(1).
29+
*
30+
* <p>Constraints: 1 <= n <= 10^3 0 <= arri , x , y <= 10^8
31+
*/
32+
public class majorityWins {
33+
int get(int arr[], int n, int x,int y)
34+
{
35+
int count_x=0;
36+
int count_y=0;
37+
for(int i=0;i<n;i++){
38+
if(arr[i] ==x){
39+
count_x++;
40+
}else if (arr[i] == y){
41+
count_y++;
42+
}
43+
}
44+
45+
if(count_x>count_y)
46+
return x;
47+
else if(count_y>count_x)
48+
return y;
49+
else
50+
{
51+
if(x>y){
52+
return y;
53+
}
54+
else{
55+
return x;
56+
}
57+
}
58+
59+
}
60+
}

Diff for: geeksForGeeks/arrays/maximumOccuredInteger.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package geeksForGeeks.arrays;
2+
3+
/**
4+
* Maximum occured integer Easy Accuracy: 44.71% Submissions: 11865 Points: 2 Given N integer
5+
* ranges, the task is to find the maximum occurring integer in these ranges. If more than one such
6+
* integer exits, find the smallest one. The ranges are given as two arrays L[] and R[]. L[i]
7+
* consists of starting point of range and R[i] consists of corresponding end point of the range.
8+
*
9+
* <p>For example consider the following ranges. L[] = {2, 1, 3}, R[] = {5, 3, 9) Ranges represented
10+
* by above arrays are. [2, 5] = {2, 3, 4, 5} [1, 3] = {1, 2, 3} [3, 9] = {3, 4, 5, 6, 7, 8, 9} The
11+
* maximum occurred integer in these ranges is 3.
12+
*
13+
* <p>Example 1:
14+
*
15+
* <p>Input: N = 4 L[] = {1,4,3,1} R[] = {15,8,5,4} Output: 4 Explanation: The given ranges are
16+
* [1,15] [4, 8] [3, 5] [1, 4]. The number that is most common or appears most times in the ranges
17+
* is 4.
18+
*
19+
* <p>Example 2:
20+
*
21+
* <p>Input: N = 5 L[] = {1,5,9,13,21} R[] = {15,8,12,20,30} Output: 5 Explanation: The given ranges
22+
* are [1,15] [5, 8] [9, 12] [13, 20] [21, 30]. The number that is most common or appears most times
23+
* in the ranges is 5.
24+
*
25+
* <p>Your Task: The task is to complete the function maxOccured() which returns the maximum occured
26+
* integer in all ranges.
27+
*
28+
* <p>Expected Time Complexity: O(N). Expected Auxiliary Space: O(N).
29+
*
30+
* <p>Constraints: 1 <= n <= 10^6 0 <= L[i], R[i] <= 10^6
31+
*/
32+
public class maximumOccuredInteger {
33+
34+
static int maxOccured(int L[], int R[], int n, int maxx) {
35+
36+
return 0;
37+
}
38+
39+
public static void main(String[] args) {}
40+
}

Diff for: geeksForGeeks/arrays/medianAndMean.java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package geeksForGeeks.arrays;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Mean And Median of Array Easy Accuracy: 50.0% Submissions: 15197 Points: 2 Given an array a[ ] of
7+
* size N. The task is to find the median and mean of the array elements. Mean is average of the
8+
* numbers and median is the element which is smaller than half of the elements and greater than
9+
* remaining half. If there are odd elements, the median is simply the middle element in the sorted
10+
* array. If there are even elements, then the median is floor of average of two middle numbers in
11+
* the sorted array. If mean is floating point number, then we need to print floor of it.
12+
*
13+
* <p>Note: To find the median, you might need to sort the array. Since sorting is covered in later
14+
* tracks, we have already provided the sort function to you in the code.
15+
*
16+
* <p>Example 1:
17+
*
18+
* <p>Input: N = 5 a[] = {1, 2, 19, 28, 5} Output: 11 5 Explanation: For array of 5 elements, mean
19+
* is (1 + 2 + 19 + 28 + 5)/5 = 11. Median is 5 (middle element after sorting) Example 2:
20+
*
21+
* <p>Input: N = 4 a[] = {2, 8, 3, 4} Output: 4 3 Explanation: For array of 4 elements, mean is
22+
* floor((2 + 8 + 3 + 4)/4) = 4. Median is floor((4 + 3)/2) = 3 Your Task: You don't need to read
23+
* input or print anything.. You just need to complete the following two function:
24+
*
25+
* <p>mean(): It takes the array and its size N as parameters and returns the mean as an integer.
26+
* median(): It takes the array and its size N as parameters and returns the median as an integer.
27+
* Expected Time Complexity: O(N log(N)). Expected Auxiliary Space: O(1).
28+
*
29+
* <p>Constraints: 1 <= N <= 10^6 1 <= a[i] <= 10^6
30+
*/
31+
public class medianAndMean {
32+
public int median(int A[], int N) {
33+
34+
Arrays.sort(A);
35+
if (N % 2 == 0) {
36+
return ((A[N / 2] + A[(N - 1) / 2]) / 2);
37+
} else {
38+
return A[N / 2];
39+
}
40+
41+
// Your code here
42+
// If median is fraction then conver it to integer and return
43+
}
44+
45+
public int mean(int A[], int N) {
46+
// Your code here
47+
int sum = 0;
48+
for (int i = 0; i < N; i++) {
49+
sum += A[i];
50+
}
51+
return sum / N;
52+
}
53+
}

0 commit comments

Comments
 (0)