From 2d879f00a99f7d1323e4003214ba189389b8791a Mon Sep 17 00:00:00 2001 From: Tanu Jain <111673705+TanuJain2002@users.noreply.github.com> Date: Tue, 4 Oct 2022 04:30:55 -0700 Subject: [PATCH 1/3] Add files via upload I have provided two different approaches for Happy Number in java.Kindly provide me the hacktoberfest label --- Solution.java | 40 ++++++++++++++++++++++++++++++++++++++++ Solution2.java | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 Solution.java create mode 100644 Solution2.java diff --git a/Solution.java b/Solution.java new file mode 100644 index 0000000..37a54b7 --- /dev/null +++ b/Solution.java @@ -0,0 +1,40 @@ +// The happy number can be defined as a number which will yield 1 when it is replaced by the sum of the square of its digits repeatedly +//Approach 1: +//Runtime: 2ms +//Memory usage: 33MB +import java.util.*; +class Solution { + + //Using HashMaps to store the evaluated values to detect repetitions. + + //FUNC TO RETURN WHETHER NUMBER IS HAPPY OR NOT + public static boolean isHappy(int n) { + HashSet set = new HashSet(); + set.add(n); + int sum = n; + while(sum!=1){ + sum = getDigitSqrSum(sum); + if(set.contains(sum)){ + return false; + } else { + set.add(sum); + } + } + return true; + } + //FUNCTION TO ADD SQUARE OF EACH DIGIT OF THE NUMBER + + private static int getDigitSqrSum(int n){ + int sum = 0; + while(n!=0){ + int d = n%10; + sum+=(d*d); + n=n/10; + } + return sum; + } + public static void main(String[] arg){ + int number = 19; + System.out.println(isHappy(number)); + } +} diff --git a/Solution2.java b/Solution2.java new file mode 100644 index 0000000..580682d --- /dev/null +++ b/Solution2.java @@ -0,0 +1,40 @@ +// The happy number can be defined as a number which will yield 1 when it is replaced by the sum of the square of its digits repeatedly + +//Approach 2: +//Runtime: 1ms +//Memory usage: 32.6MB + +import java.util.*; +class Solution2 { + //Using Floyd Cycle detection algorithm + + //FUNC TO RETURN WHETHER NUMBER IS HAPPY OR NOT + public static boolean isHappy(int n) { + int slow, fast; + slow = fast = n; + do { + slow = digitSquareSum(slow); + fast = digitSquareSum(fast); + fast = digitSquareSum(fast); + } while(slow != fast); + if (slow == 1){ + return true; + } + else return false; + } + //FUNCTION TO ADD SQUARE OF EACH DIGIT OF THE NUMBER + private static int digitSquareSum(int n) { + int sum = 0, tmp; + while (n>0) { + tmp = n % 10; + sum += tmp * tmp; //ADDING SQUARE OF EACH DIGIT + n /= 10; + } + return sum; + + } + public static void main(String[] arg){ + int number = 19; + System.out.println(isHappy(number)); + } +} \ No newline at end of file From e5a9c9edfdc025f3d18c9903dc33029562fa753b Mon Sep 17 00:00:00 2001 From: Tanu Jain <111673705+TanuJain2002@users.noreply.github.com> Date: Tue, 4 Oct 2022 05:01:11 -0700 Subject: [PATCH 2/3] Add files via upload Iterative and Recursive aprroaches for reversing kth group in a linked list. Kinndly provide me the Hacktoberfest label --- reverseKgrp.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++++++ reverseKgrp2.cpp | 78 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 reverseKgrp.cpp create mode 100644 reverseKgrp2.cpp diff --git a/reverseKgrp.cpp b/reverseKgrp.cpp new file mode 100644 index 0000000..27f1611 --- /dev/null +++ b/reverseKgrp.cpp @@ -0,0 +1,92 @@ +//ITERATIVE APPROACH TO REVERSE A GROUP OF K NUMBERS IN A LINKED LIST + +//psuedocode +// Suppose that we are given a list: +// 1->2->3->4->5->NULL, k = 2 +// Initialization: dummy -> 1, prev = dummy +// Current List: dummy->1->2->3->4->5->NULL +// First Iteration: let it point to 1 +// increase count for 1, becomes 1 +// let it point to 2 +// increase count for 1, becomes 2 +// there are at least 2 nodes left +// set nextPrev to 1 +// reverse from 1 to 2 +// let 1 point to NULL +// let 2 point to 1 +// prev2 is 2 +// cur is 3 +// let prev (dummy) point to prev2 (2) +// let nextPrev (1) point to cur (3) +// move prev (dummy) to nextPrev (1) +// Current List: dummy->2->1->3->4->5->NULL +// Second Iteration: let it point to 3 +// increase count for 1, becomes 1 +// let it point to 4 +// increase count for 1, becomes 2 +// there are at least 2 nodes left +// set nextPrev to 3 +// reverse from 3 to 4 +// let 3 point to NULL +// let 4 point to 3 +// prev2 is 4 +// cur is 5 +// let prev (1) point to prev2 (4) +// let nextPrev (3) point to cur (5) +// move prev (1) to nextPrev (3) +// Current List: dummy->2->1->4->3->5->NULL +// Second Iteration: let it point to 5 +// increment count for 1, becomes 1 +// let it point to NULL +// there is only 1 node left +// halt + +// return dummy.next = 2 + +//FUNC TO REVERSE AND PRINT K GROUPS IN A LINKEDLIST +ListNode* reverseKGroup(ListNode* head, int k) { + if (!head || !head->next || k <= 1) { + return head; + } + + ListNode dummy(0), *prev = &dummy; + prev->next = head; + + while (true) { + ListNode* it = prev->next; + int count = 0; + while (it) { + ++count; + if (count == k) { + break; + } + it = it->next; + } + if (count < k) { + break; + } + + ListNode* nextPrev = prev->next; + + ListNode* prev2 = NULL, *cur = prev->next, *next = NULL; + for (int i = 0; i < k; ++i) { + next = cur->next; + cur->next = prev2; + prev2 = cur; + cur = next; + } + + prev->next = prev2; + nextPrev->next = cur; + + prev = nextPrev; + } + + return dummy.next; +} + +/*Time Complexity: O(n) — Basically, we need to iterate the list twice overall. First, in each iteration, we have to first look ahead to check if there are k nodes left. If there are, we need reverse them, which is another linear scan. So the total time complexity is still linear. + +Space Complexity: O(1) — The only main data structure we have been using in this implementation are still pointers, so the space cost is constant. +*/ +//SOURCE : https://medium.com \ No newline at end of file diff --git a/reverseKgrp2.cpp b/reverseKgrp2.cpp new file mode 100644 index 0000000..67039e4 --- /dev/null +++ b/reverseKgrp2.cpp @@ -0,0 +1,78 @@ +//RECURSIVE APPROACH TO REVERSE A GROUP OF K NUMBERS IN A LINKED LIST +//Recursive Rules: 1) Recursively reverse the list beginning from k+1-th node. 2) Reverse the k nodes starting from current head, and connect the new reversed sub-list to the post result. + +//PSEUDOCODE + +// Suppose that we have an input list: +// 1->2->3->4->5->NULL, k = 2 +// Current List: 1->2->3->4->5->NULL +// In Main Function: call reverseKGroup(1) +// Current List: 1->2->3->4->5->NULL +// In reverseKGroup(1): let it point to 1 +// increase count for 1, becomes 1 +// let it point to 2 +// increase count for 1, becomes 2 +// there are at least 2 nodes +// call reverseKGroup(3) +// Current List: 1->2->3->4->5->NULL +// In reverseKGroup(3): let it point to 3 +// increase count for 1, becomes 1 +// let it point to 4 +// increase count for 1, becomes 2 +// there are at least 2 nodes +// call reverseKGroup(5) +// Current List: 1->2->3->4->5->NULL +// In reverseKGroup(5): let it point to 5 +// increase count for 1, becomes 1 +// there is only 1 node left +// hit base case +// return 5 +// Current List: 1->2->3->4->5->NULL +// In reverseKGroup(3): get 5 from reverseKGroup(5) + +// reverse from 3 to 4 +// let 3 point to 5 +// return 4 +// Current List: 1->2->4->3->5->NULL +// In reverseKGroup(1): get 4 from reverseKGroup(3) +// reverse from 1 to 2 + +// let 1 point to 3 +// return 2 +// Current List: 2->1->4->3->5->NULL +// In Main Function: get 2 + + +//FUNCTION TO REVERSE AND OUTPUT THE LIST WITH K GROUP REVERSED +ListNode* reverseKGroup(ListNode* head, int k) { + ListNode* it = head; + int count = 0; + while (it) { + ++count; + if (count == k) { + break; + } + it = it->next; + } + if (count < k) + return head; + + ListNode* post = reverseKGroup(it->next, k); + + ListNode* prev = NULL, *cur = head, *next = NULL; + for (int i = 0; i < k; ++i) { + next = cur->next; + cur->next = prev; + prev = cur; + cur = next; + } + + head->next = post; + + return prev; +} + +/*Time Complexity: O(n) — in each function call, we need to first check if there are k nodes left starting from input head, and if there is, we have to reverse them. So two linear scans are still necessary. + +Space Complexity: O(n / k) — With recursion, we need to take the cost of call stacks into consideration. For every k nodes, we are supposed to recursively call the function for once. So the total number of recursive calls is n / k. Within each function, we are still using a couple of pointers only in this algorithm, so they are constant cost. Thus, the total space complexity is O(n / k). +*/ \ No newline at end of file From 2aa147281cc386bd011cf1d6637a2af0cedb7876 Mon Sep 17 00:00:00 2001 From: Tanu Jain <111673705+TanuJain2002@users.noreply.github.com> Date: Tue, 4 Oct 2022 05:08:16 -0700 Subject: [PATCH 3/3] Add files via upload These are the iterative and recursive approaches for reversing the kth group in a linked list . @ MilindGupta-Creator sir , Kindly provide me the hackoberfest label