Indian Institute of Technology, Delhi 2016
My Stats
EXP gained
1925
Level
5 (Champion)
Community stats
Discussions
0
0
Know more
0
Total problems solved
0
Easy
0
Moderate
0
Hard
0
Ninja

Current streak:

0 days

Longest streak:

0 days

Less

More

Discussions
Top Solutions | Longest Mountain Subarray
Interview problems

Here are the top solutions of POTD Challenge.

Rank 1 (HUNTERMARCHI) - C++ (g++ 5.4) Solution

`int longestMountain(int *arr, int n) {<break><break>  int ans =0;<break><break>  for(int i=1;i<n-1;i++){<break><break>      int res=0;<break><break>      if(arr[i]>arr[i-1] && arr[i]>arr[i+1]){<break><break>          int l,r;<break><break>          l=r=i;<break><break>          while(l>0 && arr[l]>arr[l-1]){<break><break>              l--;<break><break>          }<break><break>          while(r<n-1 && arr[r]>arr[r+1]){<break><break>              r++;<break><break>          }<break><break>           res = r-l+1;<break><break>      }<break><break>     ans=max(ans,res);<break><break>  }<break><break>  return ans;<break>}`

Rank 2 (krishna_kumar) - C++ (g++ 5.4) Solution

`/*<break>    Time Complexity: O(N)<break>    Space Complexity: O(1)<break><break>    where 'N' is the size of the given array.<break>*/<break><break>int longestMountain(int *arr, int n)<break>{<break>    if(n < 3)<break>    {<break>    	return 0;<break>    }<break><break>    int start = -1, end = -1;<break>    int ans = 0;<break><break>    for (int i = 0; i < n - 1; ++i)<break>    {<break>    	if(arr[i+1] > arr[i])<break>    	{<break>    		if(end != -1) {<break>    			end = -1;<break>    			start = -1;<break>    		}<break><break>    		if(start == -1) {<break>	    		start = i;<break>	    	}<break>    	}<break>    	else<break>    	{<break>    		if(arr[i+1] < arr[i])<break>    		{<break>    			if(start != -1) {<break>    				end = i + 1;<break>    			}<break><break>    			if(end != -1 && start != -1) {<break>    				ans = max(ans, end - start + 1);<break>    			}<break>    		}<break>    		else<break>    		{<break>    			start = -1;<break>    			end = -1;<break>    		}<break>    	}<break>    }<break><break>    if(end != -1 && start != -1)<break>    {<break>    	ans = max(ans, end - start + 1);<break>    }<break><break>    return ans;<break>}`

Rank 3 (Gurpreet Singh) - C++ (g++ 5.4) Solution

`#include <bits/stdc++.h> <break>int longestMountain(int *arr, int n){<break>    if(n<3)<break>        return 0;<break>    int ans=0;<break>    for(int i=1;i<=n-2;){<break>        if(arr[i]>arr[i-1] and arr[i]>arr[i+1]){<break>            int c=0;<break>            int j=i;<break>            while(arr[j]>arr[j-1] and j>0)<break>                c++,j--;<break>            while(arr[i]>arr[i+1] and i<=n-2)<break>                c++,i++;<break>            ans=max(ans,c);<break>        }<break>        else<break>            i++;<break>    }<break>    if(ans>0)<break>    return ans+1;<break>    <break>    return ans;<break>}`

Rank 4 (CodeMystique) - C++ (g++ 5.4) Solution

`#include <bits/stdc++.h><break><break>int longestMountain(int *arr, int n)<break><break>{<break><break>int ans = 0;<break><break>for(int i=1;i<n-1;i++){<break><break>if(arr[i]>arr[i-1] && arr[i]>arr[i+1]){<break><break>int j = i;<break><break>while(j>0){<break><break>if(arr[j]<=arr[j-1])break;<break><break>j--;<break><break>}<break><break>int k = i;<break><break>while(k<n-1){<break><break>if(arr[k]<=arr[k+1])break;<break><break>k++;<break><break>}<break><break>ans = max(ans,k-j+1);<break><break>}<break><break>}<break><break>return ans;<break><break>}`

Rank 5 (ansh_shah) - C++ (g++ 5.4) Solution

`#include <bits/stdc++.h> <break>int longestMountain(int *arr, int n)<break>{<break>    int ans = 0;<break>    for(int i=1;i<n-1;i++){<break>        if(arr[i]>arr[i-1] && arr[i]>arr[i+1]){<break>            int j = i;<break>            while(j>0){<break>                if(arr[j]<=arr[j-1])break;<break>                j--;<break>            }<break>            int k = i;<break>            while(k<n-1){<break>                if(arr[k]<=arr[k+1])break;<break>                k++;<break>            }<break>            ans = max(ans,k-j+1);<break>        }<break>    }<break><break>    return ans;<break>}`

Explore and share your solution to get the valuable insights from the coding community.

Solve today's POTD Challenge
Codestudio
Published On 10-Dec-2023
58 views
0 replies
Top Solutions | Longest Mountain Subarray
Interview problems

Here are the top solutions of POTD Challenge.

Rank 1 (HUNTERMARCHI) - C++ (g++ 5.4) Solution

`int longestMountain(int *arr, int n) {<break><break>  int ans =0;<break><break>  for(int i=1;i<n-1;i++){<break><break>      int res=0;<break><break>      if(arr[i]>arr[i-1] && arr[i]>arr[i+1]){<break><break>          int l,r;<break><break>          l=r=i;<break><break>          while(l>0 && arr[l]>arr[l-1]){<break><break>              l--;<break><break>          }<break><break>          while(r<n-1 && arr[r]>arr[r+1]){<break><break>              r++;<break><break>          }<break><break>           res = r-l+1;<break><break>      }<break><break>     ans=max(ans,res);<break><break>  }<break><break>  return ans;<break>}`

Rank 2 (krishna_kumar) - C++ (g++ 5.4) Solution

`/*<break>    Time Complexity: O(N)<break>    Space Complexity: O(1)<break><break>    where 'N' is the size of the given array.<break>*/<break><break>int longestMountain(int *arr, int n)<break>{<break>    if(n < 3)<break>    {<break>    	return 0;<break>    }<break><break>    int start = -1, end = -1;<break>    int ans = 0;<break><break>    for (int i = 0; i < n - 1; ++i)<break>    {<break>    	if(arr[i+1] > arr[i])<break>    	{<break>    		if(end != -1) {<break>    			end = -1;<break>    			start = -1;<break>    		}<break><break>    		if(start == -1) {<break>	    		start = i;<break>	    	}<break>    	}<break>    	else<break>    	{<break>    		if(arr[i+1] < arr[i])<break>    		{<break>    			if(start != -1) {<break>    				end = i + 1;<break>    			}<break><break>    			if(end != -1 && start != -1) {<break>    				ans = max(ans, end - start + 1);<break>    			}<break>    		}<break>    		else<break>    		{<break>    			start = -1;<break>    			end = -1;<break>    		}<break>    	}<break>    }<break><break>    if(end != -1 && start != -1)<break>    {<break>    	ans = max(ans, end - start + 1);<break>    }<break><break>    return ans;<break>}`

Rank 3 (Gurpreet Singh) - C++ (g++ 5.4) Solution

`#include <bits/stdc++.h> <break>int longestMountain(int *arr, int n){<break>    if(n<3)<break>        return 0;<break>    int ans=0;<break>    for(int i=1;i<=n-2;){<break>        if(arr[i]>arr[i-1] and arr[i]>arr[i+1]){<break>            int c=0;<break>            int j=i;<break>            while(arr[j]>arr[j-1] and j>0)<break>                c++,j--;<break>            while(arr[i]>arr[i+1] and i<=n-2)<break>                c++,i++;<break>            ans=max(ans,c);<break>        }<break>        else<break>            i++;<break>    }<break>    if(ans>0)<break>    return ans+1;<break>    <break>    return ans;<break>}`

Rank 4 (CodeMystique) - C++ (g++ 5.4) Solution

`#include <bits/stdc++.h><break><break>int longestMountain(int *arr, int n)<break><break>{<break><break>int ans = 0;<break><break>for(int i=1;i<n-1;i++){<break><break>if(arr[i]>arr[i-1] && arr[i]>arr[i+1]){<break><break>int j = i;<break><break>while(j>0){<break><break>if(arr[j]<=arr[j-1])break;<break><break>j--;<break><break>}<break><break>int k = i;<break><break>while(k<n-1){<break><break>if(arr[k]<=arr[k+1])break;<break><break>k++;<break><break>}<break><break>ans = max(ans,k-j+1);<break><break>}<break><break>}<break><break>return ans;<break><break>}`

Rank 5 (ansh_shah) - C++ (g++ 5.4) Solution

`#include <bits/stdc++.h> <break>int longestMountain(int *arr, int n)<break>{<break>    int ans = 0;<break>    for(int i=1;i<n-1;i++){<break>        if(arr[i]>arr[i-1] && arr[i]>arr[i+1]){<break>            int j = i;<break>            while(j>0){<break>                if(arr[j]<=arr[j-1])break;<break>                j--;<break>            }<break>            int k = i;<break>            while(k<n-1){<break>                if(arr[k]<=arr[k+1])break;<break>                k++;<break>            }<break>            ans = max(ans,k-j+1);<break>        }<break>    }<break><break>    return ans;<break>}`

Explore and share your solution to get the valuable insights from the coding community.

Solve today's POTD Challenge
Codestudio
Published On 10-Dec-2023
58 views
0 replies
Top Solutions | Add First and Second Reversed Half
Interview problems

Here are the top solutions of POTD Challenge.

Rank 1 (krishna_kumar) - C++ (g++ 5.4) Solution

```#include <bits/stdc++.h>
<break>/****************************************************************
<break>
<break>    Following is the class structure of the Node class:
<break>
<break>        class Node
<break>        {
<break>        public:
<break>            int data;
<break>            Node *next;
<break>            Node(int data)
<break>            {
<break>                this->data = data;
<break>                this->next = NULL;
<break>            }
<break>        };
<break>
<break>*****************************************************************/
<break>
<break>  Node *prev = NULL;
<break>  while (curr != NULL) {
<break>    Node *next = curr->next;
<break>    curr->next = prev;
<break>    prev = curr;
<break>    curr = next;
<break>  }
<break>  return prev;
<break>}
<break>  Node *n1 = new Node(data);
<break>  if (head == NULL) {
<break>    tail = n1;
<break>  } else {
<break>    tail->next = n1;
<break>    tail = n1;
<break>  }
<break>}
<break>  Node *tail = NULL;
<break>  int carry = 0;
<break>  while (ptr1 != NULL || ptr2 != NULL || carry != 0) {
<break>    int first = 0, second = 0;
<break>    if (ptr1 != NULL) {
<break>      first = ptr1->data;
<break>    }
<break>    if (ptr2 != NULL) {
<break>      second = ptr2->data;
<break>    }
<break>    int sum = carry + second + first;
<break>    int digit = sum % 10;
<break>    carry = sum / 10;
<break>    if (ptr1 != NULL) {
<break>      ptr1 = ptr1->next;
<break>    }
<break>    if (ptr2 != NULL) {
<break>      ptr2 = ptr2->next;
<break>    }
<break>  }
<break>
<break>}
<break>
<break>  }
<break>  int count = 0;
<break>  while (ptr != NULL) {
<break>    count++;
<break>    ptr = ptr->next;
<break>  }
<break>  Node *tail1 = NULL;
<break>  Node *tail2 = NULL;
<break>
<break>  int mid = count / 2;
<break>  if (count & 1) {
<break>    for (int i = 0; i <= mid; i++) {
<break>      ptr = ptr->next;
<break>    }
<break>  } else {
<break>    for (int i = 0; i < mid; i++) {
<break>      ptr = ptr->next;
<break>    }
<break>  }
<break>  while (ptr != NULL) {
<break>    ptr = ptr->next;
<break>  }
<break>  }
<break>}```

Rank 2 (HUNTERMARCHI) - C++ (g++ 5.4) Solution

```#include <bits/stdc++.h>
<break>
<break>  while (fast and fast->next and fast->next->next) {
<break>    slow = slow->next;
<break>    fast = fast->next->next;
<break>  }
<break>
<break>  Node *temp = slow->next;
<break>  slow->next = NULL;
<break>  return temp;
<break>}
<break>
<break>  Node *p = head, *nxt, *prev = NULL;
<break>  while (p) {
<break>    nxt = p->next;
<break>    p->next = prev;
<break>
<break>    prev = p;
<break>    p = nxt;
<break>  }
<break>
<break>  return prev;
<break>}
<break>
<break>    return NULL;
<break>
<break>
<break>  first = reverseLL(first);
<break>
<break>  Node *dummy = new Node(0);
<break>  Node *temp = dummy;
<break>  int carry = 0;
<break>
<break>  while (first or second or carry) {
<break>    int sum = 0;
<break>
<break>    if (first) {
<break>      sum += first->data;
<break>      first = first->next;
<break>    }
<break>    if (second) {
<break>      sum += second->data;
<break>      second = second->next;
<break>    }
<break>
<break>    if (carry)
<break>      sum += carry;
<break>
<break>    carry = sum / 10;
<break>    temp->next = new Node(sum % 10);
<break>    temp = temp->next;
<break>  }
<break>
<break>  Node *p = dummy->next;
<break>  p = reverseLL(dummy->next);
<break>
<break>  while (p and p->data == 0)
<break>    p = p->next;
<break>  return p ? p : new Node(0);
<break>}```

Rank 3 (Coder_Boy) - C++ (g++ 11) Solution

```#include <bits/stdc++.h>
<break>using namespace std;
<break>
<break>/*struct Node {
<break>    int data;
<break>    Node* next;
<break>    Node(int val) : data(val), next(nullptr) {}
<break>};*/
<break>
<break>    while (fast && fast->next && fast->next->next) {
<break>        slow = slow->next;
<break>        fast = fast->next->next;
<break>    }
<break>    Node* temp = slow->next;
<break>    slow->next = nullptr;
<break>    return temp;
<break>}
<break>
<break>    Node* p = head, * nxt, * prev = nullptr;
<break>    while (p) {
<break>        nxt = p->next;
<break>        p->next = prev;
<break>        prev = p;
<break>        p = nxt;
<break>    }
<break>    return prev;
<break>}
<break>
<break>        return nullptr;
<break>
<break>    first = reverseLL(first);
<break>
<break>    Node* dummy = new Node(0);
<break>    Node* temp = dummy;
<break>    int carry = 0;
<break>
<break>    while (first || second || carry) {
<break>        int sum = 0;
<break>
<break>        if (first) {
<break>            sum += first->data;
<break>            first = first->next;
<break>        }
<break>        if (second) {
<break>            sum += second->data;
<break>            second = second->next;
<break>        }
<break>
<break>        if (carry)
<break>            sum += carry;
<break>
<break>        carry = sum / 10;
<break>        temp->next = new Node(sum % 10);
<break>        temp = temp->next;
<break>    }
<break>
<break>    Node* result = reverseLL(dummy->next);
<break>
<break>    while (result && result->data == 0)
<break>        result = result->next;
<break>
<break>    return result ? result : new Node(0);
<break>}
<break>```

Rank 4 (sagarundirwade) - Java (SE 1.8) Solution

`/*<break>    Time Complexity: O(N)<break>    Space Complexity: O(1)<break><break>    where 'N' is the number of nodes in the linkedlist.<break>*/<break><break>public class Solution {<break><break>    public static LinkedListNode addFirstAndReversedSecondHalf(LinkedListNode head) {<break>        if(head == null) {<break>            return null;<break>        }<break><break>        LinkedListNode prev = null;<break><break>        LinkedListNode slow = head, fast = head;<break>        while(fast != null && fast.next != null) {<break>            fast = fast.next.next;<break><break>            LinkedListNode temp = slow.next;<break>            slow.next = prev;<break>            prev = slow;<break>            slow = temp;<break>        }<break><break>        if(fast != null) {<break>            LinkedListNode temp = slow.next;<break>            slow.next = prev;<break>            prev = slow;<break>            slow = temp;<break>        }<break><break>        // Now slow will point to the head for second list.<break>        LinkedListNode head1 = prev, head2 = slow;<break>        <break><break>        LinkedListNode sumHead = addTwoLinkedList(head1, head2);<break><break>        while(sumHead != null && sumHead.data == 0) {<break>            sumHead = sumHead.next;<break>        }<break><break>        // If all digits in the sum string are zero, return only "0".<break>        if(sumHead == null) {<break>            return new LinkedListNode(0);<break>        }<break><break>        return sumHead;<break>    }<break><break>    private static LinkedListNode addTwoLinkedList(LinkedListNode head1, LinkedListNode head2) {<break>        LinkedListNode prev = null;<break>        int carry = 0;<break><break>        while(head1 != null || head2 != null) {<break>            int sum = carry + head1.data;<break>            if(head2 != null) {<break>                sum += head2.data;<break>            }<break><break>            if(sum > 9) {<break>                sum -= 10;<break>                carry = 1;<break>            }<break>            else {<break>                carry = 0;<break>            }<break><break>            head1.data = sum;<break>            LinkedListNode temp = head1.next;<break>            head1.next = prev;<break>            prev = head1;<break>            head1 = temp;<break><break>            if(head2 != null) {<break>                head2 = head2.next;<break>            }<break>        }<break><break>        if(carry == 1) {<break>            head1 = new LinkedListNode(1);<break>            head1.next = prev;<break>        }<break>        else {<break>            head1 = prev;<break>        }<break><break>        return head1;<break>    }<break><break>}`

Rank 5 (HpCoder100) - C++ (g++ 5.4) Solution

```#include <bits/stdc++.h>
<break>
<break>  while (fast and fast->next and fast->next->next) {
<break>    slow = slow->next;
<break>    fast = fast->next->next;
<break>  }
<break>
<break>  Node *temp = slow->next;
<break>  slow->next = NULL;
<break>  return temp;
<break>}
<break>
<break>  Node *p = head, *nxt, *prev = NULL;
<break>  while (p) {
<break>    nxt = p->next;
<break>    p->next = prev;
<break>
<break>    prev = p;
<break>    p = nxt;
<break>  }
<break>
<break>  return prev;
<break>}
<break>
<break>    return NULL;
<break>
<break>
<break>  first = reverseLL(first);
<break>
<break>  Node *dummy = new Node(0);
<break>  Node *temp = dummy;
<break>  int carry = 0;
<break>
<break>  while (first or second or carry) {
<break>    int sum = 0;
<break>
<break>    if (first) {
<break>      sum += first->data;
<break>      first = first->next;
<break>    }
<break>    if (second) {
<break>      sum += second->data;
<break>      second = second->next;
<break>    }
<break>
<break>    if (carry)
<break>      sum += carry;
<break>
<break>    carry = sum / 10;
<break>    temp->next = new Node(sum % 10);
<break>    temp = temp->next;
<break>  }
<break>
<break>  Node *p = dummy->next;
<break>  p = reverseLL(dummy->next);
<break>
<break>  while (p and p->data == 0)
<break>    p = p->next;
<break>  return p ? p : new Node(0);
<break>}```

Explore and share your solution to get the valuable insights from the coding community.

Solve today's POTD Challenge
Codestudio
Published On 10-Dec-2023
17 views
0 replies
Top Solutions | Add First and Second Reversed Half
Interview problems

Here are the top solutions of POTD Challenge.

Rank 1 (krishna_kumar) - C++ (g++ 5.4) Solution

```#include <bits/stdc++.h>
<break>/****************************************************************
<break>
<break>    Following is the class structure of the Node class:
<break>
<break>        class Node
<break>        {
<break>        public:
<break>            int data;
<break>            Node *next;
<break>            Node(int data)
<break>            {
<break>                this->data = data;
<break>                this->next = NULL;
<break>            }
<break>        };
<break>
<break>*****************************************************************/
<break>
<break>  Node *prev = NULL;
<break>  while (curr != NULL) {
<break>    Node *next = curr->next;
<break>    curr->next = prev;
<break>    prev = curr;
<break>    curr = next;
<break>  }
<break>  return prev;
<break>}
<break>  Node *n1 = new Node(data);
<break>  if (head == NULL) {
<break>    tail = n1;
<break>  } else {
<break>    tail->next = n1;
<break>    tail = n1;
<break>  }
<break>}
<break>  Node *tail = NULL;
<break>  int carry = 0;
<break>  while (ptr1 != NULL || ptr2 != NULL || carry != 0) {
<break>    int first = 0, second = 0;
<break>    if (ptr1 != NULL) {
<break>      first = ptr1->data;
<break>    }
<break>    if (ptr2 != NULL) {
<break>      second = ptr2->data;
<break>    }
<break>    int sum = carry + second + first;
<break>    int digit = sum % 10;
<break>    carry = sum / 10;
<break>    if (ptr1 != NULL) {
<break>      ptr1 = ptr1->next;
<break>    }
<break>    if (ptr2 != NULL) {
<break>      ptr2 = ptr2->next;
<break>    }
<break>  }
<break>
<break>}
<break>
<break>  }
<break>  int count = 0;
<break>  while (ptr != NULL) {
<break>    count++;
<break>    ptr = ptr->next;
<break>  }
<break>  Node *tail1 = NULL;
<break>  Node *tail2 = NULL;
<break>
<break>  int mid = count / 2;
<break>  if (count & 1) {
<break>    for (int i = 0; i <= mid; i++) {
<break>      ptr = ptr->next;
<break>    }
<break>  } else {
<break>    for (int i = 0; i < mid; i++) {
<break>      ptr = ptr->next;
<break>    }
<break>  }
<break>  while (ptr != NULL) {
<break>    ptr = ptr->next;
<break>  }
<break>  }
<break>}```

Rank 2 (HUNTERMARCHI) - C++ (g++ 5.4) Solution

```#include <bits/stdc++.h>
<break>
<break>  while (fast and fast->next and fast->next->next) {
<break>    slow = slow->next;
<break>    fast = fast->next->next;
<break>  }
<break>
<break>  Node *temp = slow->next;
<break>  slow->next = NULL;
<break>  return temp;
<break>}
<break>
<break>  Node *p = head, *nxt, *prev = NULL;
<break>  while (p) {
<break>    nxt = p->next;
<break>    p->next = prev;
<break>
<break>    prev = p;
<break>    p = nxt;
<break>  }
<break>
<break>  return prev;
<break>}
<break>
<break>    return NULL;
<break>
<break>
<break>  first = reverseLL(first);
<break>
<break>  Node *dummy = new Node(0);
<break>  Node *temp = dummy;
<break>  int carry = 0;
<break>
<break>  while (first or second or carry) {
<break>    int sum = 0;
<break>
<break>    if (first) {
<break>      sum += first->data;
<break>      first = first->next;
<break>    }
<break>    if (second) {
<break>      sum += second->data;
<break>      second = second->next;
<break>    }
<break>
<break>    if (carry)
<break>      sum += carry;
<break>
<break>    carry = sum / 10;
<break>    temp->next = new Node(sum % 10);
<break>    temp = temp->next;
<break>  }
<break>
<break>  Node *p = dummy->next;
<break>  p = reverseLL(dummy->next);
<break>
<break>  while (p and p->data == 0)
<break>    p = p->next;
<break>  return p ? p : new Node(0);
<break>}```

Rank 3 (Coder_Boy) - C++ (g++ 11) Solution

```#include <bits/stdc++.h>
<break>using namespace std;
<break>
<break>/*struct Node {
<break>    int data;
<break>    Node* next;
<break>    Node(int val) : data(val), next(nullptr) {}
<break>};*/
<break>
<break>    while (fast && fast->next && fast->next->next) {
<break>        slow = slow->next;
<break>        fast = fast->next->next;
<break>    }
<break>    Node* temp = slow->next;
<break>    slow->next = nullptr;
<break>    return temp;
<break>}
<break>
<break>    Node* p = head, * nxt, * prev = nullptr;
<break>    while (p) {
<break>        nxt = p->next;
<break>        p->next = prev;
<break>        prev = p;
<break>        p = nxt;
<break>    }
<break>    return prev;
<break>}
<break>
<break>        return nullptr;
<break>
<break>    first = reverseLL(first);
<break>
<break>    Node* dummy = new Node(0);
<break>    Node* temp = dummy;
<break>    int carry = 0;
<break>
<break>    while (first || second || carry) {
<break>        int sum = 0;
<break>
<break>        if (first) {
<break>            sum += first->data;
<break>            first = first->next;
<break>        }
<break>        if (second) {
<break>            sum += second->data;
<break>            second = second->next;
<break>        }
<break>
<break>        if (carry)
<break>            sum += carry;
<break>
<break>        carry = sum / 10;
<break>        temp->next = new Node(sum % 10);
<break>        temp = temp->next;
<break>    }
<break>
<break>    Node* result = reverseLL(dummy->next);
<break>
<break>    while (result && result->data == 0)
<break>        result = result->next;
<break>
<break>    return result ? result : new Node(0);
<break>}
<break>```

Rank 4 (sagarundirwade) - Java (SE 1.8) Solution

`/*<break>    Time Complexity: O(N)<break>    Space Complexity: O(1)<break><break>    where 'N' is the number of nodes in the linkedlist.<break>*/<break><break>public class Solution {<break><break>    public static LinkedListNode addFirstAndReversedSecondHalf(LinkedListNode head) {<break>        if(head == null) {<break>            return null;<break>        }<break><break>        LinkedListNode prev = null;<break><break>        LinkedListNode slow = head, fast = head;<break>        while(fast != null && fast.next != null) {<break>            fast = fast.next.next;<break><break>            LinkedListNode temp = slow.next;<break>            slow.next = prev;<break>            prev = slow;<break>            slow = temp;<break>        }<break><break>        if(fast != null) {<break>            LinkedListNode temp = slow.next;<break>            slow.next = prev;<break>            prev = slow;<break>            slow = temp;<break>        }<break><break>        // Now slow will point to the head for second list.<break>        LinkedListNode head1 = prev, head2 = slow;<break>        <break><break>        LinkedListNode sumHead = addTwoLinkedList(head1, head2);<break><break>        while(sumHead != null && sumHead.data == 0) {<break>            sumHead = sumHead.next;<break>        }<break><break>        // If all digits in the sum string are zero, return only "0".<break>        if(sumHead == null) {<break>            return new LinkedListNode(0);<break>        }<break><break>        return sumHead;<break>    }<break><break>    private static LinkedListNode addTwoLinkedList(LinkedListNode head1, LinkedListNode head2) {<break>        LinkedListNode prev = null;<break>        int carry = 0;<break><break>        while(head1 != null || head2 != null) {<break>            int sum = carry + head1.data;<break>            if(head2 != null) {<break>                sum += head2.data;<break>            }<break><break>            if(sum > 9) {<break>                sum -= 10;<break>                carry = 1;<break>            }<break>            else {<break>                carry = 0;<break>            }<break><break>            head1.data = sum;<break>            LinkedListNode temp = head1.next;<break>            head1.next = prev;<break>            prev = head1;<break>            head1 = temp;<break><break>            if(head2 != null) {<break>                head2 = head2.next;<break>            }<break>        }<break><break>        if(carry == 1) {<break>            head1 = new LinkedListNode(1);<break>            head1.next = prev;<break>        }<break>        else {<break>            head1 = prev;<break>        }<break><break>        return head1;<break>    }<break><break>}`

Rank 5 (HpCoder100) - C++ (g++ 5.4) Solution

```#include <bits/stdc++.h>
<break>
<break>  while (fast and fast->next and fast->next->next) {
<break>    slow = slow->next;
<break>    fast = fast->next->next;
<break>  }
<break>
<break>  Node *temp = slow->next;
<break>  slow->next = NULL;
<break>  return temp;
<break>}
<break>
<break>  Node *p = head, *nxt, *prev = NULL;
<break>  while (p) {
<break>    nxt = p->next;
<break>    p->next = prev;
<break>
<break>    prev = p;
<break>    p = nxt;
<break>  }
<break>
<break>  return prev;
<break>}
<break>
<break>    return NULL;
<break>
<break>
<break>  first = reverseLL(first);
<break>
<break>  Node *dummy = new Node(0);
<break>  Node *temp = dummy;
<break>  int carry = 0;
<break>
<break>  while (first or second or carry) {
<break>    int sum = 0;
<break>
<break>    if (first) {
<break>      sum += first->data;
<break>      first = first->next;
<break>    }
<break>    if (second) {
<break>      sum += second->data;
<break>      second = second->next;
<break>    }
<break>
<break>    if (carry)
<break>      sum += carry;
<break>
<break>    carry = sum / 10;
<break>    temp->next = new Node(sum % 10);
<break>    temp = temp->next;
<break>  }
<break>
<break>  Node *p = dummy->next;
<break>  p = reverseLL(dummy->next);
<break>
<break>  while (p and p->data == 0)
<break>    p = p->next;
<break>  return p ? p : new Node(0);
<break>}```

Explore and share your solution to get the valuable insights from the coding community.

Solve today's POTD Challenge
Codestudio
Published On 10-Dec-2023
17 views
0 replies
Top Solutions | Path With Minimum Effort
Interview problems

Here are the top solutions of POTD Challenge.

Rank 1 (hitkarmiglani05) - Python (3.10) Solution

```from typing import List
<break>from collections import deque
<break>import heapq
<break>
<break>
<break>def minimumTimeTakingPath(heights: List[List[int]]) -> int:
<break>    n=len(heights)
<break>    m=len(heights[0])
<break>    distances=[[float("inf") for i in range(m)] for j in range(n)]
<break>    distances[0][0]=0
<break>    heap=[(0,0)]
<break>    while len(heap)>0:
<break>        temp_i,temp_j=heapq.heappop(heap)
<break>        direction=[(0,1),(1,0),(0,-1),(-1,0)]
<break>        for dir_i,dir_j in direction:
<break>            if (0<=temp_i+dir_i<n) and (0<=temp_j+dir_j<m):
<break>                new_val=abs(heights[temp_i][temp_j]-heights[temp_i+dir_i][temp_j+dir_j])
<break>                new_val=max(new_val,distances[temp_i][temp_j])
<break>                old_val = distances[temp_i+dir_i][temp_j+dir_j]
<break>                if new_val<old_val:
<break>                    distances[temp_i+dir_i][temp_j+dir_j]=new_val
<break>                    heapq.heappush(heap,(temp_i+dir_i,temp_j+dir_j))
<break>    return distances[-1][-1]```

Rank 2 (CodeMystique) - C++ (g++ 5.4) Solution

```#include <bits/stdc++.h>
<break>
<break>int minimumTimeTakingPath(vector<vector<int>>& heights){
<break>
<break>int m = heights.size();
<break>
<break>int n = heights[0].size();
<break>
<break>vector<vector<int>> cost(m, vector<int>(n, 1e9));
<break>
<break>cost[0][0] = 0;
<break>
<break>vector<vector<int>> directions;
<break>
<break>directions.push_back({0, 1});
<break>
<break>directions.push_back({0, -1});
<break>
<break>directions.push_back({1, 0});
<break>
<break>directions.push_back({-1, 0});
<break>
<break>priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;
<break>
<break>q.push({0, 0, 0});
<break>
<break>int endX = m - 1;
<break>
<break>int endY = n - 1;
<break>
<break>while(q.size()){
<break>
<break>vector<int> temp = q.top();
<break>
<break>q.pop();
<break>
<break>int time = temp[0];
<break>
<break>int i = temp[1];
<break>
<break>int k = temp[2];
<break>
<break>if(i == endX && k == endY){
<break>
<break>return time;
<break>
<break>}
<break>
<break>for(int j = 0; j < 4; j++){
<break>
<break>int x = directions[j][0];
<break>
<break>int y = directions[j][1];
<break>
<break>int nxt_i = x + i;
<break>
<break>int nxt_k = k + y;
<break>
<break>if(0 <= nxt_i && nxt_i < m && 0 <= nxt_k && nxt_k < n){
<break>
<break>int nxt_time = max(time, abs(heights[nxt_i][nxt_k] - heights[i][k]));
<break>
<break>if(nxt_time < cost[nxt_i][nxt_k]){
<break>
<break>cost[nxt_i][nxt_k] = nxt_time;
<break>
<break>q.push({nxt_time, nxt_i, nxt_k});
<break>
<break>}
<break>
<break>}
<break>
<break>}
<break>
<break>}
<break>
<break>}```

Rank 3 (krishna_kumar) - C++ (g++ 11) Solution

```/*
<break>    Time Complexity: O(m * n * log(m * n))
<break>    Space Complexity: O(m * n)
<break>
<break>    Where 'm' is the number of rows and 'n' represents the number of columns.
<break>*/
<break>
<break>#include <queue>
<break>
<break>int minimumTimeTakingPath(vector<vector<int>> &heights)
<break>{
<break>
<break>    int m = heights.size();
<break>    int n = heights[0].size();
<break>
<break>    vector<vector<int>> cost(m, vector<int>(n, 1e9));
<break>
<break>    cost[0][0] = 0;
<break>
<break>    // Defining directions.
<break>    vector<vector<int>> directions;
<break>    directions.push_back({0, 1});
<break>    directions.push_back({0, -1});
<break>    directions.push_back({1, 0});
<break>    directions.push_back({-1, 0});
<break>
<break>    // Creating priority_queue.
<break>    priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;
<break>    q.push({0, 0, 0});
<break>    int endX = m - 1;
<break>    int endY = n - 1;
<break>
<break>    while (q.size())
<break>    {
<break>        vector<int> temp = q.top();
<break>        q.pop();
<break>        int time = temp[0];
<break>        int i = temp[1];
<break>        int k = temp[2];
<break>
<break>        if (i == endX && k == endY)
<break>        {
<break>            return time;
<break>        }
<break>
<break>        for (int j = 0; j < 4; j++)
<break>        {
<break>            int x = directions[j][0];
<break>            int y = directions[j][1];
<break>
<break>            int nxt_i = x + i;
<break>            int nxt_k = k + y;
<break>
<break>            if (0 <= nxt_i && nxt_i < m && 0 <= nxt_k && nxt_k < n)
<break>            {
<break>                int nxt_time = max(time, abs(heights[nxt_i][nxt_k] - heights[i][k]));
<break>
<break>                if (nxt_time < cost[nxt_i][nxt_k])
<break>                {
<break>                    // Updating cost.
<break>                    cost[nxt_i][nxt_k] = nxt_time;
<break>                    q.push({nxt_time, nxt_i, nxt_k});
<break>                }
<break>            }
<break>        }
<break>    }
<break>}```

Rank 4 (Murari) - C++ (g++ 5.4) Solution

```<break>vector<vector<int>> del = {{0,1},{0,-1},{-1,0},{1,0}};
<break>
<break>int minimumTimeTakingPath(vector<vector<int>> &heights)
<break>{
<break>    int n = heights.size(),m = heights[0].size();
<break>    vector<vector<int>> dp(n,vector<int>(m,1e9));
<break>    priority_queue<pair<int,pair<int,int>> > pq;
<break>    pq.push({0,{0,0}});
<break>    dp[0][0] = 0;
<break>    while(!pq.empty()) {
<break>        int diff = -pq.top().first,x = pq.top().second.first,y = pq.top().second.second;
<break>        pq.pop();
<break>
<break>        for(int i = 0; i < 4 ;i++){
<break>            int nx = del[i][0]+x,ny = del[i][1]+y;
<break>            if(nx>=n or ny>=m or ny<0 or nx<0)  continue;
<break>            int ndiff = max(diff, abs(heights[nx][ny]-heights[x][y]));
<break>
<break>            if(ndiff<dp[nx][ny]){
<break>                dp[nx][ny] = ndiff;
<break>                pq.push({-ndiff,{nx,ny}});
<break>            }
<break>        }
<break>    }
<break>    return dp[n-1][m-1];
<break>}```

Rank 5 (AkashSingh3031) - C++ (g++ 5.4) Solution

```# include <bits/stdc++.h>
<break>
<break>int minimumTimeTakingPath(vector<vector<int>>& heights){
<break>
<break>    int m = heights.size();
<break>    int n = heights[0].size();
<break>
<break>    vector<vector<int>> cost(m, vector<int>(n, 1e9));
<break>
<break>    cost[0][0] = 0;
<break>
<break>    vector<vector<int>> directions;
<break>    directions.push_back({0, 1});
<break>    directions.push_back({0, -1});
<break>    directions.push_back({1, 0});
<break>    directions.push_back({-1, 0});
<break>
<break>    priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;
<break>    q.push({0, 0, 0});
<break>    int endX = m - 1;
<break>    int endY = n - 1;
<break>    while(q.size()){
<break>
<break>        vector<int> temp = q.top();
<break>        q.pop();
<break>        int time = temp[0];
<break>        int i = temp[1];
<break>        int k = temp[2];
<break>
<break>        if(i == endX && k == endY){
<break>            return time;
<break>        }
<break>
<break>        for(int j = 0; j < 4; j++){
<break>            int x = directions[j][0];
<break>            int y = directions[j][1];
<break>
<break>            int nxt_i = x + i;
<break>            int nxt_k = k + y;
<break>
<break>            if(0 <= nxt_i && nxt_i < m && 0 <= nxt_k && nxt_k < n){
<break>                int nxt_time = max(time, abs(heights[nxt_i][nxt_k] - heights[i][k]));
<break>
<break>                if(nxt_time < cost[nxt_i][nxt_k]){
<break>                    cost[nxt_i][nxt_k] = nxt_time;
<break>                    q.push({nxt_time, nxt_i, nxt_k});
<break>                }
<break>            }
<break>        }
<break>    }
<break>
<break>}```

Explore and share your solution to get the valuable insights from the coding community.

Solve today's POTD Challenge
Codestudio
Published On 10-Dec-2023
7 views
0 replies
Top Solutions | Path With Minimum Effort
Interview problems

Here are the top solutions of POTD Challenge.

Rank 1 (hitkarmiglani05) - Python (3.10) Solution

```from typing import List
<break>from collections import deque
<break>import heapq
<break>
<break>
<break>def minimumTimeTakingPath(heights: List[List[int]]) -> int:
<break>    n=len(heights)
<break>    m=len(heights[0])
<break>    distances=[[float("inf") for i in range(m)] for j in range(n)]
<break>    distances[0][0]=0
<break>    heap=[(0,0)]
<break>    while len(heap)>0:
<break>        temp_i,temp_j=heapq.heappop(heap)
<break>        direction=[(0,1),(1,0),(0,-1),(-1,0)]
<break>        for dir_i,dir_j in direction:
<break>            if (0<=temp_i+dir_i<n) and (0<=temp_j+dir_j<m):
<break>                new_val=abs(heights[temp_i][temp_j]-heights[temp_i+dir_i][temp_j+dir_j])
<break>                new_val=max(new_val,distances[temp_i][temp_j])
<break>                old_val = distances[temp_i+dir_i][temp_j+dir_j]
<break>                if new_val<old_val:
<break>                    distances[temp_i+dir_i][temp_j+dir_j]=new_val
<break>                    heapq.heappush(heap,(temp_i+dir_i,temp_j+dir_j))
<break>    return distances[-1][-1]```

Rank 2 (CodeMystique) - C++ (g++ 5.4) Solution

```#include <bits/stdc++.h>
<break>
<break>int minimumTimeTakingPath(vector<vector<int>>& heights){
<break>
<break>int m = heights.size();
<break>
<break>int n = heights[0].size();
<break>
<break>vector<vector<int>> cost(m, vector<int>(n, 1e9));
<break>
<break>cost[0][0] = 0;
<break>
<break>vector<vector<int>> directions;
<break>
<break>directions.push_back({0, 1});
<break>
<break>directions.push_back({0, -1});
<break>
<break>directions.push_back({1, 0});
<break>
<break>directions.push_back({-1, 0});
<break>
<break>priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;
<break>
<break>q.push({0, 0, 0});
<break>
<break>int endX = m - 1;
<break>
<break>int endY = n - 1;
<break>
<break>while(q.size()){
<break>
<break>vector<int> temp = q.top();
<break>
<break>q.pop();
<break>
<break>int time = temp[0];
<break>
<break>int i = temp[1];
<break>
<break>int k = temp[2];
<break>
<break>if(i == endX && k == endY){
<break>
<break>return time;
<break>
<break>}
<break>
<break>for(int j = 0; j < 4; j++){
<break>
<break>int x = directions[j][0];
<break>
<break>int y = directions[j][1];
<break>
<break>int nxt_i = x + i;
<break>
<break>int nxt_k = k + y;
<break>
<break>if(0 <= nxt_i && nxt_i < m && 0 <= nxt_k && nxt_k < n){
<break>
<break>int nxt_time = max(time, abs(heights[nxt_i][nxt_k] - heights[i][k]));
<break>
<break>if(nxt_time < cost[nxt_i][nxt_k]){
<break>
<break>cost[nxt_i][nxt_k] = nxt_time;
<break>
<break>q.push({nxt_time, nxt_i, nxt_k});
<break>
<break>}
<break>
<break>}
<break>
<break>}
<break>
<break>}
<break>
<break>}```

Rank 3 (krishna_kumar) - C++ (g++ 11) Solution

```/*
<break>    Time Complexity: O(m * n * log(m * n))
<break>    Space Complexity: O(m * n)
<break>
<break>    Where 'm' is the number of rows and 'n' represents the number of columns.
<break>*/
<break>
<break>#include <queue>
<break>
<break>int minimumTimeTakingPath(vector<vector<int>> &heights)
<break>{
<break>
<break>    int m = heights.size();
<break>    int n = heights[0].size();
<break>
<break>    vector<vector<int>> cost(m, vector<int>(n, 1e9));
<break>
<break>    cost[0][0] = 0;
<break>
<break>    // Defining directions.
<break>    vector<vector<int>> directions;
<break>    directions.push_back({0, 1});
<break>    directions.push_back({0, -1});
<break>    directions.push_back({1, 0});
<break>    directions.push_back({-1, 0});
<break>
<break>    // Creating priority_queue.
<break>    priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;
<break>    q.push({0, 0, 0});
<break>    int endX = m - 1;
<break>    int endY = n - 1;
<break>
<break>    while (q.size())
<break>    {
<break>        vector<int> temp = q.top();
<break>        q.pop();
<break>        int time = temp[0];
<break>        int i = temp[1];
<break>        int k = temp[2];
<break>
<break>        if (i == endX && k == endY)
<break>        {
<break>            return time;
<break>        }
<break>
<break>        for (int j = 0; j < 4; j++)
<break>        {
<break>            int x = directions[j][0];
<break>            int y = directions[j][1];
<break>
<break>            int nxt_i = x + i;
<break>            int nxt_k = k + y;
<break>
<break>            if (0 <= nxt_i && nxt_i < m && 0 <= nxt_k && nxt_k < n)
<break>            {
<break>                int nxt_time = max(time, abs(heights[nxt_i][nxt_k] - heights[i][k]));
<break>
<break>                if (nxt_time < cost[nxt_i][nxt_k])
<break>                {
<break>                    // Updating cost.
<break>                    cost[nxt_i][nxt_k] = nxt_time;
<break>                    q.push({nxt_time, nxt_i, nxt_k});
<break>                }
<break>            }
<break>        }
<break>    }
<break>}```

Rank 4 (Murari) - C++ (g++ 5.4) Solution

```<break>vector<vector<int>> del = {{0,1},{0,-1},{-1,0},{1,0}};
<break>
<break>int minimumTimeTakingPath(vector<vector<int>> &heights)
<break>{
<break>    int n = heights.size(),m = heights[0].size();
<break>    vector<vector<int>> dp(n,vector<int>(m,1e9));
<break>    priority_queue<pair<int,pair<int,int>> > pq;
<break>    pq.push({0,{0,0}});
<break>    dp[0][0] = 0;
<break>    while(!pq.empty()) {
<break>        int diff = -pq.top().first,x = pq.top().second.first,y = pq.top().second.second;
<break>        pq.pop();
<break>
<break>        for(int i = 0; i < 4 ;i++){
<break>            int nx = del[i][0]+x,ny = del[i][1]+y;
<break>            if(nx>=n or ny>=m or ny<0 or nx<0)  continue;
<break>            int ndiff = max(diff, abs(heights[nx][ny]-heights[x][y]));
<break>
<break>            if(ndiff<dp[nx][ny]){
<break>                dp[nx][ny] = ndiff;
<break>                pq.push({-ndiff,{nx,ny}});
<break>            }
<break>        }
<break>    }
<break>    return dp[n-1][m-1];
<break>}```

Rank 5 (AkashSingh3031) - C++ (g++ 5.4) Solution

```# include <bits/stdc++.h>
<break>
<break>int minimumTimeTakingPath(vector<vector<int>>& heights){
<break>
<break>    int m = heights.size();
<break>    int n = heights[0].size();
<break>
<break>    vector<vector<int>> cost(m, vector<int>(n, 1e9));
<break>
<break>    cost[0][0] = 0;
<break>
<break>    vector<vector<int>> directions;
<break>    directions.push_back({0, 1});
<break>    directions.push_back({0, -1});
<break>    directions.push_back({1, 0});
<break>    directions.push_back({-1, 0});
<break>
<break>    priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;
<break>    q.push({0, 0, 0});
<break>    int endX = m - 1;
<break>    int endY = n - 1;
<break>    while(q.size()){
<break>
<break>        vector<int> temp = q.top();
<break>        q.pop();
<break>        int time = temp[0];
<break>        int i = temp[1];
<break>        int k = temp[2];
<break>
<break>        if(i == endX && k == endY){
<break>            return time;
<break>        }
<break>
<break>        for(int j = 0; j < 4; j++){
<break>            int x = directions[j][0];
<break>            int y = directions[j][1];
<break>
<break>            int nxt_i = x + i;
<break>            int nxt_k = k + y;
<break>
<break>            if(0 <= nxt_i && nxt_i < m && 0 <= nxt_k && nxt_k < n){
<break>                int nxt_time = max(time, abs(heights[nxt_i][nxt_k] - heights[i][k]));
<break>
<break>                if(nxt_time < cost[nxt_i][nxt_k]){
<break>                    cost[nxt_i][nxt_k] = nxt_time;
<break>                    q.push({nxt_time, nxt_i, nxt_k});
<break>                }
<break>            }
<break>        }
<break>    }
<break>
<break>}```

Explore and share your solution to get the valuable insights from the coding community.

Solve today's POTD Challenge
Codestudio
Published On 10-Dec-2023
7 views
0 replies
Weekend Coding Test x GDSC Srinivas Institute of Technology, Mangalore | 9 Dec, 2023 | Post Contest Discussion
Contests and hackathons

Hey Ninjas!

This is a post to ask doubts and share your logic for solving questions from Weekend Coding Test x GDSC Srinivas Institute of Technology, Mangalore! 😄

If you face any issues during the contest, do let us know by replying below. ✌️

How was your experience in the contest or got any feedback? Let us know here (link)

Codestudio
Published On 09-Dec-2023
49 views
0 replies
Weekend Contest 103 | 9 Dec, 2023 | Post Contest Discussion
Contests and hackathons

Hey Ninjas!

This is a post to ask doubts and share your logic for solving questions from Weekend Contest 103! 😄
You can also view the rankings for the contest here (link).

If you face any issues during the contest, do let us know by replying below. ✌️

How was your experience in the contest or got any feedback? Let us know here (link)

Codestudio
Published On 09-Dec-2023
9236 views
27 replies
Top Solutions | Equal Sum
Interview problems

Here are the top solutions of POTD Challenge.

Rank 1 (CodeMystique) - C++ (g++ 5.4) Solution

```#include <bits/stdc++.h>
<break>
<break>int equalSum(vector<int> token)
<break>
<break>{
<break>
<break>int sum = accumulate(token.begin(),token.end(),0), n = token.size(), curr = 0;
<break>
<break>for(int i=0;i<n;i++){
<break>
<break>if(curr == sum-curr-token[i])return i;
<break>
<break>curr+=token[i];
<break>
<break>}
<break>
<break>return -1;
<break>
<break>}```

Rank 2 (HUNTERMARCHI) - C++ (g++ 5.4) Solution

```int equalSum(vector<int> token)
<break>
<break>{
<break>
<break>    // Write your code here
<break>
<break>    int x=token.size();
<break>
<break>    if(x==1){
<break>
<break>        return 0;
<break>
<break>    }
<break>
<break>    vector<int>preSum(x,0);
<break>
<break>    vector<int>postSum(x,0);
<break>
<break>    for(int i=1;i<x;i++){
<break>
<break>        preSum[i]=token[i-1]+preSum[i-1];
<break>
<break>    }
<break>
<break>    for(int i=x-2;i>=0;i--){
<break>
<break>        postSum[i]=postSum[i+1]+token[i+1];
<break>
<break>    }
<break>
<break>    int ans=-1;
<break>
<break>    for(int i=0;i<x;i++){
<break>
<break>        if(preSum[i]==postSum[i]){
<break>
<break>            return i;
<break>
<break>        }
<break>
<break>    }
<break>
<break>    return -1;
<break>
<break>}
<break>
<break>```

Rank 3 (pankajsharma223) - C++ (g++ 5.4) Solution

