Skip to main content

Posts

Showing posts from March, 2020

Sub array with given Sum

Task: Find the continuous sub-array, from given an array of size N non negative integers, which sum is equal to given S. Example: Input: 1 5 12 1 2 3 7 5 Output: 2 4 // starting and ending positions of sub-array. Here is the code: Program in C++: #include<iostream> using namespace std; int main()  { int t; cin>>t; while(t--) {     int n,s;     cin>>n>>s;     int a[n];     for(int i=0;i<n;i++)     {         cin>>a[i];     }     // Implementaion of Logic     int sas=0,i=0,c=0;     for(int j=0;j<n;j++)     {         if(sas+a[j]<=s)         {             sas+=a[j];         }         else         {             sas+=a[j];             while(sas>s)             {                 sas-=a[i];                 i++;             }         }         if(sas==s)         {             cout<<i+1<<" "<<j+1<<endl;             c=1;             break;         }     }

Help a Thief

Task:  A thief wants to steal as much as gold coins from gold mine. In the gold mine N number of gold boxes are there, in every box consisting of  Ai gold plates, with consisting of Aj gold coins in each plate. Find the maximum number of gold coins can thief steal. Example: Input: 1 3 // Number of gold plates Thief can steal. 3 // Gold Boxes 1 3 2 2 3 1 // Gold plates and same number of Gold Coins on each plate. Output: 7 Here is the Code: Program in C++: #include <iostream> using namespace std; int maxPosition(int n,int *a) {     int p,max=0;     for(int i=1;i<n;i=i+2)     {         if(max<a[i])         {             max=a[i];             p=i;         }     }     return p; } int main() { int t; scanf("%d\n",&t); while(t--) {     int nT;     scanf("%d\n",&nT);     int n;     scanf("%d\n",&n);     int a[2*n];     for(int i=0;i<2*n;i++)     {         scanf("%d\n",&a[i]);     }     int p,gc=

Infix to Postfix

Task: Convert the given string from Infix to Postfix. Example: Input: 1 A*(B+C)/D Output: ABC+*D/ Here is the Code: Program in C++: #include <iostream> #include <bits/stdc++.h> using namespace std; int prec(char c) {     if(c=='^')     {         return 3;     }     else if(c=='*'||c=='/')     {         return 2;     }     else if(c=='+'||c=='-')     {         return 1;     }     else     {         return -1;     } } int infixToPostfix(string s) {     stack<char> st;     for(int i=0;i<s.length();i++)     {         if(isalpha(s[i]))         {             cout<<s[i];         }         else if(s[i]==')')         {             while(st.top()!='(')             {                 cout<<st.top();                 st.pop();             }             st.pop();         }         else         {             if(st.empty()==true)             {                 st.push(s[i]);             }             else          

Reverse words in a given String

Task: Reverse the entire string without reverse individual words, which is consisting of words with separated by dots. Example: Input: i.like.this.program.very.much Output: much.very.program.this.like.i Here is the Code: Program in C++: #include <iostream> #include <bits/stdc++.h> using namespace std; string reverseWords(string s) {     stack<char> st;     int c=0;     //reverse individual words     for(int i=0;i<s.length();i++)     {         if(s[i]=='.')         {             //back to starting position             //of current word             i=i-c;             while(st.empty()==false)             {                 s[i]=st.top();                 st.pop();                 i++;             }             c=0;         }         else         {             st.push(s[i]);             c++;         }     }     int i = s.length()-c;     while(st.empty()==false)     {         s[i]=st.top();         st.pop();         i++;     }     //reverse entire string     for(i

Next Larger Element

Task: Given an array of N distinct elements, Find the next greater element for each element of the array in order of their appearance in the array. If no such element exists, print -1. Example: Input: 1 4 1 3 2 4 Output: 3 4 4 -1 Here is the Code: Program in C++:   #include<iostream> #include<bits/stdc++.h> using namespace std; int nextLargerElement(int n,long long *a) {     stack<long long> st;     for(int i=n-1;i>=0;i--)     {         while(st.empty()==false&&st.top()<a[i])         {             st.pop();         }         long long t = a[i];         if(st.empty())         {             a[i]=-1;         }         else         {             a[i]=st.top();         }         st.push(t);     }     for(int i=0;i<n;i++)     {         cout<<a[i]<<" ";     } } int main()  { int t; cin>>t; while(t--) {     int n;     cin>>n;     long long a[n];     for(int i=0;i<n;i++)     {         cin>>a[i];