• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Sequence Finder Dynamic Programming

January 28, 2018 by Dhaval Dave

Sequence Finder

Given two arrays A and B of equal size, you have to determine whether the following the sequence is possible or not.



i.e for n=2, out of the sequences –

  • A[0] + A[1]
  • A[0] – B[1]
  • – B[0] + A[1]
  • – B[0] – B[1]

Whether a sequence with 0 (zero) value is possible or not.

Solution:

The question may look hard at first, but the solution just requires some modeling. Let’s see with the help of an example, suppose we have n=4 and the following sequence has value zero,

A[0] – B[1] – B[2] + A[3] = 0

Now take the B[ ] terms to RHS

A[0] + A[3] = B[1] + B[2]

Now add the missing A[ ] terms with same indices as B[ ] to both sides

A[0] + A[1] + A[2] + A[3] = A[1] + B[1] + A[2] + B[2]

Or

A[0] + A[1] + A[2] + A[3] = ( A[1] + B[1] ) + ( A[2] + B[2] )

Or

A[0] + A[1] + A[2] + A[3] = T[1] + T[2]    (Here- T[i]=A[i]+B[i])

Or

Summation of A[ ] = Summation of some terms of T[ ]

Or

Summation of A[ ] (CONSTANT) = Summation of a subset of T[ ]

 

Thus we have reduced our complicated summation problem to a problem of Subset Sum problem which can solved using Dynamic programming. To see solution of Subset sum problem using Dynamic Programming click here.

So, to implement this we need to the summation of A[ ] in a variable say sum and create a array T such that T[i] = A[i] + B[i].

NOTE- We can also add missing B[ ] terms in place of A[ ] terms, in that case we would be storing summation of B[ ] and T would be same.

 

Code for Sequence Finder

Implemented in C++

#include <bits/stdc++.h>
#define ll int64_t
#define MX 1000007
#define MX1 1003
using namespace std;
 
int t[MX],dp[MX];                 // Here t stores the set and dp stores whether ith element is possible or not i.e if dp[i]=1 then it is possible or not
 
bool subset_sum(int n,int sum){          // this function is used to determine whether the subset with sum is possible or not
 
  dp[0]=1;
  for(int i=0;i<n;i++){
    if(t[i]<=sum) for(int j=sum;j>=t[i];j--){
      dp[j]|=dp[j-t[i]];
    }
  }
 
  return dp[sum];
 
}
 
int main()
{
  int n,sum=0;
  cin>>n;
  for(int i=0;i<n;i++){ cin>>t[i];							// here we input array A						
    sum+=t[i];							// and store its sum of elements
  }
  for(int i=0;i<n;i++){ int a;cin>>a;						// here we input array B and add it to existing array T i.e A
    t[i]+=a;
  }
 
  (subset_sum(n,sum))?cout<<"Possible":cout<<"Not Possible";
  return 0;
 
}

Example

Sequence Finder Dynamic Programming Article is Published by Arnab Ghosh.
If you want to be a content writer with Gohired.in, please write at career@gohired.in or at admin@gohired.in.

Similar Articles

Filed Under: Algorithm, Array, Interview Questions, Uncategorized Tagged With: Array, Dynamic Programming

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

Get Minimum element in O(1) from input numbers or Stack

How strtok() Works

Spanning Tree

Max Sum in circularly situated Values

Adobe Interview Questions 8 month Exp

Stickler thief

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

Length of the longest substring without repeating characters

Urban Ladder Written Test.

Trie Dictionary

Knight Tour Problem (Graph – Breadth First Search)

Circular Linked List

Trapping Rain Water

Find next greater number with same set of digits

25 horses 5 tracks Find 3 fastest puzzle

Get K Max and Delete K Max in stream of incoming integers

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Reversal of LinkedList

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

C++ OOPs Part2

Find loop in Singly linked list

Coin Collection Dynamic Programming

Rectangular chocolate bar Create at least one piece which consists of exactly nTiles tiles

Binary Tree in Java

FizzBuzz Solution C C++

SAP Hiring Off-Campus General Aptitude

Maximum size of square sub matrix with all 1’s in a binary matrix

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

SAP Off Campus Hiring_ March 2015 Verbal Skills

Copyright © 2025 · Genesis Framework · WordPress · Log in