Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add files via upload #113

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Solution.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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));
}
}
40 changes: 40 additions & 0 deletions Solution2.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
92 changes: 92 additions & 0 deletions reverseKgrp.cpp
Original file line number Diff line number Diff line change
@@ -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
78 changes: 78 additions & 0 deletions reverseKgrp2.cpp
Original file line number Diff line number Diff line change
@@ -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).
*/