Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, How can I print out the results for this Python Class ? Given an array A of sorted integers and another non negative integer

Hi,

How can I print out the results for this Python Class ?

Given an array A of sorted integers and another non negative integer k, find if there exists 2 indices i and j such that A[i] - A[j] = k, i != j.

Example:

Input :

 A : [1 3 5] k : 4 

Output : YES

as 5 - 1 = 4

Return 0 / 1 ( 0 for false, 1 for true ) for this problem

Try doing this in less than linear space complexity.

class Solution: # @param A : list of integers # @param B : integer # @return an integer def diffPossible(self, A, B): dif = 0 for i in xrange(0, len(A) - 1): for j in xrange(i + 1, len(A)): dif = A[j] - A[i] if dif == B: return 1 if dif > B: break return 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions