Arrays 1: Subarray with given sum | Must Do Coding Questions | Interview Preparation | GeeksForGeeks

preview_player
Показать описание
This video contains Must Do Coding Questions for Companies like Amazon, Microsoft, Adobe, …
from geeksforgeeks platform.

website link:

Topics covered in this video series are:
1.Arrays
2.String
3.Linked List
4.Stack and Queue
5.Tree and BST
6.Heap
7.Recursion
8.Hashing
9.Graph
10.Greedy
11.Dynamic Programming
12.Divide and Conquer
13.Backtracking
14.Bit Magic

Arrays:
1. Subarray with given sum:
Given an unsorted array A of size N that contains only non-negative integers, find a continuous sub-array which adds to a given number S.

Example:
Input:
N = 5, S = 12
A[] = {1,2,3,7,5}
Output: 2 4
Explanation: The sum of elements from 2nd position to 4th position is 12.

Code Link:

Intro Music :
Рекомендации по теме
Комментарии
Автор

Watched multiple videos for this problem but found your approach and code the best !!!! ❤

hypnotic_dude
Автор

This is the second problem concept i watched on your channel after equilibrium point . You explain in such a way that it gets stuck in mind forever !! Thanks bhai ji...

saurabhchaturvedi
Автор

Bhai maine c m ye program banaya aur mera ye life ka pehla accha question tha maine apne logic se thoda coplex sa kiya h but accha laga karke thnx a lot brother

suryapratap
Автор

Keep going bro..complete the must do list.

divyanshuchaudhari
Автор

Replace this for correct output😅❤❤❤
If(start <=I && sum == s)
{
Flag = 1;
Break;
}

aditya
Автор

Whats the time complexity for this? Also we would need to add another condition for a corner case where S is 0. Just before you start the for loop.
if(S == 0)
return -1;

shubh_
Автор

in problems ko solve karne se or kaha se practice kar sakte hai?kyuki pehle kabhi yeh problems mene dekhi nahi hai....

pmahaur
Автор

Can I know what is the count step im not getting it sir ??

leelaanuhyak
Автор

import java.util.*;

public class Hello {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Read the number of elements in the array
System.out.print("Enter the number of elements: ");
int n = sc.nextInt();

// Create an array to store the elements
int[] arr = new int[n];

// Prompt user to enter elements
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt(); // Read each element and store it in the array
}

// Assume S is the sum or difference we want to find in pairs
int S = 10; // Example value, you can change this as needed

// Iterate through the array to find pairs
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if ((arr[i] + arr[j] == S) || (arr[i] - arr[j] == S) || (arr[j] - arr[i] == S)) {
// Print the indices of the elements that form the pair
System.out.println("Pair found at indices: " + i + " and " + j);
}
}
}

// Close the Scanner
sc.close();
}
}
will this code work for this ?

ronyuniverse
Автор

hey i am writting this code but it saying {segmentation error}

class solutin
{
public:
vector<int> subarraySum(int arr[], int n, long long s)
{
{
int value= 0;
int i, start=0, found = 0;
for( i=0; i<n; i++)
{
value=value + arr[i];

while(value>s)
{
value = value - arr[start];
start++;

}
if(value == s)
{
cout<<start<<i;
found = 1;
break;
}

}
if(!found){
cout<<"no such array";
}
}
}
};

roshangupta
Автор

Thank you so much bro you helped a lot

levi-lbdp
Автор

The range is index 1 to index 3 (1, 3) 2+3+7 I think. Please explain if I am wrong.

vinothkannans
Автор

the complexity should be O(N) please optimize the solution

Deepakkumar-krfk
Автор

THe code has a progblem add if(sum ==s) break;after the loop

siddheshb.kukade
Автор

replace this one for correct output
if(start <= i && sum == s)
{
flag = 1;
break;
}
because if s is 0 your output can be wrong ❤😢😊

aditya
Автор

Bhai suppose whichever sum we are finding is not present in these array then toh it will stuck in while loop

Knowledgeduniya
Автор

It's not working for second example which is 10 15. Is anyone explain me about this?

ourcreations
Автор

bro, Arithmatic ke coding round questions karvado SDE | & || keliye medium level ke please

PIYUSH-lzzq
Автор

For java is there any ds Coding playlist

RichaBehera
Автор

Sir agar frequency thodi jada hoti to Jada better rehta

vaishalidangi