diff --git a/README.md b/README.md index 2ebad2e1b..d6d32033c 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,11 @@ This project is licensed under the [MIT License](LICENSE). ## Contributors + + +![Contributors](https://opencollective.com/codeharborhub/contributors.svg?button=false&avatarHeight=50&width=600) ## Chat with us diff --git a/dsa-solutions/gfg-solutions/0003-kadane's-algorithm.md b/dsa-solutions/gfg-solutions/0003-kadane's-algorithm.md new file mode 100644 index 000000000..12ee2ecc8 --- /dev/null +++ b/dsa-solutions/gfg-solutions/0003-kadane's-algorithm.md @@ -0,0 +1,270 @@ +--- +id: kadane's-algorithm +title: Kadane's Alogrithm (Geeks for Geeks) +sidebar_label: 0003 - Kadane's Algorithm +tags: + - intermediate + - Array + - Dynamic Programming + - Data Structure + - Algorithms + +description: "This is a solution to the Kadane's Algorithm problem on Geeks for Geeks." +--- + +This tutorial contains a complete walk-through of the Kadane's Algorithm problem from the Geeks for Geeks website. It features the implementation of the solution code in three programming languages: Python ,C++ ,java. + +## Problem Description + +Given an array Arr[] of N integers, find the contiguous sub-array (containing at least one number) which has the maximum sum and return its sum. + +## Examples + +**Example 1:** + +``` +Input: +N = 5 +Arr[] = {1,2,3,-2,5} +Output: +9 +Explanation: +Max subarray sum is 9 +of elements (1, 2, 3, -2, 5) which +is a contiguous subarray. +``` + +**Example 2:** + +``` +Input: +N = 4 +Arr[] = {-1,-2,-3,-4} +Output: +-1 +Explanation: +Max subarray sum is -1 +of element (-1) +``` + +## Your Task + +You don't need to read input or print anything. Your task is to complete the function `maxSubarraySum()` which takes Arr[] and N as input parameters and returns the sum of subarray with maximum sum. + +Expected Time Complexity: $O(N)$ +Expected Auxiliary Space: $O(1)$ + +## Constraints + +1. $(1 \leq N \leq 10^6)$ +2. $(-10^7 \leq A[i] \leq 10^7)$ + +## Solution Approach + +### Brute Force Approach + +#### Intuition: +Try all possible subarrays by using nested loops and pick the subarray which has the maximum sum. + +#### Implementation: +1. Keep an answer variable to store the maximum subarray sum. +2. Run a loop(i). +3. Run a nested loop from i till the end of the array. +4. Generate all subarrays starting from the ith index and compare its sum with the answer and update the answer with the maximum one. +5. Return the answer. + +#### Code (C++): + +```cpp +class Solution{ + public: + // arr: input array + // n: size of array + //Function to find the sum of contiguous subarray with maximum sum. + long long maxSubarraySum(int arr[], int n){ + + // Your code here + long long ans=arr[0]; + for(int i=0;i { return (
-

Level Up Skills with CodeHarborHub

+ + Level Up Skills with CodeHarborHub + + {/*

Level Up Skills with CodeHarborHub

*/} {
@@ -50,14 +64,14 @@ const HeaderContent = () => { @@ -97,14 +111,14 @@ const HeaderImage = () => { return ( diff --git a/src/components/HomePage/header.css b/src/components/HomePage/header.css index 36c8343e7..f53d02c6f 100644 --- a/src/components/HomePage/header.css +++ b/src/components/HomePage/header.css @@ -316,4 +316,8 @@ .float-animation { animation: float 2s ease-in-out infinite; -} \ No newline at end of file +} + + + + diff --git a/src/pages/index.module.css b/src/pages/index.module.css index 2338c3c1c..852de9fb4 100644 --- a/src/pages/index.module.css +++ b/src/pages/index.module.css @@ -34,6 +34,7 @@ .section { padding: 1rem; } + /* scrollbar.css */ ::-webkit-scrollbar { width: 10px; @@ -49,4 +50,24 @@ } ::-webkit-scrollbar-thumb:hover { background: darkgrey; -} \ No newline at end of file +} +.scrollToBottomButton { + position: fixed; + bottom: 20px; + right: 20px; + top: auto; + background-color: var(--ifm-color-primary); + color: #fff; + border: none; + border-radius: 50%; + padding: 10px; + box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1); + cursor: pointer; + z-index: 1000; +} + +.scrollToBottomButton:hover { + background-color: var(--ifm-color-primary); + box-shadow: 0px 6px 8px rgba(0, 0, 0, 0.2); +} + diff --git a/src/pages/index.tsx b/src/pages/index.tsx index fa9a0c62c..9cfb100b4 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -1,6 +1,6 @@ import clsx from "clsx"; // import Link from "@docusaurus/Link"; -import React from "react"; +import React, { useState, useEffect } from "react"; import useDocusaurusContext from "@docusaurus/useDocusaurusContext"; import Layout from "@theme/Layout"; import Heading from "@theme/Heading"; @@ -12,6 +12,7 @@ import Header from "../components/HomePage/Header"; import Tweet from "../components/Tweet"; import Tweets, { type TweetItem } from "../data/tweets"; import { motion } from "framer-motion"; +import { FaArrowDown } from "react-icons/fa"; function TweetsSection(): React.JSX.Element { const tweetColumns: TweetItem[][] = [[], [], []]; @@ -23,14 +24,14 @@ function TweetsSection(): React.JSX.Element {
@@ -44,14 +45,14 @@ function TweetsSection(): React.JSX.Element {
{tweetColumns.map((tweetItems, i) => ( { + window.scrollTo({ + top: document.documentElement.scrollHeight, + behavior: "smooth", + }); + setShowScrollButton(false); + }; + + const handleScroll = () => { + if (window.scrollY === 0) { + setShowScrollButton(true); + } + }; + + useEffect(() => { + window.addEventListener("scroll", handleScroll); + return () => { + window.removeEventListener("scroll", handleScroll); + }; + }, []); + return ( @@ -97,28 +121,28 @@ export default function Home(): React.JSX.Element { @@ -126,20 +150,29 @@ export default function Home(): React.JSX.Element { + + {showScrollButton && ( + + )} );