```/*
<break>    Time Complexity: O(N)
<break>    Space Complexity: O(1)
<break>
<break>    where 'N' is the number of checkpoints.
<break>*/
<break>
<break>int equalSum(vector<int> token)
<break>{
<break>    int n = token.size();
<break>    int sum_token = accumulate(token.begin(), token.end(), 0);
<break>    int token_alex = 0;
<break>
<break>    for (int i = 0; i < n; i++)
<break>    {
<break>        int token_rome = sum_token - token_alex - token[i];
<break>
<break>        if (token_alex == token_rome)
<break>        {
<break>            return i;
<break>        }
<break>
<break>        token_alex = token_alex + token[i];
<break>    }
<break>
<break>    // If no suitable checkpoint found
<break>    return -1;
<break>}
<break>```

Rank 4 (krishna_kumar) - C++ (g++ 5.4) Solution

```/*
<break>    Time Complexity: O(N)
<break>    Space Complexity: O(1)
<break>
<break>    where 'N' is the number of checkpoints.
<break>*/
<break>
<break>int equalSum(vector<int> token)
<break>{
<break>    int n = token.size();
<break>    int sum_token = accumulate(token.begin(), token.end(), 0);
<break>    int token_alex = 0;
<break>
<break>    for (int i = 0; i < n; i++)
<break>    {
<break>        int token_rome = sum_token - token_alex - token[i];
<break>
<break>        if (token_alex == token_rome)
<break>        {
<break>            return i;
<break>        }
<break>
<break>        token_alex = token_alex + token[i];
<break>    }
<break>
<break>    // If no suitable checkpoint found
<break>    return -1;
<break>}
<break>```

Rank 5 (Rohit Mishra) - C++ (g++ 5.4) Solution

```#include <bits/stdc++.h>
<break>int equalSum(vector<int> token)
<break>{
<break>    // Write your code here
<break>
<break>    int n = token.size();
<break>    vector<int> alex(n,0);
<break>    vector<int> rome(n,0);
<break>
<break>    alex[0] = token[0];
<break>    for(int i  = 1 ;i<n; i++){
<break>       alex[i] = token[i] + alex[i-1];
<break>
<break>    }
<break>
<break>    rome[n-1] = token[n-1];
<break>    for( int i = n-2;i>=0; i--){
<break>       rome[i] = token[i]+ rome[i+1];
<break>    }
<break>
<break>    for(int i = 0;i<n;i++){
<break>        if(alex[i] ==  rome[i]){
<break>           return i;
<break>        }
<break>    }
<break>   return -1;
<break>}
<break>```

Explore and share your solution to get the valuable insights from the coding community.

Solve today's POTD Challenge
Codestudio
Published On 09-Dec-2023
80 views
0 replies
Top Solutions | Equal Sum
Interview problems

Here are the top solutions of POTD Challenge.

Rank 1 (CodeMystique) - C++ (g++ 5.4) Solution

```#include <bits/stdc++.h>
<break>
<break>int equalSum(vector<int> token)
<break>
<break>{
<break>
<break>int sum = accumulate(token.begin(),token.end(),0), n = token.size(), curr = 0;
<break>
<break>for(int i=0;i<n;i++){
<break>
<break>if(curr == sum-curr-token[i])return i;
<break>
<break>curr+=token[i];
<break>
<break>}
<break>
<break>return -1;
<break>
<break>}```

Rank 2 (HUNTERMARCHI) - C++ (g++ 5.4) Solution

```int equalSum(vector<int> token)
<break>
<break>{
<break>
<break>    // Write your code here
<break>
<break>    int x=token.size();
<break>
<break>    if(x==1){
<break>
<break>        return 0;
<break>
<break>    }
<break>
<break>    vector<int>preSum(x,0);
<break>
<break>    vector<int>postSum(x,0);
<break>
<break>    for(int i=1;i<x;i++){
<break>
<break>        preSum[i]=token[i-1]+preSum[i-1];
<break>
<break>    }
<break>
<break>    for(int i=x-2;i>=0;i--){
<break>
<break>        postSum[i]=postSum[i+1]+token[i+1];
<break>
<break>    }
<break>
<break>    int ans=-1;
<break>
<break>    for(int i=0;i<x;i++){
<break>
<break>        if(preSum[i]==postSum[i]){
<break>
<break>            return i;
<break>
<break>        }
<break>
<break>    }
<break>
<break>    return -1;
<break>
<break>}
<break>
<break>```

Rank 3 (pankajsharma223) - C++ (g++ 5.4) Solution

```/*
<break>    Time Complexity: O(N)
<break>    Space Complexity: O(1)
<break>
<break>    where 'N' is the number of checkpoints.
<break>*/
<break>
<break>int equalSum(vector<int> token)
<break>{
<break>    int n = token.size();
<break>    int sum_token = accumulate(token.begin(), token.end(), 0);
<break>    int token_alex = 0;
<break>
<break>    for (int i = 0; i < n; i++)
<break>    {
<break>        int token_rome = sum_token - token_alex - token[i];
<break>
<break>        if (token_alex == token_rome)
<break>        {
<break>            return i;
<break>        }
<break>
<break>        token_alex = token_alex + token[i];
<break>    }
<break>
<break>    // If no suitable checkpoint found
<break>    return -1;
<break>}
<break>```

Rank 4 (krishna_kumar) - C++ (g++ 5.4) Solution

```/*
<break>    Time Complexity: O(N)
<break>    Space Complexity: O(1)
<break>
<break>    where 'N' is the number of checkpoints.
<break>*/
<break>
<break>int equalSum(vector<int> token)
<break>{
<break>    int n = token.size();
<break>    int sum_token = accumulate(token.begin(), token.end(), 0);
<break>    int token_alex = 0;
<break>
<break>    for (int i = 0; i < n; i++)
<break>    {
<break>        int token_rome = sum_token - token_alex - token[i];
<break>
<break>        if (token_alex == token_rome)
<break>        {
<break>            return i;
<break>        }
<break>
<break>        token_alex = token_alex + token[i];
<break>    }
<break>
<break>    // If no suitable checkpoint found
<break>    return -1;
<break>}
<break>```

Rank 5 (Rohit Mishra) - C++ (g++ 5.4) Solution

```#include <bits/stdc++.h>
<break>int equalSum(vector<int> token)
<break>{
<break>    // Write your code here
<break>
<break>    int n = token.size();
<break>    vector<int> alex(n,0);
<break>    vector<int> rome(n,0);
<break>
<break>    alex[0] = token[0];
<break>    for(int i  = 1 ;i<n; i++){
<break>       alex[i] = token[i] + alex[i-1];
<break>
<break>    }
<break>
<break>    rome[n-1] = token[n-1];
<break>    for( int i = n-2;i>=0; i--){
<break>       rome[i] = token[i]+ rome[i+1];
<break>    }
<break>
<break>    for(int i = 0;i<n;i++){
<break>        if(alex[i] ==  rome[i]){
<break>           return i;
<break>        }
<break>    }
<break>   return -1;
<break>}
<break>```

Explore and share your solution to get the valuable insights from the coding community.

Solve today's POTD Challenge
Codestudio
Published On 09-Dec-2023
80 views
0 replies