Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want to contribute #23

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions 1_Arrays/Array01_MaxAndMiniElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Q. Given an array of size N. The task is to find the maximum and the minimum element of the array using the minimum number of comparisons.

Examples:
Input: arr[] = {3, 5, 4, 1, 9}
Output: Minimum element is: 1
Maximum element is: 9

Input: arr[] = {22, 14, 8, 17, 35, 3}
Output: Minimum element is: 3
Maximum element is: 35
*/

public class Array01_MaxAndMiniElement{

//search for maximum element in array
public static int maxElement(int arr[]){ //TC -> O(n)
int max = Integer.MIN_VALUE; //take minimum value such as -infinity
for(int i = 0; i < arr.length; i++){
if(arr[i] > max){ //compare
max = arr[i];
}
}
return max;
}

//search for minimum element in a array
public static int minElement(int arr[]){ //TC -> O(n)
int min = Integer.MAX_VALUE; //take maximum value such as +infinity
for(int i = 0; i < arr.length; i++){
if(arr[i] < min){ //compare
min = arr[i];
}
}
return min;
}
public static void main(String args[]){
int arr1[] = {3, 5, 4, 1, 9};
int arr2[] = {22, 14, 8, 17, 35, 5};
int arr3[] = {1000, 11, 445, 1, 330, 3000};
int arr[] = {1000, 11, 445, 1, 330, 3000, 3, 5, 4, 1, 9, 22, 14, 8, 17, 35, 5};
System.out.println("Maximum element -> "+ maxElement(arr));
System.out.println("Minimum element -> "+ minElement(arr));
}
}
64 changes: 64 additions & 0 deletions 1_Arrays/Array02_ReverseArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Q. Given an array (or string), the task is to reverse the array/string.
Examples : 
Input : arr[] = {1, 2, 3}
Output : arr[] = {3, 2, 1}

Input : arr[] = {4, 5, 1, 2}
Output : arr[] = {2, 1, 5, 4}
*/

public class Array02_ReverseArray {
/*----Approach 1 --> Iterative way ----*/
public static void reverseArray(int arr[]){ //TC -> O(n)
int startIndex = 0; //always start from index 0
int endIndex = arr.length-1; //array have 0 to n-1 index & n is the size of array
while(startIndex < endIndex){
//swap
int temp = arr[startIndex];
arr[startIndex] = arr[endIndex];
arr[endIndex] = temp;

//change index
startIndex++;
endIndex--;
}

}
/*---- ----*/
/*----Approach 2 --> recursive way ----*/
public static void reverseArray(int arr[], int startIndex, int endIndex){ //TC -> O(n)
//base case
if(startIndex >= endIndex){
return;
}
//swap
int temp = arr[startIndex];
arr[startIndex] = arr[endIndex];
arr[endIndex] = temp;

//calling recursive function
reverseArray(arr, startIndex+1, endIndex-1);
}
/*---- ----*/
//print array
public static void printArray(int arr[]){
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
}

public static void main(String args[]){
int arr1[] = {1, 2, 3} , arr2[] = {4, 5, 1, 2};
int arr[] = {1, 2, 3, 4, 5, 1, 2};
System.out.print("original Array -> ");
printArray(arr2);

reverseArray(arr2);
// reverseArray(arr, 0, arr.length-1);

System.out.print("new Array -> ");
printArray(arr2);
}
}
56 changes: 56 additions & 0 deletions 1_Arrays/Array03_MaximumSumSubArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Given an integer array nums, find the subarray with the largest sum, and return its sum.

Example 1:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: The subarray [4,-1,2,1] has the largest sum 6.

Example 2:
Input: nums = [1]
Output: 1
Explanation: The subarray [1] has the largest sum 1.

Example 3:
Input: nums = [5,4,-1,7,8]
Output: 23
Explanation: The subarray [5,4,-1,7,8] has the largest sum 23. 

Constraints:
1 <= nums.length <= 105
-104 <= nums[i] <= 104


Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

*/

public class Array03_MaximumSumSubArray {

/*---- kadaen's method ----*/
public static int maxSumOfSubArray(int arr[]){
//step 1 -> initialize values
int currSum = 0;
int maxSum = Integer.MIN_VALUE; //it refers to -infinity

//step 2 -> run a loop and do work
for(int i = 0; i < arr.length; i++){
currSum += arr[i];//sum
if(currSum <= 0){ //if current sum less then 0, then reassigned the value 0 to current sum
currSum = 0;
}
if(maxSum < currSum){ //compare current sum and maximum sum
maxSum = currSum;
}
}

return maxSum;
}

public static void main(String[] args) {
int nums1[] = {-2,1,-3,4,-1,2,1,-5,4};
int nums2[] = {1};
int nums3[] = {5,4,-1,7,8};
System.out.println("Maximum sum SubArray -> "+maxSumOfSubArray(nums3));
}
}
40 changes: 40 additions & 0 deletions 1_Arrays/Array04_DuplicateValues.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

Example 1:
Input: nums = [1,2,3,1]
Output: true

Example 2:
Input: nums = [1,2,3,4]
Output: false

Example 3:
Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true

Constraints:
1 <= nums.length <= 105
-109 <= nums[i] <= 109
*/

public class Array04_DuplicateValues {

public static boolean checkDuplicate(int nums[]){ //TC -> O(n^2)
for(int i = 0; i < nums.length-1; i++){
for(int j = i+1; j <nums.length; j++ ){
if(nums[i] == nums[j]){
return true;
}
}
}
return false;
}
public static void main(String args[]) {
int nums1[] = {1,2,3,1};
int nums2[] = {1,2,3,4};
int nums3[] = {1,1,1,3,3,4,3,2,4,2};

System.out.println(checkDuplicate(nums3));
}
}
74 changes: 74 additions & 0 deletions 1_Arrays/Array05_ChocolateDistributionProblem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Given an array of N integers where each value represents the number of chocolates in a packet.Each packet can have a variable number of chocolates. There are m students, the task is to distribute chocolate packets such that: 
Each student gets one packet.
The difference between the number of chocolates in the packet with maximum chocolates and the packet with minimum chocolates given to the students is minimum.

Examples:
Input : arr[] = {7, 3, 2, 4, 9, 12, 56} , m = 3 
Output: Minimum Difference is 2 
Explanation:
We have seven packets of chocolates and we need to pick three packets for 3 students 
If we pick 2, 3 and 4, we get the minimum difference between maximum and minimum packet sizes.

Input : arr[] = {3, 4, 1, 9, 56, 7, 9, 12} , m = 5 
Output: Minimum Difference is 6 

Input : arr[] = {12, 4, 7, 9, 2, 23, 25, 41, 30, 40, 28, 42, 30, 44, 48, 43, 50} , m = 7 
Output: Minimum Difference is 10 
*/

import java.util.Arrays;
public class Array05_ChocolateDistributionProblem {

public static int minDiff(int arr[], int m){ //TC -> O(n*log(n))
//step 1 -> sort the array(we can use java in build sorting algo <<or>> we can make our own sorting algo)
Arrays.sort(arr); //java built in sort
// bubbleSort(arr); //we can also use .... user defined sorting instate of build in sort
//step 2 -> initialize +infinity & run a loop
int minDiff = Integer.MAX_VALUE; //+infinity
for(int i = 0; i < arr.length; i++){
//step 3 -> difference between maximum and minimum
int diff = arr[i+m-1]-arr[i];
//step 4 -> compare
if(minDiff > diff){
minDiff = diff;
}
//corner case -> this will stop the loop
if((i+m-1) >= arr.length-1){
break;
}
}

return minDiff;
}
/*---- Bubble Sort algo ----*/
public static void bubbleSort(int arr[]){
for(int i = 0; i < arr.length-1; i++){
int track = 0;
for(int j = 0; j < arr.length-1-i; j++){
if(arr[j] > arr[j+1]){
//swap
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
track++;
}
}
if(track == 0){
break;
}
}
}
/*---- ----*/

public static void main(String args[]){
int arr1[] = {7, 3, 2, 4, 9, 12, 56};
int arr2[] = {3, 4, 1, 9, 56, 7, 9, 12};
int arr3[] = {12, 4, 7, 9, 2, 23, 25, 41, 30, 40, 28, 42, 30, 44, 48, 43, 50};
int arr_worstCase[] = {32,24,15,46,52,57,5,39};

System.out.println("Minimum difference -> "+minDiff(arr_worstCase, 2));


}
}
Loading