From 38daad8fa98615b8a6e686885a0a4a6452f59543 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Sun, 5 Dec 2021 20:16:29 +0530 Subject: [PATCH 01/32] Fixes: #{2391} --- .../com/thealgorithms/strings/Anagrams.java | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 src/main/java/com/thealgorithms/strings/Anagrams.java diff --git a/src/main/java/com/thealgorithms/strings/Anagrams.java b/src/main/java/com/thealgorithms/strings/Anagrams.java new file mode 100644 index 000000000000..1db3bf1b7bf2 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/Anagrams.java @@ -0,0 +1,150 @@ +/** Author : Siddhant Swarup Mallick + * Github : https://github.com/siddhant2002 + */ + +/** PROBLEM DESCRIPTION : + * An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.[1] For example, the word anagram itself can be rearranged into nag a ram, also the word binary into brainy and the word adobe into abode. Reference from https://en.wikipedia.org/wiki/Anagram + */ + +package com.thealgorithms.strings; +import java.util.*; +public class Anagrams +{ + // 4 approaches are provided for anagram checking. approach 2 and approach 3 are similar but differ in running time. + public static void main(String args[]) { + String first = "deal"; + String second = "lead"; + // All the below methods takes input but doesn't return any output to the main method. + Anagrams nm=new Anagrams(); + nm.approach2(first, second); /* To activate methods for different approaches*/ + nm.approach1(first, second); /* To activate methods for different approaches*/ + nm.approach3(first, second); /* To activate methods for different approaches*/ + nm.approach4(first, second); /* To activate methods for different approaches*/ + + /** + * OUTPUT : + * first string ="deal" second string ="lead" + * Output: Anagram + * Input and output is constant for all four approaches + * 1st approach Time Complexity : O(n logn) + * Auxiliary Space Complexity : O(1) + * 2nd approach Time Complexity : O(n) + * Auxiliary Space Complexity : O(1) + * 3rd approach Time Complexity : O(n) + * Auxiliary Space Complexity : O(1) + * 4th approach Time Complexity : O(n) + * Auxiliary Space Complexity : O(n) + */ + } + + void approach1(String s, String t) + { + if (s.length() != t.length()) + { + System.out.println(false); + } + else + { + char c[] = s.toCharArray(); + char d[] = t.toCharArray(); + Arrays.sort(c); + Arrays.sort(d); /* In this approach the strings are stored in the character arrays and both the arrays are sorted. After that both the arrays are compared for checking anangram */ + if (Arrays.equals(c, d)) + { + System.out.println("Anagram"); + } else + { + System.out.println("Not Anagram"); + } + } + } + + void approach2(String a, String b) + { + if(a.length()!=b.length()) + { + System.out.println("Not Anagram"); + } + else + { + int m[]=new int[26]; + int n[]=new int[26]; + for(char c: a.toCharArray()) + { + m[c-'a']++; + } + // In this approach the frequency of both the strings are stored and after that the frequencies are iterated from 0 to 26(from 'a' to 'z' ). If the frequencies match then anagram message is displayed + // Running time and space complexity of this algo is less as compared to others + for(char c:b.toCharArray()) + { + n[c-'a']++; + } + for(int i=0;i<26;i++) + { + if(m[i]!=n[i]) + { + System.out.println("Not Anagram"); + } + } + System.out.println("Anagram"); + } + } + + void approach3(String s, String t) + { + if(s.length()!=t.length()) + { + System.out.println("Not Anagram"); + } + // this is similar to approach number 2 but here the string is not converted to character array + else + { + int a[]=new int[26]; + int b[]=new int[26]; + int k=s.length(); + for(int i=0;i nm=new HashMap<>(); + HashMap kk=new HashMap<>(); + for(char c: s.toCharArray()) + { + nm.put(c, nm.getOrDefault(c,0)+1); + } + for(char c: t.toCharArray()) + { + + kk.put(c, kk.getOrDefault(c,0)+1); + } + // It checks for equal frequencies + for(char c:nm.keySet()) + { + if(!nm.get(c).equals(kk.get(c))) + { + System.out.println("Not Anagram"); + } + } + System.out.println("Anagram"); + } + } +} \ No newline at end of file From a4172291f5745bd11c02580f9468225edf07bf2c Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Tue, 7 Dec 2021 18:21:15 +0530 Subject: [PATCH 02/32] Fixes #(2391) --- .../com/thealgorithms/strings/Anagrams.java | 44 +++++++++---------- .../strings/String_Comparision.java | 0 2 files changed, 22 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/thealgorithms/strings/String_Comparision.java diff --git a/src/main/java/com/thealgorithms/strings/Anagrams.java b/src/main/java/com/thealgorithms/strings/Anagrams.java index 1db3bf1b7bf2..f76db06c4d58 100644 --- a/src/main/java/com/thealgorithms/strings/Anagrams.java +++ b/src/main/java/com/thealgorithms/strings/Anagrams.java @@ -16,10 +16,10 @@ public static void main(String args[]) { String second = "lead"; // All the below methods takes input but doesn't return any output to the main method. Anagrams nm=new Anagrams(); - nm.approach2(first, second); /* To activate methods for different approaches*/ - nm.approach1(first, second); /* To activate methods for different approaches*/ - nm.approach3(first, second); /* To activate methods for different approaches*/ - nm.approach4(first, second); /* To activate methods for different approaches*/ + System.out.println(nm.approach2(first, second)); /* To activate methods for different approaches*/ + System.out.println(nm.approach1(first, second)); /* To activate methods for different approaches*/ + System.out.println(nm.approach3(first, second)); /* To activate methods for different approaches*/ + System.out.println(nm.approach4(first, second)); /* To activate methods for different approaches*/ /** * OUTPUT : @@ -37,11 +37,11 @@ public static void main(String args[]) { */ } - void approach1(String s, String t) + boolean approach1(String s, String t) { if (s.length() != t.length()) { - System.out.println(false); + return false; } else { @@ -51,19 +51,19 @@ void approach1(String s, String t) Arrays.sort(d); /* In this approach the strings are stored in the character arrays and both the arrays are sorted. After that both the arrays are compared for checking anangram */ if (Arrays.equals(c, d)) { - System.out.println("Anagram"); + return true; } else { - System.out.println("Not Anagram"); + return false; } } } - void approach2(String a, String b) + boolean approach2(String a, String b) { if(a.length()!=b.length()) { - System.out.println("Not Anagram"); + return false; } else { @@ -73,7 +73,7 @@ void approach2(String a, String b) { m[c-'a']++; } - // In this approach the frequency of both the strings are stored and after that the frequencies are iterated from 0 to 26(from 'a' to 'z' ). If the frequencies match then anagram message is displayed + // In this approach the frequency of both the strings are stored and after that the frequencies are iterated from 0 to 26(from 'a' to 'z' ). If the frequencies match then anagram message is displayed in the form of boolean format // Running time and space complexity of this algo is less as compared to others for(char c:b.toCharArray()) { @@ -83,18 +83,18 @@ void approach2(String a, String b) { if(m[i]!=n[i]) { - System.out.println("Not Anagram"); + return false; } } - System.out.println("Anagram"); + return true; } } - void approach3(String s, String t) + boolean approach3(String s, String t) { if(s.length()!=t.length()) { - System.out.println("Not Anagram"); + return false; } // this is similar to approach number 2 but here the string is not converted to character array else @@ -110,19 +110,19 @@ void approach3(String s, String t) for(int i=0;i<26;i++) { if(a[i]!=b[i]) - System.out.println("Not Anagram"); + return false; } - System.out.println("Anagram"); + return true; } } - void approach4(String s, String t) + boolean approach4(String s, String t) { if(s.length()!=t.length()) { - System.out.println("Not Anagram"); + return false; } - // This approach is done using hashmap where frequencies are stored and checked iteratively and if all the frequencies of first string match with the second string then anagram message is displayed + // This approach is done using hashmap where frequencies are stored and checked iteratively and if all the frequencies of first string match with the second string then anagram message is displayed in boolean format else { HashMap nm=new HashMap<>(); @@ -141,10 +141,10 @@ void approach4(String s, String t) { if(!nm.get(c).equals(kk.get(c))) { - System.out.println("Not Anagram"); + return false; } } - System.out.println("Anagram"); + return true; } } } \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/strings/String_Comparision.java b/src/main/java/com/thealgorithms/strings/String_Comparision.java new file mode 100644 index 000000000000..e69de29bb2d1 From e3c28dc4a8b56687faf4d82952bb5e881b2a689b Mon Sep 17 00:00:00 2001 From: Andrii Siriak Date: Thu, 9 Dec 2021 20:45:40 +0200 Subject: [PATCH 03/32] Delete String_Comparision.java --- src/main/java/com/thealgorithms/strings/String_Comparision.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/strings/String_Comparision.java diff --git a/src/main/java/com/thealgorithms/strings/String_Comparision.java b/src/main/java/com/thealgorithms/strings/String_Comparision.java deleted file mode 100644 index e69de29bb2d1..000000000000 From f27eff41930233b95dc0a636c61c42ea9e6c9cba Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Tue, 14 Dec 2021 09:24:30 +0530 Subject: [PATCH 04/32] Fixes: #{2820} --- .../thealgorithms/strings/String_Compare.java | 47 ++++++++++++++ .../strings/String_Compare1.java | 62 +++++++++++++++++++ .../strings/String_Compare2.java | 43 +++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 src/main/java/com/thealgorithms/strings/String_Compare.java create mode 100644 src/main/java/com/thealgorithms/strings/String_Compare1.java create mode 100644 src/main/java/com/thealgorithms/strings/String_Compare2.java diff --git a/src/main/java/com/thealgorithms/strings/String_Compare.java b/src/main/java/com/thealgorithms/strings/String_Compare.java new file mode 100644 index 000000000000..0b0b4bfb9fa8 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/String_Compare.java @@ -0,0 +1,47 @@ +/** Author : Siddhant Swarup Mallick + * Github : https://github.com/siddhant2002 + */ + + /** Program description - To sort the names alphabetically using comparators and collection class + */ + package com.thealgorithms.strings; +import java.util.*; +public class String_Compare { + public static void main(String args[]) + { + // list is created of string type where names are initialized + List nm = new ArrayList<>(); + nm.add("Shah"); + nm.add("Chhotwant"); + nm.add("Ayush"); + nm.add("Mannu"); + nm.add("Sachin"); + nm.add("Siddhi"); + nm.add("Urus"); + nm.add("Mony"); + String_Compare3 kk=new String_Compare3(); + nm=kk.task(nm); + System.out.println(nm); + // task method is executed and the returned result is stored in the list and after that the list is displayed + + /** + * OUTPUT : + * Input - ["Shah","Chhotwant","Ayush","Mannu","Sachin","Siddhi","Urus","Mony"] + * Output: [Ayush, Chhotwant, Mannu, Mony, Sachin, Shah, Siddhi, Urus] + * 1st approach Time Complexity : O(n logn) + * Auxiliary Space Complexity : O(n) + * */ + } + List task(List nm) + { + Collections.sort(nm , new Comparator(){ + public int compare(String s1 , String s2) + { + return s1.compareTo(s2); + } + }); + return nm; + + // comparator method is used to sort the names in ascending order/alphabetically. To display in reverse order then line number 32 should be changed to s2.compareTo(s1) + } +} \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/strings/String_Compare1.java b/src/main/java/com/thealgorithms/strings/String_Compare1.java new file mode 100644 index 000000000000..8da5d26d9785 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/String_Compare1.java @@ -0,0 +1,62 @@ +/** Author : Siddhant Swarup Mallick + * Github : https://github.com/siddhant2002 + */ + + /** Program description - To check strings as per object values and string content + */ + + +package com.thealgorithms.strings; +public class String_Compare1 { + public static void main(String args[]) + { + // 4 methods are called which are assigned to different sort of task and the return type is in boolean + String_Compare1 nm=new String_Compare1(); + String s1="Alpha"; + String s2="Alpha"; + String s3=new String(s2); + System.out.println(nm.task(s1,s2)); + System.out.println(nm.task1(s1,s3)); + System.out.println(nm.task2(s1,s2)); + System.out.println(nm.task3(s1,s3)); + + /** + * OUTPUT : + * The input is common for all the methods + * first string ="Alpha" second string ="Alpha" third string ="Alpha" + * Output: true/false + * 1st Output - true + * 1st approach Time Complexity : O(1) + * Auxiliary Space Complexity : O(1) + * 2nd output - false + * 2nd approach Time Complexity : O(1) + * Auxiliary Space Complexity : O(1) + * 3rd output - true + * 3rd approach Time Complexity : O(1) + * Auxiliary Space Complexity : O(1) + * 4th output - true + * 4th approach Time Complexity : O(1) + * Auxiliary Space Complexity : O(1) + */ + } + boolean task(String s1, String s2) + { + return s1==s2; + // Here the answer is true because the object of strings s1 and s2 are same so the object value is checked instead of their content + } + boolean task1(String s1, String s2) + { + return s1==s2; + // Here the answer is false because the object of strings s1 and s2 are different so the object value is checked instead of their content + } + boolean task2(String s1, String s2) + { + return s1.equals(s2); + // Here the answer is true because the content of the strings are checked irrespective of their objects location + } + boolean task3(String s1, String s2) + { + return s1.equals(s2); + // Here the answer is true because the content of the strings are checked irrespective of their objects location + } +} diff --git a/src/main/java/com/thealgorithms/strings/String_Compare2.java b/src/main/java/com/thealgorithms/strings/String_Compare2.java new file mode 100644 index 000000000000..c678d67cf2bb --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/String_Compare2.java @@ -0,0 +1,43 @@ +/** Author : Siddhant Swarup Mallick + * Github : https://github.com/siddhant2002 + */ + + /** Program description - Showing compareTo function in 2 different ways. In first method using compareTo function to generate the length difference and in the second method compareTo function is used to generate the difference between the ascii codes of the characters of the strings + */ + +package com.thealgorithms.strings; +public class String_Compare2 { + public static void main(String args[]) + { + // 2 methods are called to execute the functions and return is in integer + String_Compare2 nm=new String_Compare2(); + String s1="Alpha123"; + String s2="Alpha"; + System.out.println(nm.task(s1,s2)); + s1="Highway"; + s2="Harry"; + System.out.println(nm.task(s1,s2)); + + /** + * OUTPUT : + * 1st input - 1st string="Alpha123" 2nd string="Alpha" + * 1st Output - 3 + * 1st approach Time Complexity : O(1) + * Auxiliary Space Complexity : O(1) + * 2nd input - 1st string="Highway" 2nd string="Harry" + * 2nd output - 8 + * 2nd approach Time Complexity : O(1) + * Auxiliary Space Complexity : O(1) + * */ + } + int task(String s1, String s2) + { + return s1.compareTo(s2); + // Here the output will be displayed as per the length between the two strings. So 3 will be displayed + } + int task1(String s1, String s2) + { + return s1.compareTo(s2); + // Here the output will be displayed as per the ascii code the characters present in both the strings. So 8 will be the output + } +} \ No newline at end of file From 9e77af519aa0f2eb5d1ebc6e0eabee250d7d0f9b Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Tue, 14 Dec 2021 12:34:10 +0530 Subject: [PATCH 05/32] Fixes #{2820} --- src/main/java/com/thealgorithms/strings/String_Compare.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/strings/String_Compare.java b/src/main/java/com/thealgorithms/strings/String_Compare.java index 0b0b4bfb9fa8..967a10091fdb 100644 --- a/src/main/java/com/thealgorithms/strings/String_Compare.java +++ b/src/main/java/com/thealgorithms/strings/String_Compare.java @@ -19,7 +19,7 @@ public static void main(String args[]) nm.add("Siddhi"); nm.add("Urus"); nm.add("Mony"); - String_Compare3 kk=new String_Compare3(); + String_Compare kk=new String_Compare(); nm=kk.task(nm); System.out.println(nm); // task method is executed and the returned result is stored in the list and after that the list is displayed From a192de6811d0c54ef057e251e8c51432d89c6584 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Mon, 20 Dec 2021 10:39:44 +0530 Subject: [PATCH 06/32] Fixes(#2450) --- .../thealgorithms/sorts/LinkList_Sort.java | 301 ++++++++++++++++++ 1 file changed, 301 insertions(+) create mode 100644 src/main/java/com/thealgorithms/sorts/LinkList_Sort.java diff --git a/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java b/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java new file mode 100644 index 000000000000..5e23b2baf97f --- /dev/null +++ b/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java @@ -0,0 +1,301 @@ +/** Author : Siddhant Swarup Mallick + * Github : https://github.com/siddhant2002 + */ + + /** Program description - To sort the LinkList as per sorting technique */ + +package com.thealgorithms.sorts; + +public class LinkList_Sort { + public static void main(String args[]) + { + int a[] = {89,56,98,123,26,75,12,40,39,68,91}; + // Array is taken as input + int ch=1; + // Choice is choosed as 1(Merge sort) + switch(ch) + { + case 1: + Task nm=new Task(); + Node start=null,prev=null,fresh,ptr; + for(int i=0;i=n[i]) + b[k++]=n[i++]; + else + b[k++]=n[j++]; + } + // Smallest number is stored after checking from both the arrays + while(i<=m) + { + b[k++]=n[i++]; + } + while(j<=e) + { + b[k++]=n[j++]; + } + for(int p=s;p<=e;p++) + { + a[p]=b[p-s]; + } + } + // The method task and task1 is used to sort the linklist using merge sort +} +class Task1 +{ + public Node sort_by_insertionsort(Node head) { + if(head==null||head.next==null) + return head; + int c=count(head); + int a[]=new int[c]; + // Array of size c is created + a[0]=head.val; + int i; + Node ptr; + for(ptr=head.next,i=1;ptr!=null;ptr=ptr.next,i++) + { + int j=i-1; + while(j>=0&&a[j]>ptr.val) + { + // values are stored in the array + a[j+1]=a[j]; + j--; + } + a[j+1]=ptr.val; + } + i=0; + for(ptr=head;ptr!=null;ptr=ptr.next) + { + ptr.val=a[i++]; + // Value is stored in the linklist after being sorted + } + return head; + } + static int count(Node head) + { + Node ptr; + int c=0; + for(ptr=head;ptr!=null;ptr=ptr.next) + { + c++; + } + return c; + // This Method is used to count number of elements/nodes present in the linklist + // It will return a integer type value denoting the number of nodes present + } + // The method task and task1 is used to sort the linklist using insertion sort +} +class Task2 +{ + static int a[]; + public Node sort_by_heapsort(Node head) { + if(head==null||head.next==null) + return head; + int c=count(head); + a=new int[c]; + // Array of size c is created + int i=0; + for(Node ptr=head;ptr!=null;ptr=ptr.next) + { + a[i++]=ptr.val; + // values are stored in the array + } + i=0; + task(a); + for(Node ptr=head;ptr!=null;ptr=ptr.next) + { + ptr.val=a[i++]; + // Value is stored in the linklist after being sorted + } + return head; + } + int count(Node head) + { + int c=0; + Node ptr; + for(ptr=head;ptr!=null;ptr=ptr.next) + { + c++; + } + return c; + // This Method is used to count number of elements/nodes present in the linklist + // It will return a integer type value denoting the number of nodes present + } + void task(int n[]) + { + int k=n.length; + for(int i=k/2-1;i>=0;i--) + { + task1(n,k,i); + } + for(int i=k-1;i>0;i--) + { + int d=n[0]; + n[0]=n[i]; + n[i]=d; + task1(n,i,0); + // recursive calling of task1 method + } + } + void task1(int n[], int k, int i) + { + int p=i; + int l=2*i+1; + int r=2*i+2; + if(ln[p]) + p=l; + if(rn[p]) + p=r; + if(p!=i) + { + int d=n[p]; + n[p]=n[i]; + n[i]=d; + task1(n,k,p); + } + } + // The method task and task1 is used to sort the linklist using heap sort +} From 10e58e39a7bef930c7480b2eedb56fc550034063 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Tue, 21 Dec 2021 08:02:04 +0530 Subject: [PATCH 07/32] string Codes removed --- .../thealgorithms/strings/String_Compare.java | 47 -------------- .../strings/String_Compare1.java | 62 ------------------- .../strings/String_Compare2.java | 43 ------------- 3 files changed, 152 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/strings/String_Compare.java delete mode 100644 src/main/java/com/thealgorithms/strings/String_Compare1.java delete mode 100644 src/main/java/com/thealgorithms/strings/String_Compare2.java diff --git a/src/main/java/com/thealgorithms/strings/String_Compare.java b/src/main/java/com/thealgorithms/strings/String_Compare.java deleted file mode 100644 index 967a10091fdb..000000000000 --- a/src/main/java/com/thealgorithms/strings/String_Compare.java +++ /dev/null @@ -1,47 +0,0 @@ -/** Author : Siddhant Swarup Mallick - * Github : https://github.com/siddhant2002 - */ - - /** Program description - To sort the names alphabetically using comparators and collection class - */ - package com.thealgorithms.strings; -import java.util.*; -public class String_Compare { - public static void main(String args[]) - { - // list is created of string type where names are initialized - List nm = new ArrayList<>(); - nm.add("Shah"); - nm.add("Chhotwant"); - nm.add("Ayush"); - nm.add("Mannu"); - nm.add("Sachin"); - nm.add("Siddhi"); - nm.add("Urus"); - nm.add("Mony"); - String_Compare kk=new String_Compare(); - nm=kk.task(nm); - System.out.println(nm); - // task method is executed and the returned result is stored in the list and after that the list is displayed - - /** - * OUTPUT : - * Input - ["Shah","Chhotwant","Ayush","Mannu","Sachin","Siddhi","Urus","Mony"] - * Output: [Ayush, Chhotwant, Mannu, Mony, Sachin, Shah, Siddhi, Urus] - * 1st approach Time Complexity : O(n logn) - * Auxiliary Space Complexity : O(n) - * */ - } - List task(List nm) - { - Collections.sort(nm , new Comparator(){ - public int compare(String s1 , String s2) - { - return s1.compareTo(s2); - } - }); - return nm; - - // comparator method is used to sort the names in ascending order/alphabetically. To display in reverse order then line number 32 should be changed to s2.compareTo(s1) - } -} \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/strings/String_Compare1.java b/src/main/java/com/thealgorithms/strings/String_Compare1.java deleted file mode 100644 index 8da5d26d9785..000000000000 --- a/src/main/java/com/thealgorithms/strings/String_Compare1.java +++ /dev/null @@ -1,62 +0,0 @@ -/** Author : Siddhant Swarup Mallick - * Github : https://github.com/siddhant2002 - */ - - /** Program description - To check strings as per object values and string content - */ - - -package com.thealgorithms.strings; -public class String_Compare1 { - public static void main(String args[]) - { - // 4 methods are called which are assigned to different sort of task and the return type is in boolean - String_Compare1 nm=new String_Compare1(); - String s1="Alpha"; - String s2="Alpha"; - String s3=new String(s2); - System.out.println(nm.task(s1,s2)); - System.out.println(nm.task1(s1,s3)); - System.out.println(nm.task2(s1,s2)); - System.out.println(nm.task3(s1,s3)); - - /** - * OUTPUT : - * The input is common for all the methods - * first string ="Alpha" second string ="Alpha" third string ="Alpha" - * Output: true/false - * 1st Output - true - * 1st approach Time Complexity : O(1) - * Auxiliary Space Complexity : O(1) - * 2nd output - false - * 2nd approach Time Complexity : O(1) - * Auxiliary Space Complexity : O(1) - * 3rd output - true - * 3rd approach Time Complexity : O(1) - * Auxiliary Space Complexity : O(1) - * 4th output - true - * 4th approach Time Complexity : O(1) - * Auxiliary Space Complexity : O(1) - */ - } - boolean task(String s1, String s2) - { - return s1==s2; - // Here the answer is true because the object of strings s1 and s2 are same so the object value is checked instead of their content - } - boolean task1(String s1, String s2) - { - return s1==s2; - // Here the answer is false because the object of strings s1 and s2 are different so the object value is checked instead of their content - } - boolean task2(String s1, String s2) - { - return s1.equals(s2); - // Here the answer is true because the content of the strings are checked irrespective of their objects location - } - boolean task3(String s1, String s2) - { - return s1.equals(s2); - // Here the answer is true because the content of the strings are checked irrespective of their objects location - } -} diff --git a/src/main/java/com/thealgorithms/strings/String_Compare2.java b/src/main/java/com/thealgorithms/strings/String_Compare2.java deleted file mode 100644 index c678d67cf2bb..000000000000 --- a/src/main/java/com/thealgorithms/strings/String_Compare2.java +++ /dev/null @@ -1,43 +0,0 @@ -/** Author : Siddhant Swarup Mallick - * Github : https://github.com/siddhant2002 - */ - - /** Program description - Showing compareTo function in 2 different ways. In first method using compareTo function to generate the length difference and in the second method compareTo function is used to generate the difference between the ascii codes of the characters of the strings - */ - -package com.thealgorithms.strings; -public class String_Compare2 { - public static void main(String args[]) - { - // 2 methods are called to execute the functions and return is in integer - String_Compare2 nm=new String_Compare2(); - String s1="Alpha123"; - String s2="Alpha"; - System.out.println(nm.task(s1,s2)); - s1="Highway"; - s2="Harry"; - System.out.println(nm.task(s1,s2)); - - /** - * OUTPUT : - * 1st input - 1st string="Alpha123" 2nd string="Alpha" - * 1st Output - 3 - * 1st approach Time Complexity : O(1) - * Auxiliary Space Complexity : O(1) - * 2nd input - 1st string="Highway" 2nd string="Harry" - * 2nd output - 8 - * 2nd approach Time Complexity : O(1) - * Auxiliary Space Complexity : O(1) - * */ - } - int task(String s1, String s2) - { - return s1.compareTo(s2); - // Here the output will be displayed as per the length between the two strings. So 3 will be displayed - } - int task1(String s1, String s2) - { - return s1.compareTo(s2); - // Here the output will be displayed as per the ascii code the characters present in both the strings. So 8 will be the output - } -} \ No newline at end of file From d24fdf2852fb50f39a1bd707aaeed9e9b24fb562 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Wed, 22 Dec 2021 23:12:10 +0530 Subject: [PATCH 08/32] fixes(#2882) --- .../thealgorithms/sorts/LinkList_Sort.java | 405 +++++++++--------- 1 file changed, 202 insertions(+), 203 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java b/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java index 5e23b2baf97f..fc05120c1fb6 100644 --- a/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java +++ b/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java @@ -2,224 +2,228 @@ * Github : https://github.com/siddhant2002 */ - /** Program description - To sort the LinkList as per sorting technique */ +/** Program description - To sort the LinkList as per sorting technique */ package com.thealgorithms.sorts; public class LinkList_Sort { - public static void main(String args[]) - { - int a[] = {89,56,98,123,26,75,12,40,39,68,91}; + public static void main(String args[]) { + int a[] = { 89, 56, 98, 123, 26, 75, 12, 40, 39, 68, 91 }; // Array is taken as input - int ch=1; - // Choice is choosed as 1(Merge sort) - switch(ch) - { + int b[] = { 12, 26, 39, 40, 56, 68, 75, 89, 91, 98, 123 }; + // Excepted Array + int ch = 1; + // Choice is choosed as 1 (So the linked list will be sorted by Merge sort technique) + switch (ch) { case 1: - Task nm=new Task(); - Node start=null,prev=null,fresh,ptr; - for(int i=0;i=n[i]) - b[k++]=n[i++]; + + void task1(int n[], int s, int m, int e) { + int i = s, k = 0, j = m + 1; + int b[] = new int[e - s + 1]; + while (i <= m && j <= e) { + if (n[j] >= n[i]) + b[k++] = n[i++]; else - b[k++]=n[j++]; + b[k++] = n[j++]; } // Smallest number is stored after checking from both the arrays - while(i<=m) - { - b[k++]=n[i++]; + while (i <= m) { + b[k++] = n[i++]; } - while(j<=e) - { - b[k++]=n[j++]; + while (j <= e) { + b[k++] = n[j++]; } - for(int p=s;p<=e;p++) - { - a[p]=b[p-s]; + for (int p = s; p <= e; p++) { + a[p] = b[p - s]; } } // The method task and task1 is used to sort the linklist using merge sort } -class Task1 -{ + +class Task1 { public Node sort_by_insertionsort(Node head) { - if(head==null||head.next==null) + if (head == null || head.next == null) return head; - int c=count(head); - int a[]=new int[c]; - // Array of size c is created - a[0]=head.val; + int c = count(head); + int a[] = new int[c]; + // Array of size c is created + a[0] = head.val; int i; Node ptr; - for(ptr=head.next,i=1;ptr!=null;ptr=ptr.next,i++) - { - int j=i-1; - while(j>=0&&a[j]>ptr.val) - { + for (ptr = head.next, i = 1; ptr != null; ptr = ptr.next, i++) { + int j = i - 1; + while (j >= 0 && a[j] > ptr.val) { // values are stored in the array - a[j+1]=a[j]; + a[j + 1] = a[j]; j--; } - a[j+1]=ptr.val; + a[j + 1] = ptr.val; } - i=0; - for(ptr=head;ptr!=null;ptr=ptr.next) - { - ptr.val=a[i++]; - // Value is stored in the linklist after being sorted + i = 0; + for (ptr = head; ptr != null; ptr = ptr.next) { + ptr.val = a[i++]; + // Value is stored in the linklist after being sorted } return head; } - static int count(Node head) - { + + static int count(Node head) { Node ptr; - int c=0; - for(ptr=head;ptr!=null;ptr=ptr.next) - { + int c = 0; + for (ptr = head; ptr != null; ptr = ptr.next) { c++; } return c; @@ -228,73 +232,68 @@ static int count(Node head) } // The method task and task1 is used to sort the linklist using insertion sort } -class Task2 -{ + +class Task2 { static int a[]; + public Node sort_by_heapsort(Node head) { - if(head==null||head.next==null) + if (head == null || head.next == null) return head; - int c=count(head); - a=new int[c]; - // Array of size c is created - int i=0; - for(Node ptr=head;ptr!=null;ptr=ptr.next) - { - a[i++]=ptr.val; + int c = count(head); + a = new int[c]; + // Array of size c is created + int i = 0; + for (Node ptr = head; ptr != null; ptr = ptr.next) { + a[i++] = ptr.val; // values are stored in the array } - i=0; + i = 0; task(a); - for(Node ptr=head;ptr!=null;ptr=ptr.next) - { - ptr.val=a[i++]; + for (Node ptr = head; ptr != null; ptr = ptr.next) { + ptr.val = a[i++]; // Value is stored in the linklist after being sorted } return head; } - int count(Node head) - { - int c=0; + + int count(Node head) { + int c = 0; Node ptr; - for(ptr=head;ptr!=null;ptr=ptr.next) - { + for (ptr = head; ptr != null; ptr = ptr.next) { c++; } return c; // This Method is used to count number of elements/nodes present in the linklist // It will return a integer type value denoting the number of nodes present } - void task(int n[]) - { - int k=n.length; - for(int i=k/2-1;i>=0;i--) - { - task1(n,k,i); + + void task(int n[]) { + int k = n.length; + for (int i = k / 2 - 1; i >= 0; i--) { + task1(n, k, i); } - for(int i=k-1;i>0;i--) - { - int d=n[0]; - n[0]=n[i]; - n[i]=d; - task1(n,i,0); + for (int i = k - 1; i > 0; i--) { + int d = n[0]; + n[0] = n[i]; + n[i] = d; + task1(n, i, 0); // recursive calling of task1 method } } - void task1(int n[], int k, int i) - { - int p=i; - int l=2*i+1; - int r=2*i+2; - if(ln[p]) - p=l; - if(rn[p]) - p=r; - if(p!=i) - { - int d=n[p]; - n[p]=n[i]; - n[i]=d; - task1(n,k,p); + + void task1(int n[], int k, int i) { + int p = i; + int l = 2 * i + 1; + int r = 2 * i + 2; + if (l < k && n[l] > n[p]) + p = l; + if (r < k && n[r] > n[p]) + p = r; + if (p != i) { + int d = n[p]; + n[p] = n[i]; + n[i] = d; + task1(n, k, p); } } // The method task and task1 is used to sort the linklist using heap sort From f9fc81e6b89aff84d91e6af6a1aa96ef7a556680 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Wed, 19 Jan 2022 09:55:19 +0530 Subject: [PATCH 09/32] Fixes(2882) --- .../thealgorithms/sorts/LinkList_Sort.java | 60 +++++++++++------ .../others/LinkList_Sort_test.java | 64 +++++++++++++++++++ 2 files changed, 105 insertions(+), 19 deletions(-) create mode 100644 src/test/java/com/thealgorithms/others/LinkList_Sort_test.java diff --git a/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java b/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java index fc05120c1fb6..2480091621a9 100644 --- a/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java +++ b/src/main/java/com/thealgorithms/sorts/LinkList_Sort.java @@ -5,15 +5,17 @@ /** Program description - To sort the LinkList as per sorting technique */ package com.thealgorithms.sorts; - +import java.util.*; public class LinkList_Sort { - public static void main(String args[]) { - int a[] = { 89, 56, 98, 123, 26, 75, 12, 40, 39, 68, 91 }; - // Array is taken as input - int b[] = { 12, 26, 39, 40, 56, 68, 75, 89, 91, 98, 123 }; - // Excepted Array - int ch = 1; - // Choice is choosed as 1 (So the linked list will be sorted by Merge sort technique) + public static boolean isSorted(int p[] , int option) { + try (Scanner sc = new Scanner(System.in)) { + } + int a[] = p; + // Array is taken as input from test class + int b[] = p; + // array similar to a + int ch = option; + // Choice is choosed as any number from 1 to 3 (So the linked list will be sorted by Merge sort technique/Insertion sort technique/Heap sort technique) switch (ch) { case 1: Task nm = new Task(); @@ -35,11 +37,18 @@ public static void main(String args[]) { a[i++]=ptr.val; // storing the sorted values in the array } + Arrays.sort(b); + // array b is sorted and it will return true when checked with sorted list LinkList_Sort uu=new LinkList_Sort(); - System.out.println(uu.compare(a,b)); + if(uu.compare(a,b)) + { + return true; + } + else + { + return false; + } // The given array and the expected array is checked if both are same then true is displayed else false is displayed - break; - case 2: Node start1 = null, prev1 = null, fresh1, ptr1; for (int i1 = 0; i1 < a.length; i1++) { @@ -61,10 +70,16 @@ public static void main(String args[]) { // storing the sorted values in the array } LinkList_Sort uu1=new LinkList_Sort(); - System.out.println(uu1.compare(a,b)); + // array b is not sorted and it will return false when checked with sorted list + if(uu1.compare(a,b)) + { + return true; + } + else + { + return false; + } // The given array and the expected array is checked if both are same then true is displayed else false is displayed - break; - case 3: Task2 mm = new Task2(); Node start2 = null, prev2 = null, fresh2, ptr2; @@ -85,16 +100,24 @@ public static void main(String args[]) { a[i3++]=ptr2.val; // storing the sorted values in the array } + Arrays.sort(b); + // array b is sorted and it will return true when checked with sorted list LinkList_Sort uu2=new LinkList_Sort(); - System.out.println(uu2.compare(a,b)); + if(uu2.compare(a,b)) + { + return true; + } + else + { + return false; + } // The given array and the expected array is checked if both are same then true is displayed else false is displayed - break; - default: // default is used incase user puts a unauthorized value System.out.println("Wrong choice"); } // Switch case is used to call the classes as per the user requirement + return false; } boolean compare(int a[] , int b[]) { @@ -192,7 +215,6 @@ void task1(int n[], int s, int m, int e) { } // The method task and task1 is used to sort the linklist using merge sort } - class Task1 { public Node sort_by_insertionsort(Node head) { if (head == null || head.next == null) @@ -297,4 +319,4 @@ void task1(int n[], int k, int i) { } } // The method task and task1 is used to sort the linklist using heap sort -} +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/others/LinkList_Sort_test.java b/src/test/java/com/thealgorithms/others/LinkList_Sort_test.java new file mode 100644 index 000000000000..758d25328ad7 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/LinkList_Sort_test.java @@ -0,0 +1,64 @@ +package com.thealgorithms.others; +import org.junit.jupiter.api.Test; + +import com.thealgorithms.sorts.LinkList_Sort; + +import static org.junit.jupiter.api.Assertions.*; +public class LinkList_Sort_test { + @Test + void testForOneElement() + { + int a[]={56}; + assertTrue(LinkList_Sort.isSorted(a,2)); + } + + @Test + void testForTwoElements() + { + int a[]={6,4}; + assertTrue(LinkList_Sort.isSorted(a,1)); + } + + @Test + void testForThreeElements() + { + int a[]={875,253,12}; + assertTrue(LinkList_Sort.isSorted(a,3)); + } + + @Test + void testForFourElements() + { + int a[]={86,32,87,13}; + assertFalse(LinkList_Sort.isSorted(a,2)); + } + + @Test + void testForFiveElements() + { + int a[]={6,5,3,0,9}; + assertTrue(LinkList_Sort.isSorted(a,1)); + } + + + @Test + void testForSixElements() + { + int a[]={9,65,432,32,47,327}; + assertTrue(LinkList_Sort.isSorted(a,3)); + } + + @Test + void testForSevenElements() + { + int a[]={6,4,2,1,3,6,7}; + assertTrue(LinkList_Sort.isSorted(a,1)); + } + + @Test + void testForEightElements() + { + int a[]={123,234,145,764,322,367,768,34}; + assertFalse(LinkList_Sort.isSorted(a,2)); + } +} From e136a3bf6153bc86d9c8345338a708f5529296c7 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Sat, 22 Jan 2022 11:16:18 +0530 Subject: [PATCH 10/32] Fixes(#2877) --- .../dynamicprogramming/Max_Sub_Array_Sum.java | 45 +++++++++++++ .../others/Max_Sub_Array_Sum_Test.java | 63 +++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 src/main/java/com/thealgorithms/dynamicprogramming/Max_Sub_Array_Sum.java create mode 100644 src/test/java/com/thealgorithms/others/Max_Sub_Array_Sum_Test.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Max_Sub_Array_Sum.java b/src/main/java/com/thealgorithms/dynamicprogramming/Max_Sub_Array_Sum.java new file mode 100644 index 000000000000..3a1f8c53fcee --- /dev/null +++ b/src/main/java/com/thealgorithms/dynamicprogramming/Max_Sub_Array_Sum.java @@ -0,0 +1,45 @@ +/** Author : Siddhant Swarup Mallick + * Github : https://github.com/siddhant2002 + */ + +/** Program description - To find the maximum subarray sum */ + package com.thealgorithms.dynamicprogramming; + +public class Max_Sub_Array_Sum { + public static boolean max_Sum(int a[]) + { + int sum=a[0],running_sum=0; + for(int k:a) + { + running_sum=running_sum+k; + // running sum of all the indexs are stored + sum=Math.max(sum,running_sum); + // the max is stored inorder to the get the maximum sum + if(running_sum<0) + running_sum=0; + // if running sum is negative then it is initialized to zero + } + // for-each loop is used to iterate over the array and find the maximum subarray sum + return task(a,sum); + // the max_Sum method returns true if the "sub array sum" matches with the "sum of all the elements present in the array" else it returns false + } + /** + * OUTPUT : + * Input - {89,56,98,123,26,75,12,40,39,68,91} + * Output: it returns either true or false + * 1st approach Time Complexity : O(n) + * Auxiliary Space Complexity : O(1) + */ + static boolean task(int a[] , int s) + { + int p=0; + // p is created inorder to store the sum of the elements present in the array + for(int i:a) + { + p=p+i; + } + // for-each loop is initialized inorder to get the sum of all the elements of the array + return p==s; + // the task method adds all the elements of the array and checks it with the "maximum subarray sum". If the sum is equal then it returns true else it returns false + } +} diff --git a/src/test/java/com/thealgorithms/others/Max_Sub_Array_Sum_Test.java b/src/test/java/com/thealgorithms/others/Max_Sub_Array_Sum_Test.java new file mode 100644 index 000000000000..d9ecce94fce1 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/Max_Sub_Array_Sum_Test.java @@ -0,0 +1,63 @@ +package com.thealgorithms.others; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +import com.thealgorithms.dynamicprogramming.Max_Sub_Array_Sum; +public class Max_Sub_Array_Sum_Test { + @Test + void testForOneElement() + { + int a[]={-1}; + assertTrue(Max_Sub_Array_Sum.max_Sum(a)); + } + + @Test + void testForTwoElements() + { + int a[]={-2,1}; + assertFalse(Max_Sub_Array_Sum.max_Sum(a)); + } + + @Test + void testForThreeElements() + { + int a[]={5,3,12}; + assertTrue(Max_Sub_Array_Sum.max_Sum(a)); + } + + @Test + void testForFourElements() + { + int a[]={-1,-3,-7,-4}; + assertFalse(Max_Sub_Array_Sum.max_Sum(a)); + } + + @Test + void testForFiveElements() + { + int a[]={4,5,3,0,2}; + assertTrue(Max_Sub_Array_Sum.max_Sum(a)); + } + + + @Test + void testForSixElements() + { + int a[]={-43,-45,47,12,87,-13}; + assertFalse(Max_Sub_Array_Sum.max_Sum(a)); + } + + @Test + void testForSevenElements() + { + int a[]={9,8,2,23,13,6,7}; + assertTrue(Max_Sub_Array_Sum.max_Sum(a)); + } + + @Test + void testForEightElements() + { + int a[]={9,-5,-5,-2,4,5,0,1}; + assertFalse(Max_Sub_Array_Sum.max_Sum(a)); + } +} From 6220cee48526832a1e04f568261b8ae21a876124 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Fri, 11 Feb 2022 08:56:46 +0530 Subject: [PATCH 11/32] Fixes(#2877) --- .../dynamicprogramming/KadaneAlgorithm.java | 77 ++++++++----------- .../dynamicprogramming/Max_Sub_Array_Sum.java | 45 ----------- ...Sum_Test.java => KadaneAlogrithmTest.java} | 20 ++--- 3 files changed, 44 insertions(+), 98 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/dynamicprogramming/Max_Sub_Array_Sum.java rename src/test/java/com/thealgorithms/others/{Max_Sub_Array_Sum_Test.java => KadaneAlogrithmTest.java} (59%) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java b/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java index 364d16a3e9f5..8192b11d3e48 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java @@ -1,54 +1,45 @@ -package com.thealgorithms.dynamicprogramming; +/** Author : Siddhant Swarup Mallick + * Github : https://github.com/siddhant2002 + */ -import java.util.Scanner; +/** Program description - To find the maximum subarray sum */ + package com.thealgorithms.dynamicprogramming; -/** - * Program to implement Kadane’s Algorithm to calculate maximum contiguous - * subarray sum of an array Time Complexity: O(n) - * - * @author Nishita Aggarwal - */ public class KadaneAlgorithm { - - /** - * This method implements Kadane's Algorithm - * - * @param arr The input array - * @return The maximum contiguous subarray sum of the array - */ - static int largestContiguousSum(int arr[]) { - int i, len = arr.length, cursum = 0, maxsum = Integer.MIN_VALUE; - if (len == 0) // empty array + public static boolean max_Sum(int a[]) + { + int sum=a[0],running_sum=0; + for(int k:a) { - return 0; - } - for (i = 0; i < len; i++) { - cursum += arr[i]; - if (cursum > maxsum) { - maxsum = cursum; - } - if (cursum <= 0) { - cursum = 0; - } + running_sum=running_sum+k; + // running sum of all the indexs are stored + sum=Math.max(sum,running_sum); + // the max is stored inorder to the get the maximum sum + if(running_sum<0) + running_sum=0; + // if running sum is negative then it is initialized to zero } - return maxsum; + // for-each loop is used to iterate over the array and find the maximum subarray sum + return task(a,sum); + // the max_Sum method returns true if the "sub array sum" matches with the "sum of all the elements present in the array" else it returns false } - /** - * Main method - * - * @param args Command line arguments + * OUTPUT : + * Input - {89,56,98,123,26,75,12,40,39,68,91} + * Output: it returns either true or false + * 1st approach Time Complexity : O(n) + * Auxiliary Space Complexity : O(1) */ - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int n, arr[], i; - n = sc.nextInt(); - arr = new int[n]; - for (i = 0; i < n; i++) { - arr[i] = sc.nextInt(); + static boolean task(int a[] , int s) + { + int p=0; + // p is created inorder to store the sum of the elements present in the array + for(int i:a) + { + p=p+i; } - int maxContSum = largestContiguousSum(arr); - System.out.println(maxContSum); - sc.close(); + // for-each loop is initialized inorder to get the sum of all the elements of the array + return p==s; + // the task method adds all the elements of the array and checks it with the "maximum subarray sum". If the sum is equal then it returns true else it returns false } } diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Max_Sub_Array_Sum.java b/src/main/java/com/thealgorithms/dynamicprogramming/Max_Sub_Array_Sum.java deleted file mode 100644 index 3a1f8c53fcee..000000000000 --- a/src/main/java/com/thealgorithms/dynamicprogramming/Max_Sub_Array_Sum.java +++ /dev/null @@ -1,45 +0,0 @@ -/** Author : Siddhant Swarup Mallick - * Github : https://github.com/siddhant2002 - */ - -/** Program description - To find the maximum subarray sum */ - package com.thealgorithms.dynamicprogramming; - -public class Max_Sub_Array_Sum { - public static boolean max_Sum(int a[]) - { - int sum=a[0],running_sum=0; - for(int k:a) - { - running_sum=running_sum+k; - // running sum of all the indexs are stored - sum=Math.max(sum,running_sum); - // the max is stored inorder to the get the maximum sum - if(running_sum<0) - running_sum=0; - // if running sum is negative then it is initialized to zero - } - // for-each loop is used to iterate over the array and find the maximum subarray sum - return task(a,sum); - // the max_Sum method returns true if the "sub array sum" matches with the "sum of all the elements present in the array" else it returns false - } - /** - * OUTPUT : - * Input - {89,56,98,123,26,75,12,40,39,68,91} - * Output: it returns either true or false - * 1st approach Time Complexity : O(n) - * Auxiliary Space Complexity : O(1) - */ - static boolean task(int a[] , int s) - { - int p=0; - // p is created inorder to store the sum of the elements present in the array - for(int i:a) - { - p=p+i; - } - // for-each loop is initialized inorder to get the sum of all the elements of the array - return p==s; - // the task method adds all the elements of the array and checks it with the "maximum subarray sum". If the sum is equal then it returns true else it returns false - } -} diff --git a/src/test/java/com/thealgorithms/others/Max_Sub_Array_Sum_Test.java b/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java similarity index 59% rename from src/test/java/com/thealgorithms/others/Max_Sub_Array_Sum_Test.java rename to src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java index d9ecce94fce1..fb2159fe026c 100644 --- a/src/test/java/com/thealgorithms/others/Max_Sub_Array_Sum_Test.java +++ b/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java @@ -2,41 +2,41 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; -import com.thealgorithms.dynamicprogramming.Max_Sub_Array_Sum; -public class Max_Sub_Array_Sum_Test { +import com.thealgorithms.dynamicprogramming.KadaneAlgorithm; +public class KadaneAlogrithmTest { @Test void testForOneElement() { int a[]={-1}; - assertTrue(Max_Sub_Array_Sum.max_Sum(a)); + assertTrue(KadaneAlgorithm.max_Sum(a)); } @Test void testForTwoElements() { int a[]={-2,1}; - assertFalse(Max_Sub_Array_Sum.max_Sum(a)); + assertFalse(KadaneAlgorithm.max_Sum(a)); } @Test void testForThreeElements() { int a[]={5,3,12}; - assertTrue(Max_Sub_Array_Sum.max_Sum(a)); + assertTrue(KadaneAlgorithm.max_Sum(a)); } @Test void testForFourElements() { int a[]={-1,-3,-7,-4}; - assertFalse(Max_Sub_Array_Sum.max_Sum(a)); + assertFalse(KadaneAlgorithm.max_Sum(a)); } @Test void testForFiveElements() { int a[]={4,5,3,0,2}; - assertTrue(Max_Sub_Array_Sum.max_Sum(a)); + assertTrue(KadaneAlgorithm.max_Sum(a)); } @@ -44,20 +44,20 @@ void testForFiveElements() void testForSixElements() { int a[]={-43,-45,47,12,87,-13}; - assertFalse(Max_Sub_Array_Sum.max_Sum(a)); + assertFalse(KadaneAlgorithm.max_Sum(a)); } @Test void testForSevenElements() { int a[]={9,8,2,23,13,6,7}; - assertTrue(Max_Sub_Array_Sum.max_Sum(a)); + assertTrue(KadaneAlgorithm.max_Sum(a)); } @Test void testForEightElements() { int a[]={9,-5,-5,-2,4,5,0,1}; - assertFalse(Max_Sub_Array_Sum.max_Sum(a)); + assertFalse(KadaneAlgorithm.max_Sum(a)); } } From ab73827883bdbf34478e20010dcd114e960100b0 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Sat, 12 Feb 2022 21:46:32 +0530 Subject: [PATCH 12/32] Fixes(#2877) --- .../dynamicprogramming/KadaneAlgorithm.java | 18 +++--------------- .../others/KadaneAlogrithmTest.java | 16 ++++++++-------- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java b/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java index 8192b11d3e48..dcd54d5fbc4d 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java @@ -6,7 +6,7 @@ package com.thealgorithms.dynamicprogramming; public class KadaneAlgorithm { - public static boolean max_Sum(int a[]) + public static boolean max_Sum(int a[] , int predicted_answer) { int sum=a[0],running_sum=0; for(int k:a) @@ -20,8 +20,8 @@ public static boolean max_Sum(int a[]) // if running sum is negative then it is initialized to zero } // for-each loop is used to iterate over the array and find the maximum subarray sum - return task(a,sum); - // the max_Sum method returns true if the "sub array sum" matches with the "sum of all the elements present in the array" else it returns false + return sum==predicted_answer; + // It returns true if sum and predicted answer matches } /** * OUTPUT : @@ -30,16 +30,4 @@ public static boolean max_Sum(int a[]) * 1st approach Time Complexity : O(n) * Auxiliary Space Complexity : O(1) */ - static boolean task(int a[] , int s) - { - int p=0; - // p is created inorder to store the sum of the elements present in the array - for(int i:a) - { - p=p+i; - } - // for-each loop is initialized inorder to get the sum of all the elements of the array - return p==s; - // the task method adds all the elements of the array and checks it with the "maximum subarray sum". If the sum is equal then it returns true else it returns false - } } diff --git a/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java b/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java index fb2159fe026c..89560deca930 100644 --- a/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java +++ b/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java @@ -8,35 +8,35 @@ public class KadaneAlogrithmTest { void testForOneElement() { int a[]={-1}; - assertTrue(KadaneAlgorithm.max_Sum(a)); + assertTrue(KadaneAlgorithm.max_Sum(a,-1)); } @Test void testForTwoElements() { int a[]={-2,1}; - assertFalse(KadaneAlgorithm.max_Sum(a)); + assertTrue(KadaneAlgorithm.max_Sum(a,1)); } @Test void testForThreeElements() { int a[]={5,3,12}; - assertTrue(KadaneAlgorithm.max_Sum(a)); + assertTrue(KadaneAlgorithm.max_Sum(a,20)); } @Test void testForFourElements() { int a[]={-1,-3,-7,-4}; - assertFalse(KadaneAlgorithm.max_Sum(a)); + assertTrue(KadaneAlgorithm.max_Sum(a,-1)); } @Test void testForFiveElements() { int a[]={4,5,3,0,2}; - assertTrue(KadaneAlgorithm.max_Sum(a)); + assertTrue(KadaneAlgorithm.max_Sum(a,14)); } @@ -44,20 +44,20 @@ void testForFiveElements() void testForSixElements() { int a[]={-43,-45,47,12,87,-13}; - assertFalse(KadaneAlgorithm.max_Sum(a)); + assertTrue(KadaneAlgorithm.max_Sum(a,10)); } @Test void testForSevenElements() { int a[]={9,8,2,23,13,6,7}; - assertTrue(KadaneAlgorithm.max_Sum(a)); + assertTrue(KadaneAlgorithm.max_Sum(a,146)); } @Test void testForEightElements() { int a[]={9,-5,-5,-2,4,5,0,1}; - assertFalse(KadaneAlgorithm.max_Sum(a)); + assertTrue(KadaneAlgorithm.max_Sum(a,68)); } } From f903458d9a754762bdb3e3621494fbff848116d8 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Sat, 12 Feb 2022 21:51:37 +0530 Subject: [PATCH 13/32] Fixes(#2877) --- src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java b/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java index 89560deca930..db86e2392bd6 100644 --- a/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java +++ b/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java @@ -60,4 +60,4 @@ void testForEightElements() int a[]={9,-5,-5,-2,4,5,0,1}; assertTrue(KadaneAlgorithm.max_Sum(a,68)); } -} +} \ No newline at end of file From ababfa4a06c03f7a1bf02737247c95b3dc63931b Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Sun, 13 Feb 2022 21:28:35 +0530 Subject: [PATCH 14/32] Fixes(#2877) --- .../com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java b/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java index dcd54d5fbc4d..ab15ba17d968 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java @@ -22,6 +22,7 @@ public static boolean max_Sum(int a[] , int predicted_answer) // for-each loop is used to iterate over the array and find the maximum subarray sum return sum==predicted_answer; // It returns true if sum and predicted answer matches + // The predicted answer is the answer itself. So it always return true } /** * OUTPUT : @@ -30,4 +31,4 @@ public static boolean max_Sum(int a[] , int predicted_answer) * 1st approach Time Complexity : O(n) * Auxiliary Space Complexity : O(1) */ -} +} \ No newline at end of file From 255e4df7894d42f3765f6868bff05a801b7ba5ff Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Sun, 13 Feb 2022 21:34:52 +0530 Subject: [PATCH 15/32] Fixes(#2877) --- .../java/com/thealgorithms/others/KadaneAlogrithmTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java b/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java index db86e2392bd6..a4934b7ebb88 100644 --- a/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java +++ b/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java @@ -44,20 +44,20 @@ void testForFiveElements() void testForSixElements() { int a[]={-43,-45,47,12,87,-13}; - assertTrue(KadaneAlgorithm.max_Sum(a,10)); + assertTrue(KadaneAlgorithm.max_Sum(a,146)); } @Test void testForSevenElements() { int a[]={9,8,2,23,13,6,7}; - assertTrue(KadaneAlgorithm.max_Sum(a,146)); + assertTrue(KadaneAlgorithm.max_Sum(a,68)); } @Test void testForEightElements() { int a[]={9,-5,-5,-2,4,5,0,1}; - assertTrue(KadaneAlgorithm.max_Sum(a,68)); + assertTrue(KadaneAlgorithm.max_Sum(a,10)); } } \ No newline at end of file From a404bdc47152f535b602020df6bcf1e8a8489e12 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Wed, 16 Feb 2022 10:50:10 +0530 Subject: [PATCH 16/32] Fixes(#2873) --- .../dynamicprogramming/UniquePaths.java | 67 +++++++++++++++++++ .../others/UniquePathsTests.java | 56 ++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java create mode 100644 src/test/java/com/thealgorithms/others/UniquePathsTests.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java b/src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java new file mode 100644 index 000000000000..508227f098c3 --- /dev/null +++ b/src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java @@ -0,0 +1,67 @@ +/** Author : Siddhant Swarup Mallick + * Github : https://github.com/siddhant2002 + */ + + +/** + * A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). + * The robot can only move either down or right at any point in time. + * The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below). + * How many possible unique paths are there? + */ + +/** Program description - To find the number of unique paths possible */ + +package com.thealgorithms.dynamicprogramming; + +import java.util.*; + +public class UniquePaths { + public static boolean uniquePaths(int m , int n , int ans) { + int []dp = new int[n]; + Arrays.fill(dp,1); + for (int i=1; i Date: Tue, 22 Feb 2022 11:38:22 +0530 Subject: [PATCH 17/32] Fixes(#2884) --- .../dynamicprogramming/NewManShanksPrime.java | 26 +++++++++ .../others/NewManShanksPrimeTest.java | 54 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java create mode 100644 src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java new file mode 100644 index 000000000000..b158137fa8b6 --- /dev/null +++ b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java @@ -0,0 +1,26 @@ +/** Author : Siddhant Swarup Mallick + * Github : https://github.com/siddhant2002 + */ + + +/** Program description - To find the New Man Shanks Prime. */ +/** Wikipedia Link - https://en.wikipedia.org/wiki/Newman%E2%80%93Shanks%E2%80%93Williams_prime */ + +package com.thealgorithms.dynamicprogramming; + +public class NewManShanksPrime { + public static boolean newManShanksPrime(int n , int answer) + { + int a[] = new int[n+1]; + // array of n+1 size is initialized + a[0] = a[1] = 1; + // The 0th and 1st index position values are fixed. They are initialized as 1 + for(int i=2;i<=n;i++) + { + a[i]=2*a[i-1]+a[i-2]; + } + // The loop is continued till n + return a[n]==answer; + // Calculated sum is checked with the expected answer + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java b/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java new file mode 100644 index 000000000000..264a50dd3b52 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java @@ -0,0 +1,54 @@ +package com.thealgorithms.others; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +import com.thealgorithms.dynamicprogramming.NewManShanksPrime; +public class NewManShanksPrimeTest { + @Test + void testForOneElement() + { + assertTrue(NewManShanksPrime.newManShanksPrime(1,1)); + } + + @Test + void testForTwoElements() + { + assertTrue(NewManShanksPrime.newManShanksPrime(2,3)); + } + + @Test + void testForThreeElements() + { + assertTrue(NewManShanksPrime.newManShanksPrime(3,7)); + } + + @Test + void testForFourElements() + { + assertTrue(NewManShanksPrime.newManShanksPrime(4,17)); + } + + @Test + void testForFiveElements() + { + assertTrue(NewManShanksPrime.newManShanksPrime(5,41)); + } + + @Test + void testForSixElements() + { + assertTrue(NewManShanksPrime.newManShanksPrime(6,99)); + } + + @Test + void testForSevenElements() + { + assertTrue(NewManShanksPrime.newManShanksPrime(7,239)); + } + + @Test + void testForEightElements() + { + assertTrue(NewManShanksPrime.newManShanksPrime(8,577)); + } +} \ No newline at end of file From 26ac95e125f5241d8cd1f64df24f9f4af9fd0fb7 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Tue, 22 Feb 2022 20:03:08 +0530 Subject: [PATCH 18/32] Fixes(#2884) --- .../dynamicprogramming/NewManShanksPrime.java | 3 ++- .../others/NewManShanksPrimeTest.java | 16 ++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java index b158137fa8b6..39ffbfbdcb57 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java @@ -9,7 +9,7 @@ package com.thealgorithms.dynamicprogramming; public class NewManShanksPrime { - public static boolean newManShanksPrime(int n , int answer) + public static boolean nthManShanksPrime(int n , int answer) { int a[] = new int[n+1]; // array of n+1 size is initialized @@ -22,5 +22,6 @@ public static boolean newManShanksPrime(int n , int answer) // The loop is continued till n return a[n]==answer; // Calculated sum is checked with the expected answer + // here it will always return true } } \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java b/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java index 264a50dd3b52..b3ed2761dc48 100644 --- a/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java +++ b/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java @@ -7,48 +7,48 @@ public class NewManShanksPrimeTest { @Test void testForOneElement() { - assertTrue(NewManShanksPrime.newManShanksPrime(1,1)); + assertTrue(NewManShanksPrime.nthManShanksPrime(1,1)); } @Test void testForTwoElements() { - assertTrue(NewManShanksPrime.newManShanksPrime(2,3)); + assertTrue(NewManShanksPrime.nthManShanksPrime(2,3)); } @Test void testForThreeElements() { - assertTrue(NewManShanksPrime.newManShanksPrime(3,7)); + assertTrue(NewManShanksPrime.nthManShanksPrime(3,7)); } @Test void testForFourElements() { - assertTrue(NewManShanksPrime.newManShanksPrime(4,17)); + assertTrue(NewManShanksPrime.nthManShanksPrime(4,17)); } @Test void testForFiveElements() { - assertTrue(NewManShanksPrime.newManShanksPrime(5,41)); + assertTrue(NewManShanksPrime.nthManShanksPrime(5,41)); } @Test void testForSixElements() { - assertTrue(NewManShanksPrime.newManShanksPrime(6,99)); + assertTrue(NewManShanksPrime.nthManShanksPrime(6,99)); } @Test void testForSevenElements() { - assertTrue(NewManShanksPrime.newManShanksPrime(7,239)); + assertTrue(NewManShanksPrime.nthManShanksPrime(7,239)); } @Test void testForEightElements() { - assertTrue(NewManShanksPrime.newManShanksPrime(8,577)); + assertTrue(NewManShanksPrime.nthManShanksPrime(8,577)); } } \ No newline at end of file From 5ceffa0aeb5ef8d10066450f99eee1af47615618 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Tue, 22 Feb 2022 20:15:43 +0530 Subject: [PATCH 19/32] Fixes(#2884) --- .../dynamicprogramming/NewManShanksPrime.java | 4 +-- .../others/NewManShanksPrimeTest.java | 34 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java index 39ffbfbdcb57..a559b6c4a88a 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java @@ -9,7 +9,7 @@ package com.thealgorithms.dynamicprogramming; public class NewManShanksPrime { - public static boolean nthManShanksPrime(int n , int answer) + public static int nthManShanksPrime(int n) { int a[] = new int[n+1]; // array of n+1 size is initialized @@ -20,7 +20,7 @@ public static boolean nthManShanksPrime(int n , int answer) a[i]=2*a[i-1]+a[i-2]; } // The loop is continued till n - return a[n]==answer; + return a[n]; // Calculated sum is checked with the expected answer // here it will always return true } diff --git a/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java b/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java index b3ed2761dc48..e26a3e879328 100644 --- a/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java +++ b/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java @@ -5,50 +5,50 @@ import com.thealgorithms.dynamicprogramming.NewManShanksPrime; public class NewManShanksPrimeTest { @Test - void testForOneElement() + void testOne() { - assertTrue(NewManShanksPrime.nthManShanksPrime(1,1)); + System.out.println((NewManShanksPrime.nthManShanksPrime(1))); } @Test - void testForTwoElements() + void testTwo() { - assertTrue(NewManShanksPrime.nthManShanksPrime(2,3)); + System.out.println((NewManShanksPrime.nthManShanksPrime(2))); } @Test - void testForThreeElements() + void testThree() { - assertTrue(NewManShanksPrime.nthManShanksPrime(3,7)); + System.out.println((NewManShanksPrime.nthManShanksPrime(3))); } @Test - void testForFourElements() + void testFour() { - assertTrue(NewManShanksPrime.nthManShanksPrime(4,17)); + System.out.println((NewManShanksPrime.nthManShanksPrime(4))); } @Test - void testForFiveElements() + void testFive() { - assertTrue(NewManShanksPrime.nthManShanksPrime(5,41)); + System.out.println((NewManShanksPrime.nthManShanksPrime(5))); } @Test - void testForSixElements() + void testSix() { - assertTrue(NewManShanksPrime.nthManShanksPrime(6,99)); + System.out.println((NewManShanksPrime.nthManShanksPrime(6))); } - @Test - void testForSevenElements() + @Test + void testSeven() { - assertTrue(NewManShanksPrime.nthManShanksPrime(7,239)); + System.out.println((NewManShanksPrime.nthManShanksPrime(7))); } @Test - void testForEightElements() + void testEight() { - assertTrue(NewManShanksPrime.nthManShanksPrime(8,577)); + System.out.println((NewManShanksPrime.nthManShanksPrime(8))); } } \ No newline at end of file From e0c8cc906c0c2d13e36411b261f01c212a7bcafd Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Tue, 22 Feb 2022 20:17:55 +0530 Subject: [PATCH 20/32] Fixes(#2884) --- .../thealgorithms/dynamicprogramming/NewManShanksPrime.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java index a559b6c4a88a..a65b3410a8b6 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java @@ -21,7 +21,6 @@ public static int nthManShanksPrime(int n) } // The loop is continued till n return a[n]; - // Calculated sum is checked with the expected answer - // here it will always return true + // returns the nth shank prime } } \ No newline at end of file From 31826c01ca35283f8a31d9b4770777317871070e Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Fri, 25 Feb 2022 19:56:11 +0530 Subject: [PATCH 21/32] Fixes(#2884) --- .../dynamicprogramming/NewManShanksPrime.java | 6 +++--- .../others/NewManShanksPrimeTest.java | 17 +++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java index a65b3410a8b6..645f4cfa4595 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java @@ -9,7 +9,7 @@ package com.thealgorithms.dynamicprogramming; public class NewManShanksPrime { - public static int nthManShanksPrime(int n) + public static boolean nthManShanksPrime(int n , int expected_answer) { int a[] = new int[n+1]; // array of n+1 size is initialized @@ -20,7 +20,7 @@ public static int nthManShanksPrime(int n) a[i]=2*a[i-1]+a[i-2]; } // The loop is continued till n - return a[n]; - // returns the nth shank prime + return a[n]==expected_answer; + // returns true if calculated answer matches with expected answer } } \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java b/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java index e26a3e879328..134f4fbdf76f 100644 --- a/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java +++ b/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java @@ -7,48 +7,49 @@ public class NewManShanksPrimeTest { @Test void testOne() { - System.out.println((NewManShanksPrime.nthManShanksPrime(1))); + assertTrue(NewManShanksPrime.nthManShanksPrime(1,1)); } @Test void testTwo() { - System.out.println((NewManShanksPrime.nthManShanksPrime(2))); + assertTrue(NewManShanksPrime.nthManShanksPrime(2,3)); } @Test void testThree() { - System.out.println((NewManShanksPrime.nthManShanksPrime(3))); + assertTrue(NewManShanksPrime.nthManShanksPrime(3,7)); } @Test void testFour() { - System.out.println((NewManShanksPrime.nthManShanksPrime(4))); + assertTrue(NewManShanksPrime.nthManShanksPrime(4,17)); } @Test void testFive() { - System.out.println((NewManShanksPrime.nthManShanksPrime(5))); + assertTrue(NewManShanksPrime.nthManShanksPrime(5,41)); } @Test void testSix() { - System.out.println((NewManShanksPrime.nthManShanksPrime(6))); + assertTrue(NewManShanksPrime.nthManShanksPrime(6,99)); } @Test void testSeven() { - System.out.println((NewManShanksPrime.nthManShanksPrime(7))); + assertTrue(NewManShanksPrime.nthManShanksPrime(7,239)); } @Test void testEight() { - System.out.println((NewManShanksPrime.nthManShanksPrime(8))); + assertTrue(NewManShanksPrime.nthManShanksPrime(8,577)); } + } \ No newline at end of file From 972cf29748d2ddb76d561c2cd4b79097492bf0c1 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Sat, 26 Mar 2022 10:49:12 +0530 Subject: [PATCH 22/32] Fixes(#2889) --- .../dynamicprogramming/Golombsequence.java | 33 ++++++++++ .../others/GolombsequenceTest.java | 62 +++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java create mode 100644 src/test/java/com/thealgorithms/others/GolombsequenceTest.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java new file mode 100644 index 000000000000..1ef06bd8da81 --- /dev/null +++ b/src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java @@ -0,0 +1,33 @@ +/** Author : Siddhant Swarup Mallick + * Github : https://github.com/siddhant2002 + */ + + +/** + * In mathematics, the Golomb sequence is a non-decreasing integer sequence where n-th term is equal to number of times n appears in the sequence. + */ + + /** + * Wikipedia Link - https://en.wikipedia.org/wiki/Golomb_sequence + */ + + /** Program description - To find the Golomb sequence upto n */ + + package com.thealgorithms.dynamicprogramming; +import java.util.*; +public class Golombsequence { + public static boolean golombsequence(int n , int a[]) + { + int dp[] = new int[n+1]; + // array of n+1 size is created + dp[1] = 1; + // since 1st index position value is fixed so it's marked as 1 + for(int i=2;i<=n;i++) + { + dp[i] = 1 + dp[i - dp[dp[i - 1]]]; + // formula for ith golomb sequence + } + return Arrays.equals(dp , a); + // returns true if calculated answer matches with the expected answer + } +} diff --git a/src/test/java/com/thealgorithms/others/GolombsequenceTest.java b/src/test/java/com/thealgorithms/others/GolombsequenceTest.java new file mode 100644 index 000000000000..3e71b3b30dd5 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/GolombsequenceTest.java @@ -0,0 +1,62 @@ +package com.thealgorithms.others; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +import com.thealgorithms.dynamicprogramming.Golombsequence; +public class GolombsequenceTest { + @Test + void testForOneElement() + { + int a[] = {1,2,2}; + assertTrue(Golombsequence.golombsequence(3,a)); + } + + @Test + void testForTwoElements() + { + int a[] = {1,2,2,3}; + assertTrue(Golombsequence.golombsequence(4,a)); + } + + @Test + void testForThreeElements() + { + int a[] = {1,2,2,3,3}; + assertTrue(Golombsequence.golombsequence(5,a)); + } + + @Test + void testForFourElements() + { + int a[] = {1,2,2,3,3,4}; + assertTrue(Golombsequence.golombsequence(6,a)); + } + + @Test + void testForFiveElements() + { + int a[] = {1,2,2,3,3,4,4}; + assertTrue(Golombsequence.golombsequence(7,a)); + } + + @Test + void testForSixElements() + { + int a[] = {1,2,2,3,3,4,4,4}; + assertTrue(Golombsequence.golombsequence(8,a)); + } + + @Test + void testForSevenElements() + { + int a[] = {1,2,2,3,3,4,4,4,5}; + assertTrue(Golombsequence.golombsequence(9,a)); + } + + @Test + void testForEightElements() + { + int a[] = {1,2,2,3,3,4,4,4,5,5}; + assertTrue(Golombsequence.golombsequence(10,a)); + } +} \ No newline at end of file From 11575fc048ed36e0c3dffc3a509f7d655851ef54 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Sat, 26 Mar 2022 10:59:56 +0530 Subject: [PATCH 23/32] Fixes (#2889) --- .../dynamicprogramming/Golombsequence.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java index 1ef06bd8da81..1f91e419c987 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java @@ -20,14 +20,21 @@ public static boolean golombsequence(int n , int a[]) { int dp[] = new int[n+1]; // array of n+1 size is created - dp[1] = 1; + dp[0] = 1; // since 1st index position value is fixed so it's marked as 1 - for(int i=2;i<=n;i++) + for(int i=1;i Date: Wed, 30 Mar 2022 19:25:16 +0530 Subject: [PATCH 24/32] Fixes(#2889) --- .../dynamicprogramming/Golombsequence.java | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java index 1f91e419c987..f9b1d42ba2a4 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java @@ -1,36 +1,30 @@ /** Author : Siddhant Swarup Mallick * Github : https://github.com/siddhant2002 */ - - /** * In mathematics, the Golomb sequence is a non-decreasing integer sequence where n-th term is equal to number of times n appears in the sequence. */ - /** - * Wikipedia Link - https://en.wikipedia.org/wiki/Golomb_sequence - */ +/** + * Wikipedia Link - https://en.wikipedia.org/wiki/Golomb_sequence + */ + +/** Program description - To find the Golomb sequence upto n */ - /** Program description - To find the Golomb sequence upto n */ +package com.thealgorithms.dynamicprogramming; - package com.thealgorithms.dynamicprogramming; -import java.util.*; public class Golombsequence { - public static boolean golombsequence(int n , int a[]) - { - int dp[] = new int[n+1]; + public static boolean golombsequence(int n, int a[]) { + int dp[] = new int[n + 1]; // array of n+1 size is created dp[0] = 1; // since 1st index position value is fixed so it's marked as 1 - for(int i=1;i Date: Wed, 30 Mar 2022 19:37:13 +0530 Subject: [PATCH 25/32] Fixes(#2889) --- .../com/thealgorithms/dynamicprogramming/Golombsequence.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java index f9b1d42ba2a4..6686e8fcb421 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java @@ -21,11 +21,12 @@ public static boolean golombsequence(int n, int a[]) { // since 1st index position value is fixed so it's marked as 1 for (int i = 1; i < n; i++) { dp[i] = 1 + dp[i - dp[dp[i - 1]]]; - // formula for ith golomb sequence + // formula for ith golomb sequence is dp(i) = 1 + dp(i – dp(dp(i - 1))) } for (int i = 1; i < n; i++) { if (a[i - 1] != dp[i]) { return false; + // checks whether the calculated answer matches with the expected answer } } return true; From 5bb853bce7ea754ef04120cf6182ca8c6d7bf872 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Fri, 1 Apr 2022 00:03:43 +0530 Subject: [PATCH 26/32] Fixes(#2889) --- ...sequence.java => CountFriendsPairing.java} | 4 ++-- ...Test.java => CountFriendsPairingTest.java} | 20 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) rename src/main/java/com/thealgorithms/dynamicprogramming/{Golombsequence.java => CountFriendsPairing.java} (91%) rename src/test/java/com/thealgorithms/others/{GolombsequenceTest.java => CountFriendsPairingTest.java} (55%) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/CountFriendsPairing.java similarity index 91% rename from src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java rename to src/main/java/com/thealgorithms/dynamicprogramming/CountFriendsPairing.java index 6686e8fcb421..ad7e7d30a7da 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/Golombsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/CountFriendsPairing.java @@ -13,8 +13,8 @@ package com.thealgorithms.dynamicprogramming; -public class Golombsequence { - public static boolean golombsequence(int n, int a[]) { +public class CountFriendsPairing { + public static boolean CountFriendsPairing(int n, int a[]) { int dp[] = new int[n + 1]; // array of n+1 size is created dp[0] = 1; diff --git a/src/test/java/com/thealgorithms/others/GolombsequenceTest.java b/src/test/java/com/thealgorithms/others/CountFriendsPairingTest.java similarity index 55% rename from src/test/java/com/thealgorithms/others/GolombsequenceTest.java rename to src/test/java/com/thealgorithms/others/CountFriendsPairingTest.java index 3e71b3b30dd5..833ad79341f2 100644 --- a/src/test/java/com/thealgorithms/others/GolombsequenceTest.java +++ b/src/test/java/com/thealgorithms/others/CountFriendsPairingTest.java @@ -2,61 +2,61 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; -import com.thealgorithms.dynamicprogramming.Golombsequence; -public class GolombsequenceTest { +import com.thealgorithms.dynamicprogramming.CountFriendsPairing; +public class CountFriendsPairingTest { @Test void testForOneElement() { int a[] = {1,2,2}; - assertTrue(Golombsequence.golombsequence(3,a)); + assertTrue(CountFriendsPairing.CountFriendsPairing(3,a)); } @Test void testForTwoElements() { int a[] = {1,2,2,3}; - assertTrue(Golombsequence.golombsequence(4,a)); + assertTrue(CountFriendsPairing.CountFriendsPairing(4,a)); } @Test void testForThreeElements() { int a[] = {1,2,2,3,3}; - assertTrue(Golombsequence.golombsequence(5,a)); + assertTrue(CountFriendsPairing.CountFriendsPairing(5,a)); } @Test void testForFourElements() { int a[] = {1,2,2,3,3,4}; - assertTrue(Golombsequence.golombsequence(6,a)); + assertTrue(CountFriendsPairing.CountFriendsPairing(6,a)); } @Test void testForFiveElements() { int a[] = {1,2,2,3,3,4,4}; - assertTrue(Golombsequence.golombsequence(7,a)); + assertTrue(CountFriendsPairing.CountFriendsPairing(7,a)); } @Test void testForSixElements() { int a[] = {1,2,2,3,3,4,4,4}; - assertTrue(Golombsequence.golombsequence(8,a)); + assertTrue(CountFriendsPairing.CountFriendsPairing(8,a)); } @Test void testForSevenElements() { int a[] = {1,2,2,3,3,4,4,4,5}; - assertTrue(Golombsequence.golombsequence(9,a)); + assertTrue(CountFriendsPairing.CountFriendsPairing(9,a)); } @Test void testForEightElements() { int a[] = {1,2,2,3,3,4,4,4,5,5}; - assertTrue(Golombsequence.golombsequence(10,a)); + assertTrue(CountFriendsPairing.CountFriendsPairing(10,a)); } } \ No newline at end of file From ecee9f4d8bd419a501ee6371835c2e02fac1033f Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Fri, 1 Apr 2022 00:13:23 +0530 Subject: [PATCH 27/32] Fixes(#2889) --- .../thealgorithms/dynamicprogramming/CountFriendsPairing.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/CountFriendsPairing.java b/src/main/java/com/thealgorithms/dynamicprogramming/CountFriendsPairing.java index ad7e7d30a7da..a5d069391dfb 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/CountFriendsPairing.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/CountFriendsPairing.java @@ -14,7 +14,7 @@ package com.thealgorithms.dynamicprogramming; public class CountFriendsPairing { - public static boolean CountFriendsPairing(int n, int a[]) { + public static boolean countFriendsPairing(int n, int a[]) { int dp[] = new int[n + 1]; // array of n+1 size is created dp[0] = 1; From d69880e46769687d23e51c880ec97b122cbcd751 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Fri, 1 Apr 2022 00:17:07 +0530 Subject: [PATCH 28/32] Fixes(#2889) --- .../others/CountFriendsPairingTest.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/CountFriendsPairingTest.java b/src/test/java/com/thealgorithms/others/CountFriendsPairingTest.java index 833ad79341f2..1e1d2111240e 100644 --- a/src/test/java/com/thealgorithms/others/CountFriendsPairingTest.java +++ b/src/test/java/com/thealgorithms/others/CountFriendsPairingTest.java @@ -8,55 +8,55 @@ public class CountFriendsPairingTest { void testForOneElement() { int a[] = {1,2,2}; - assertTrue(CountFriendsPairing.CountFriendsPairing(3,a)); + assertTrue(CountFriendsPairing.countFriendsPairing(3,a)); } @Test void testForTwoElements() { int a[] = {1,2,2,3}; - assertTrue(CountFriendsPairing.CountFriendsPairing(4,a)); + assertTrue(CountFriendsPairing.countFriendsPairing(4,a)); } @Test void testForThreeElements() { int a[] = {1,2,2,3,3}; - assertTrue(CountFriendsPairing.CountFriendsPairing(5,a)); + assertTrue(CountFriendsPairing.countFriendsPairing(5,a)); } @Test void testForFourElements() { int a[] = {1,2,2,3,3,4}; - assertTrue(CountFriendsPairing.CountFriendsPairing(6,a)); + assertTrue(CountFriendsPairing.countFriendsPairing(6,a)); } @Test void testForFiveElements() { int a[] = {1,2,2,3,3,4,4}; - assertTrue(CountFriendsPairing.CountFriendsPairing(7,a)); + assertTrue(CountFriendsPairing.countFriendsPairing(7,a)); } @Test void testForSixElements() { int a[] = {1,2,2,3,3,4,4,4}; - assertTrue(CountFriendsPairing.CountFriendsPairing(8,a)); + assertTrue(CountFriendsPairing.countFriendsPairing(8,a)); } @Test void testForSevenElements() { int a[] = {1,2,2,3,3,4,4,4,5}; - assertTrue(CountFriendsPairing.CountFriendsPairing(9,a)); + assertTrue(CountFriendsPairing.countFriendsPairing(9,a)); } @Test void testForEightElements() { int a[] = {1,2,2,3,3,4,4,4,5,5}; - assertTrue(CountFriendsPairing.CountFriendsPairing(10,a)); + assertTrue(CountFriendsPairing.countFriendsPairing(10,a)); } } \ No newline at end of file From 72e750d2ce525288e94ba341ffbf889e695eac86 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Wed, 4 May 2022 21:52:22 +0530 Subject: [PATCH 29/32] Fixes (#2957) --- .../stacks/CalculateMaxOfMin.java | 43 ++++++++++++++ .../others/CalculateMaxOfMinTest.java | 57 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/stacks/CalculateMaxOfMin.java create mode 100644 src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/CalculateMaxOfMin.java b/src/main/java/com/thealgorithms/datastructures/stacks/CalculateMaxOfMin.java new file mode 100644 index 000000000000..b5f5213bec51 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/stacks/CalculateMaxOfMin.java @@ -0,0 +1,43 @@ +/** Author : Siddhant Swarup Mallick + * Github : https://github.com/siddhant2002 + */ + +/** Program description - Given an integer array. The task is to find the maximum of the minimum of the array */ +package com.thealgorithms.datastructures.stacks; + +import java.util.*; + +public class CalculateMaxOfMin { + public static boolean calculateMaxOfMin(int[] a, int b[], int n) { + int[] ans = new int[n]; + int[] arr2 = Arrays.copyOf(a, a.length); + Arrays.sort(arr2); + int maxNum = arr2[arr2.length - 1]; + ans[0] = maxNum; + int index = 1; + while (index != ans.length) { + int[] minimums = new int[n - index]; + for (int i = 0; i < n - index; i++) { + int[] windowArray = Arrays.copyOfRange(a, i, i + index + 1); + Arrays.sort(windowArray); + int minNum = windowArray[0]; + minimums[i] = minNum; + } + Arrays.sort(minimums); + ans[index] = minimums[minimums.length - 1]; + index += 1; + } + for (int i = 0; i < b.length; i++) { + if (b[i] != ans[i]) { + return false; + // checks whether the calculated answer matches with the expected answer + } + } + return true; + // returns true if calculated answer matches with the expected answer + } +} +/** + * Given an integer array. The task is to find the maximum of the minimum of the + * given array + */ \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java b/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java new file mode 100644 index 000000000000..d6c001f9fb7d --- /dev/null +++ b/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java @@ -0,0 +1,57 @@ +package com.thealgorithms.others; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +import com.thealgorithms.datastructures.stacks.CalculateMaxOfMin; + +public class CalculateMaxOfMinTest { + @Test + void testForOneElement() { + int a[] = { 10, 20, 30, 50, 10, 70, 30 }; + int b[] = { 70, 30, 20, 10, 10, 10, 10 }; + assertTrue(CalculateMaxOfMin.calculateMaxOfMin(a, b, a.length)); + } + + @Test + void testForTwoElements() { + int a[] = { 5, 3, 2, 6, 3, 2, 6 }; + int b[] = { 6, 3, 2, 2, 2, 2, 2 }; + assertTrue(CalculateMaxOfMin.calculateMaxOfMin(a, b, a.length)); + } + + @Test + void testForThreeElements() { + int a[] = { 10, 10, 10, 10, 10, 10, 10 }; + int b[] = { 10, 10, 10, 10, 10, 10, 10 }; + assertTrue(CalculateMaxOfMin.calculateMaxOfMin(a, b, a.length)); + } + + @Test + void testForFourElements() { + int a[] = { 70, 60, 50, 40, 30, 20 }; + int b[] = { 70, 60, 50, 40, 30, 20 }; + assertTrue(CalculateMaxOfMin.calculateMaxOfMin(a, b, a.length)); + } + + @Test + void testForFiveElements() { + int a[] = { 50 }; + int b[] = { 50 }; + assertTrue(CalculateMaxOfMin.calculateMaxOfMin(a, b, a.length)); + } + + @Test + void testForSixElements() { + int a[] = { 1, 4, 7, 9, 2, 4, 6 }; + int b[] = { 9, 7, 4, 2, 2, 2, 1 }; + assertTrue(CalculateMaxOfMin.calculateMaxOfMin(a, b, a.length)); + } + + @Test + void testForSevenElements() { + int a[] = { -1, -5, -7, -9, -12, -14 }; + int b[] = { -1, -5, -7, -9, -12, -14 }; + assertTrue(CalculateMaxOfMin.calculateMaxOfMin(a, b, a.length)); + } +} From 13aa7fa404c929a598208e521cbe53f58910f1f8 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Wed, 4 May 2022 23:19:06 +0530 Subject: [PATCH 30/32] Fixes (#2957) --- .../java/com/thealgorithms/others/CalculateMaxOfMinTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java b/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java index d6c001f9fb7d..3648748d5325 100644 --- a/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java +++ b/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java @@ -1,8 +1,6 @@ package com.thealgorithms.others; - import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; - import com.thealgorithms.datastructures.stacks.CalculateMaxOfMin; public class CalculateMaxOfMinTest { From 156cbdc659b8675bfcc06330694ef486aa8ff644 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Sun, 8 May 2022 12:51:53 +0530 Subject: [PATCH 31/32] Fixes ('2957) --- .../stacks/CalculateMaxOfMin.java | 14 +++------ .../others/CalculateMaxOfMinTest.java | 29 ++++++++++--------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/CalculateMaxOfMin.java b/src/main/java/com/thealgorithms/datastructures/stacks/CalculateMaxOfMin.java index b5f5213bec51..7af1192ad581 100644 --- a/src/main/java/com/thealgorithms/datastructures/stacks/CalculateMaxOfMin.java +++ b/src/main/java/com/thealgorithms/datastructures/stacks/CalculateMaxOfMin.java @@ -8,9 +8,10 @@ import java.util.*; public class CalculateMaxOfMin { - public static boolean calculateMaxOfMin(int[] a, int b[], int n) { + public static int calculateMaxOfMin(int[] a) { + int n = a.length; int[] ans = new int[n]; - int[] arr2 = Arrays.copyOf(a, a.length); + int[] arr2 = Arrays.copyOf(a, n); Arrays.sort(arr2); int maxNum = arr2[arr2.length - 1]; ans[0] = maxNum; @@ -27,14 +28,7 @@ public static boolean calculateMaxOfMin(int[] a, int b[], int n) { ans[index] = minimums[minimums.length - 1]; index += 1; } - for (int i = 0; i < b.length; i++) { - if (b[i] != ans[i]) { - return false; - // checks whether the calculated answer matches with the expected answer - } - } - return true; - // returns true if calculated answer matches with the expected answer + return ans[0]; } } /** diff --git a/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java b/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java index 3648748d5325..367c7e5b3ef6 100644 --- a/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java +++ b/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java @@ -1,4 +1,5 @@ package com.thealgorithms.others; + import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; import com.thealgorithms.datastructures.stacks.CalculateMaxOfMin; @@ -7,49 +8,49 @@ public class CalculateMaxOfMinTest { @Test void testForOneElement() { int a[] = { 10, 20, 30, 50, 10, 70, 30 }; - int b[] = { 70, 30, 20, 10, 10, 10, 10 }; - assertTrue(CalculateMaxOfMin.calculateMaxOfMin(a, b, a.length)); + int k = CalculateMaxOfMin.calculateMaxOfMin(a); + assertTrue(k == 70); } @Test void testForTwoElements() { int a[] = { 5, 3, 2, 6, 3, 2, 6 }; - int b[] = { 6, 3, 2, 2, 2, 2, 2 }; - assertTrue(CalculateMaxOfMin.calculateMaxOfMin(a, b, a.length)); + int k = CalculateMaxOfMin.calculateMaxOfMin(a); + assertTrue(k == 6); } @Test void testForThreeElements() { int a[] = { 10, 10, 10, 10, 10, 10, 10 }; - int b[] = { 10, 10, 10, 10, 10, 10, 10 }; - assertTrue(CalculateMaxOfMin.calculateMaxOfMin(a, b, a.length)); + int k = CalculateMaxOfMin.calculateMaxOfMin(a); + assertTrue(k == 10); } @Test void testForFourElements() { int a[] = { 70, 60, 50, 40, 30, 20 }; - int b[] = { 70, 60, 50, 40, 30, 20 }; - assertTrue(CalculateMaxOfMin.calculateMaxOfMin(a, b, a.length)); + int k = CalculateMaxOfMin.calculateMaxOfMin(a); + assertTrue(k == 70); } @Test void testForFiveElements() { int a[] = { 50 }; - int b[] = { 50 }; - assertTrue(CalculateMaxOfMin.calculateMaxOfMin(a, b, a.length)); + int k = CalculateMaxOfMin.calculateMaxOfMin(a); + assertTrue(k == 50); } @Test void testForSixElements() { int a[] = { 1, 4, 7, 9, 2, 4, 6 }; - int b[] = { 9, 7, 4, 2, 2, 2, 1 }; - assertTrue(CalculateMaxOfMin.calculateMaxOfMin(a, b, a.length)); + int k = CalculateMaxOfMin.calculateMaxOfMin(a); + assertTrue(k == 9); } @Test void testForSevenElements() { int a[] = { -1, -5, -7, -9, -12, -14 }; - int b[] = { -1, -5, -7, -9, -12, -14 }; - assertTrue(CalculateMaxOfMin.calculateMaxOfMin(a, b, a.length)); + int k = CalculateMaxOfMin.calculateMaxOfMin(a); + assertTrue(k == -1); } } From 9418ff235bbe8c752f9e69989ea1fcc988b52a1a Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Sun, 8 May 2022 12:53:09 +0530 Subject: [PATCH 32/32] Fixes(2957) --- .../java/com/thealgorithms/others/CalculateMaxOfMinTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java b/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java index 367c7e5b3ef6..c5a02822605e 100644 --- a/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java +++ b/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java @@ -53,4 +53,4 @@ void testForSevenElements() { int k = CalculateMaxOfMin.calculateMaxOfMin(a); assertTrue(k == -1); } -} +} \ No newline at end of file