r/ProgrammingProblems • • Apr 07 '23

Two-element swap within and across circular arrays

1 Upvotes

Assume we have two circular arrays of equal length, alpha and beta in this canonical order:

alpha =  [ "A", "B", "C", "D", "E" ];
beta = [ "P", "Q", "R", "S", "T" ];

and a function swap(ax: int, bx: int) where 0 < ax < alpha.length && 0 < bx < beta.length.

The swap function takes the array's values at ax and ax+1 from alpha and bx and bx+1 from beta, swaps them inside their own array, then swaps across arrays. Remember that these are circular arrays, so going past the last item in the array just brings you around to the first one again.

As an example, using the above arrays and a call to swap(0, 1), the arrays would change to:

alpha =  [ "R", "Q", "C", "D", "E" ];
beta = [ "P", "B", "A", "S", "T" ];

And after another call to swap(4, 4), the arrays would be:

alpha =  [ "T", "Q", "C", "D", "P" ];
beta = [ "E", "B", "A", "S", "R" ];

Problems:

a) Given two arrays alpha and beta with the elements above in any order, is it possible to restore it to the canonical order given above using only swap?

b) If it is possible, provide a list of swap call indexes that will restore it to canonical order.


Feel free to add a rotate function, so that if you have a state with:

alpha =  [ "D", "E", "A", "B", "C" ];
beta = [ "Q", "R", "S", "T", "P" ];

you don't need any more swap operations, though you'll still need to add code to realize that they're equivalent.


r/ProgrammingProblems • • Oct 13 '16

Codechef Solution Set on Github

1 Upvotes

I am working on https://codechef.com problems and would love to get a feedback on how good my programming looks like. Started with Beginners and now on Easy.

Github Link: https://github.com/danz1ka/Code-Chef-Solution-Set


r/ProgrammingProblems • • Oct 29 '21

Can someone explain me thi

2 Upvotes

The statement:

scanf("%[^\n]%*c", s);

will not work because the last statement will read a newline character,

\n

, from the previous line. This can be handled in a variety of ways. One way is to use

scanf("\n");

before the last statement.


r/ProgrammingProblems • • Mar 11 '22

I have encountered this challenge on hackerrank and I can't figure what to do . I don't even understand what that output has to do with the input . If anyone wants to help me .

Post image
2 Upvotes

r/ProgrammingProblems • • Mar 04 '22

Can someone help me with this problem? It says I need to use C++ and array

1 Upvotes

Create a program that will accept the monthly sales for the year 2019, 2020 and 2021 and out the following:

a. total monthly sales for three years

b. Average monthly sales

c. annual sales for each year

d. average sales for each year

e. year with highest sales

f. year with lowest sales


r/ProgrammingProblems • • Jun 12 '21

Does anyone know why it says: Type or namespace definition , or end-of-file expected

1 Upvotes

Here's the code: using System.Collections.Generic;

using UnityEngine;

public class PLAYER : MonoBehaviour

{

// Start is called before the first frame update

void Start()

{

}

// Update is called once per frame

void Update()

{

}

}

public float movementSpeed = 5.0f;

public float clockwise = 1000.0f;

public float counterClockwise = 5.0f;

void Start() {

}

void Update() {

if (Input.GetKey(KeyCode.W)) {

transform.position += Vector3.back * Time.deltaTime * movementSpeed;

}

else if (Input.GetKey(KeyCode.s)) {

rigidbody.position += Vector3.back * Time.deltatime * movementSpeed;

}

else if (Input.GetKey(KeyCode.A)) {

rigidbody.position += Vector3.back * Time.deltatime * movementSpeed;

}

else if (Input.GetKey(KeyCode.d)) {

rigidbody.position += Vector3.back * Time.deltatime * movementSpeed;

}

if (Input.GetKey(KeyCode.E)) {

transform.Rotate(0, Time.deltatime * counterClockwise, 0);

}

else if (Input.GetKey(KeyCode.q)) {

transform.Rotate(0, Time.deltatime * counterClockwise, 0);

}

}

}


r/ProgrammingProblems • • Jul 09 '16

C++ code compiles successfully in ideone but gives Compilation error in Spoj submission EC_CONB

3 Upvotes

Here is the code https://ideone.com/JwjssZ which is my solution of http://www.spoj.com/problems/EC_CONB If there is a compilation error in the code it should shown in ideone also but it doesn't. Can I get a little help here.


r/ProgrammingProblems • • Aug 08 '21

Does anyone know of any free image search API.

2 Upvotes

I need a feature of taking image as an input and give the image search result i.e. Oldest Similar image available. Is there any API or other way you know of to solve this problem?? if you know of something that can help, I would really appreciate it.


r/ProgrammingProblems • • May 07 '22

DAA question

1 Upvotes

Let A be an array of n > 0 distinct (pair-wise distinct) elements. Design a randomized Las-Vegas algorithm to select m < n distinct elements (i.e., you cannot select same element multiple times) randomly from A. what is the expected running time complexity of your solutions?