Question

Hi, I have programming problem related to array, sorting, and. swap operation

A non-empty array A consisting of N integers is given. You can perform a single swap operation in array A. This operation tak

For example, given: A[] 1 A[1] = 5 A[2] = 3 A[3] = 3 A[4] = 7 the function should return True, as explained above. On the oth

Assume that: • Nis an integer within the range [1..100); • each element of array A is an integer within the range (1..1,000,0

Thank you,

Best Regards..

0 0
Add a comment Improve this question Transcribed image text
Answer #1

# Code for checking logic
def solution(A):
   temp_A = []
   N = len(A)
   count_diff = 0
   for item in A:
       temp_A.append(item)
   temp_A.sort()
  
   # Comparing 2 arrays
   for i in range(N):
       if A[i] != temp_A[i]:
           count_diff = count_diff + 1
   if count_diff == 0 or count_diff == 2:
       return True
   else:
       return False


# Declaring array of integers
A = [1,5,3,3,7]
#A = [1,3,5]
#A = [1,3,5,3,4]
result = solution(A)
if result:
   print("Yes array can be sorted with atmost one swap")
      
else:
   print("No array cannot be sorted with atmost one swap")

Add a comment
Know the answer?
Add Answer to:
Hi, I have programming problem related to array, sorting, and. swap operation Thank you, Best Regards.....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi, I have Java programming problem: Please solve using Java. Thanks. Best Regards. In the army,...

    Hi, I have Java programming problem: Please solve using Java. Thanks. Best Regards. In the army, each soldier has an assigned rank. A soldier of rank X has to report to (any) soldier of rank X + 1. Many soldiers can report to the same superior. Write a function: class Solution { public int solution(int[] ranks); } that, given an array ranks consisting of soldiers' ranks, returns the number of soldiers who can report to some superior. Examples: 1. Given...

  • Hi, I have Python programming problem as follow: Thank you. Best Regards. Write a function solution...

    Hi, I have Python programming problem as follow: Thank you. Best Regards. Write a function solution that, given two integers A and B, returns a string containing exactly A letters 'a' and exactly B letters 'b' with no three consecutive letters being the same in other words, neither "aaa" nor "bbb" may occur in the returned string). Examples: 1. Given A = 5 and B = 3, your function may return "aabaabab". Note that "abaabbaa" would also be a correct...

  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

  • Hi, I need help in solving Python programming problem below: Thank you. Best Regards When John...

    Hi, I need help in solving Python programming problem below: Thank you. Best Regards When John gambles at the casino, he always uses a special system of tactics that he devised himself. It's based on always betting in one of two ways in each game: • betting exactly one chip (to test his luck); • betting all-in (he bets everything he has). Wins in the casino are paid equal to the wager, so if he bets C chips and wins,...

  • Parvati is given an array of integers. She is asked to return the sum of all...

    Parvati is given an array of integers. She is asked to return the sum of all the two-digit numbers in the array. Please help her to perform this task. Write a function: class Solution { public int solution (int[] A); } that, given an array A consisting of N integers, returns the sum of all two-digit numbers. For example, given A = [1, 1000, 80, -91], the function should return -11 (as the two-digit numbers are 80 and -91). Given...

  • the programming language is in java Problem 2 You are given an array A with n...

    the programming language is in java Problem 2 You are given an array A with n distinct elements. Implement an (n log n)-time algorithm that creates an array B where all elements are in range from 0 to n - 1 and where the order of elements is the same as in A. That is, 0 has the same index in B as the smallest element in A, 1 has the same index in B as the second smallest element...

  • Using Java solve recursively without the use of loops or modifying the method signatures. /** *...

    Using Java solve recursively without the use of loops or modifying the method signatures. /** * Given a sorted input array a, return a sorted array of size a.length + 1, * consisting of all elements of array a and integer i. * * @param a an array that is sorted in a non-descending order * @param i an integer * @return a sorted array of size a.length + 1, consisting of all elements of * array a and integer...

  • 6. Consider the following algorithm, where P is an array containing random numbers. The function swap(v1,v2)...

    6. Consider the following algorithm, where P is an array containing random numbers. The function swap(v1,v2) will swap the values stored in the variables v1 and v2. Note that % is the modulus operation, and will return the integer remainder r of a/b, i.e., r-a%b Require: Array P with n > 0 values 1: i-1, j-n-l 2: while i<=j do for a=i to j by i do 4: 5: 6: 7: if Pla>Pat 11 and Pla]%2--0 then swap(Plal, Pla+1l) end...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT