Mikel  Okuneva

Mikel Okuneva

1597921200

Count of replacements required to make the sum of all Pairs of given type

Given an integer array arr of length N and an integer K, the task is to find the number of array elements to be replaced by a value from the range [1, K] such that each pair (arr[i], arr[N – 1 – i] have equal sum.

Examples :

Input:_ arr[] = {1, 2, 2, 1}, K = 3_

Output:_ 1_

Explanation:

Replace arr[0] with 3, so that the array becomes {3, 2, 2, 1}.

Now, it can be seen that arr[0] + arr[3] = arr[1] + arr[2] = 4.

_Input: _arr[] = {1, 2, 1, 2, 1, 2}

Output:_ 0_

Explanation:

No need to make any changes as arr[0] + arr[5] = arr[1] + arr[4] = arr[2] + arr[3] = 3

Naive Approach :

The simplest approach to this problem could be like iterate for all possible values of X, which can be any number in the range 1 to 2*K, and find the number of operation require to achieve pair sum equal to X. And finally return the minimum numbers of replacement from all operations.

Time Complexity :_ O (N * K)_

Auxiliary Space :_ O (1)_

Efficient Approach: The above approach can be optimized by the following observations:

  • X can clearly take values in the range [2, 2 * K].
  • Considering pairs arr[i] and arr[N – i – 1], it can be observed that the sum of any pair can be made equal to X in 0, 1 or 2 replacements.
  • Let the maximum of these two numbers be mx and minimum be mn. In one replacement, the sum of this pair will be in the range [mn + 1, mx + K].
  • The pair for which mx is less than **( X – K) **and mn is greater than or equal to X will need 2 replacements.
  • Simply insert mx and mn for all pairs in two different vectors, sort them and apply binary search to find the number of pairs for which 2 replacements are required.
  • Find the pairs which do not need any replacement by using Map by storing the frequencies of each sum.
  • Finally number of pairs which require one replacement can be calculated by the equation (N / 2 – (pairs which need 2 replacements + pairs which need no replacement) ).
  1. Follow these steps to find the solve the problem:
  2. Find maximum and minimum for all pairs arr [ i ]arr [ N – i – 1 ] and store them in max_values and min_values vectors respectively. Also store the frequencies of sum of all such pairs in a map.
  3. Sort the vectors max_values and min_values.
  4. Iterate from X = 2 to X = 2 * K, and for every value of X find the total number of replacements as described in the approach above. Update the answer as:

answer = min ( answer, 2 * (number of pairs for which we need to make 2 replacements) + (number of pairs for which we need to make one replacement) ).

Illustration :

arr[] = { 1, 2, 2, 1 }, K = 3

max_values = {1, 2}

min_values = {1, 2}

map = {{2, 1}, {4, 1}}

At X = 4, minimum replacement is required, that is, only 1 replacement, either conversion arr[0] to 3 or arr[3] to 3, is required to make the sum of all pairs equal to 4.

Below is the implementation of the above approach:

  • C++

// C++ Progrm to implement the

// above approach

#include <bits/stdc++.h>

#define int long long int

**using** **namespace** std;

**const** **int** inf = 1e18;

// Function to find the minimum

// replacements required

**int** minimumReplacement(``**int**``* arr, **int** N,

**int** K)

{

**int** ans = inf;

// Stores the maximum and minimum

// values for every pair of

// the form arr[i], arr[n-i-1]

vector<``**int**``> max_values;

vector<``**int**``> min_values;

// Map for storing frequencies

// of every sum formed by pairs

map<``**int**``, **int**``> sum_equal_to_x;

**for** (``**int** i = 0; i < N / 2; i++) {

// Minimum element in the pair

**int** mn = min(arr[i], arr[N - i - 1]);

// Maximum element in the pair

**int** mx = max(arr[i], arr[N - i - 1]);

// Incrementing the frequency of

// sum encountered

sum_equal_to_x[arr[i]

+ arr[N - i - 1]]++;

// Insert minimum and maximum values

min_values.push_back(mn);

max_values.push_back(mx);

}

// Sorting the vectors

sort(max_values.begin(),

max_values.end());

sort(min_values.begin(),

min_values.end());

// Iterate over all possible values of x

**for** (``**int** x = 2; x <= 2 * K; x++) {

// Count of pairs for which x > x + k

**int** mp1

= lower_bound(max_values.begin(),

max_values.end(), x - K)

- max_values.begin();

// Count of pairs for which x < mn + 1

**int** mp2

= lower_bound(min_values.begin(),

min_values.end(), x)

- min_values.begin();

// Count of pairs requiring 2 replacements

**int** rep2 = mp1 + (N / 2 - mp2);

// Count of pairs requiring no replacements

**int** rep0 = sum_equal_to_x[x];

// Count of pairs requiring 1 replacement

**int** rep1 = (N / 2 - rep2 - rep0);

// Update the answer

ans = min(ans, rep2 * 2 + rep1);

}

// Return the answer

**return** ans;

}

// Driver Code

int32_t main()

{

**int** N = 4;

**int** K = 3;

**int** arr[] = { 1, 2, 2, 1 };

cout << minimumReplacement(arr, N, K);

**return** 0;

}

Time Complexity:_ O(NlogN)_

Auxiliary Space:_ O(N)_

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#arrays #mathematical #searching #binary search #frequency-counting

What is GEEK

Buddha Community

Count of replacements required to make the sum of all Pairs of given type
Mikel  Okuneva

Mikel Okuneva

1597921200

Count of replacements required to make the sum of all Pairs of given type

Given an integer array arr of length N and an integer K, the task is to find the number of array elements to be replaced by a value from the range [1, K] such that each pair (arr[i], arr[N – 1 – i] have equal sum.

Examples :

Input:_ arr[] = {1, 2, 2, 1}, K = 3_

Output:_ 1_

Explanation:

Replace arr[0] with 3, so that the array becomes {3, 2, 2, 1}.

Now, it can be seen that arr[0] + arr[3] = arr[1] + arr[2] = 4.

_Input: _arr[] = {1, 2, 1, 2, 1, 2}

Output:_ 0_

Explanation:

No need to make any changes as arr[0] + arr[5] = arr[1] + arr[4] = arr[2] + arr[3] = 3

Naive Approach :

The simplest approach to this problem could be like iterate for all possible values of X, which can be any number in the range 1 to 2*K, and find the number of operation require to achieve pair sum equal to X. And finally return the minimum numbers of replacement from all operations.

Time Complexity :_ O (N * K)_

Auxiliary Space :_ O (1)_

Efficient Approach: The above approach can be optimized by the following observations:

  • X can clearly take values in the range [2, 2 * K].
  • Considering pairs arr[i] and arr[N – i – 1], it can be observed that the sum of any pair can be made equal to X in 0, 1 or 2 replacements.
  • Let the maximum of these two numbers be mx and minimum be mn. In one replacement, the sum of this pair will be in the range [mn + 1, mx + K].
  • The pair for which mx is less than **( X – K) **and mn is greater than or equal to X will need 2 replacements.
  • Simply insert mx and mn for all pairs in two different vectors, sort them and apply binary search to find the number of pairs for which 2 replacements are required.
  • Find the pairs which do not need any replacement by using Map by storing the frequencies of each sum.
  • Finally number of pairs which require one replacement can be calculated by the equation (N / 2 – (pairs which need 2 replacements + pairs which need no replacement) ).
  1. Follow these steps to find the solve the problem:
  2. Find maximum and minimum for all pairs arr [ i ]arr [ N – i – 1 ] and store them in max_values and min_values vectors respectively. Also store the frequencies of sum of all such pairs in a map.
  3. Sort the vectors max_values and min_values.
  4. Iterate from X = 2 to X = 2 * K, and for every value of X find the total number of replacements as described in the approach above. Update the answer as:

answer = min ( answer, 2 * (number of pairs for which we need to make 2 replacements) + (number of pairs for which we need to make one replacement) ).

Illustration :

arr[] = { 1, 2, 2, 1 }, K = 3

max_values = {1, 2}

min_values = {1, 2}

map = {{2, 1}, {4, 1}}

At X = 4, minimum replacement is required, that is, only 1 replacement, either conversion arr[0] to 3 or arr[3] to 3, is required to make the sum of all pairs equal to 4.

Below is the implementation of the above approach:

  • C++

// C++ Progrm to implement the

// above approach

#include <bits/stdc++.h>

#define int long long int

**using** **namespace** std;

**const** **int** inf = 1e18;

// Function to find the minimum

// replacements required

**int** minimumReplacement(``**int**``* arr, **int** N,

**int** K)

{

**int** ans = inf;

// Stores the maximum and minimum

// values for every pair of

// the form arr[i], arr[n-i-1]

vector<``**int**``> max_values;

vector<``**int**``> min_values;

// Map for storing frequencies

// of every sum formed by pairs

map<``**int**``, **int**``> sum_equal_to_x;

**for** (``**int** i = 0; i < N / 2; i++) {

// Minimum element in the pair

**int** mn = min(arr[i], arr[N - i - 1]);

// Maximum element in the pair

**int** mx = max(arr[i], arr[N - i - 1]);

// Incrementing the frequency of

// sum encountered

sum_equal_to_x[arr[i]

+ arr[N - i - 1]]++;

// Insert minimum and maximum values

min_values.push_back(mn);

max_values.push_back(mx);

}

// Sorting the vectors

sort(max_values.begin(),

max_values.end());

sort(min_values.begin(),

min_values.end());

// Iterate over all possible values of x

**for** (``**int** x = 2; x <= 2 * K; x++) {

// Count of pairs for which x > x + k

**int** mp1

= lower_bound(max_values.begin(),

max_values.end(), x - K)

- max_values.begin();

// Count of pairs for which x < mn + 1

**int** mp2

= lower_bound(min_values.begin(),

min_values.end(), x)

- min_values.begin();

// Count of pairs requiring 2 replacements

**int** rep2 = mp1 + (N / 2 - mp2);

// Count of pairs requiring no replacements

**int** rep0 = sum_equal_to_x[x];

// Count of pairs requiring 1 replacement

**int** rep1 = (N / 2 - rep2 - rep0);

// Update the answer

ans = min(ans, rep2 * 2 + rep1);

}

// Return the answer

**return** ans;

}

// Driver Code

int32_t main()

{

**int** N = 4;

**int** K = 3;

**int** arr[] = { 1, 2, 2, 1 };

cout << minimumReplacement(arr, N, K);

**return** 0;

}

Time Complexity:_ O(NlogN)_

Auxiliary Space:_ O(N)_

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#arrays #mathematical #searching #binary search #frequency-counting

Arvel  Parker

Arvel Parker

1593156510

Basic Data Types in Python | Python Web Development For Beginners

At the end of 2019, Python is one of the fastest-growing programming languages. More than 10% of developers have opted for Python development.

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Table of Contents  hide

I Mutable objects

II Immutable objects

III Built-in data types in Python

Mutable objects

The Size and declared value and its sequence of the object can able to be modified called mutable objects.

Mutable Data Types are list, dict, set, byte array

Immutable objects

The Size and declared value and its sequence of the object can able to be modified.

Immutable data types are int, float, complex, String, tuples, bytes, and frozen sets.

id() and type() is used to know the Identity and data type of the object

a**=25+**85j

type**(a)**

output**:<class’complex’>**

b**={1:10,2:“Pinky”****}**

id**(b)**

output**:**238989244168

Built-in data types in Python

a**=str(“Hello python world”)****#str**

b**=int(18)****#int**

c**=float(20482.5)****#float**

d**=complex(5+85j)****#complex**

e**=list((“python”,“fast”,“growing”,“in”,2018))****#list**

f**=tuple((“python”,“easy”,“learning”))****#tuple**

g**=range(10)****#range**

h**=dict(name=“Vidu”,age=36)****#dict**

i**=set((“python”,“fast”,“growing”,“in”,2018))****#set**

j**=frozenset((“python”,“fast”,“growing”,“in”,2018))****#frozenset**

k**=bool(18)****#bool**

l**=bytes(8)****#bytes**

m**=bytearray(8)****#bytearray**

n**=memoryview(bytes(18))****#memoryview**

Numbers (int,Float,Complex)

Numbers are stored in numeric Types. when a number is assigned to a variable, Python creates Number objects.

#signed interger

age**=**18

print**(age)**

Output**:**18

Python supports 3 types of numeric data.

int (signed integers like 20, 2, 225, etc.)

float (float is used to store floating-point numbers like 9.8, 3.1444, 89.52, etc.)

complex (complex numbers like 8.94j, 4.0 + 7.3j, etc.)

A complex number contains an ordered pair, i.e., a + ib where a and b denote the real and imaginary parts respectively).

String

The string can be represented as the sequence of characters in the quotation marks. In python, to define strings we can use single, double, or triple quotes.

# String Handling

‘Hello Python’

#single (') Quoted String

“Hello Python”

# Double (") Quoted String

“”“Hello Python”“”

‘’‘Hello Python’‘’

# triple (‘’') (“”") Quoted String

In python, string handling is a straightforward task, and python provides various built-in functions and operators for representing strings.

The operator “+” is used to concatenate strings and “*” is used to repeat the string.

“Hello”+“python”

output**:****‘Hello python’**

"python "*****2

'Output : Python python ’

#python web development #data types in python #list of all python data types #python data types #python datatypes #python types #python variable type

How does tinder make money?

Essential information regarding how do dating apps make money and how does tinder make money. Moreover, we present unique ways to make money through dating apps.

#how does tinder make money #how does bumble make money #how much money do dating apps make #how dating apps make money #how do dating apps make money

How much does an iOS or Android chat app cost to make?

Messaging is one of the most essential functions that smartphone users want to have at hand. Smartphone won’t be a must in our life if it has no chatting function. There is no one that doesn’t have WhatsApp, Viber, WeChat or Snapchat installed on his device. AppClues Infotech has a relevant experience crafting different messaging apps with top-notch technology stacks behind them, and we want to share our insights with you.

We have a team of professional Chat App Developers, experienced Whatsapp clone app developers who works hard on simple as well as complex problem and give their best out of it. Our Chat highly experienced app developers design an application which are elegant, feasible, easy accessible and capability to generate high traffic towards your website.

Ideal features in a Chat app:

  • Instant Messaging
  • Real time connectivity
  • Multimedia file transmission
  • Security
  • Push Notification
  • Quick search
  • Group Chat
  • Video and voice calling
  • Social Integration

The Cost to build an Chat app is between $12 to $15 per hour. The Cost is depends on complexity of a product and feature we need in chat app. The following three factors affect the final cost:

  • Technical complexity
  • The number of devices and OS
  • Custom designs and animations.

Benefits with AppClues Infotech:

  • Steady Mobile Chat App Development Service : Our chatting app development services aim at kickstarting and concluding the app development tasks reliably.
  • Affordable Chatting App Development : It is our vision AppClues Infotech to concentrate on the chat mobile app designing and development in the most affordable way.
  • Guaranteed WhatsApp Chat Clone Security : The specialization of our experts lies in securing the apps with some of the most robust features.
  • Quick Client Support: We at AppClues Infotech take it as our responsibility to provide quick client support in any of the ways required to them.

With its years of expertise in developing messaging/ chatting apps, the AppClues Infotech team of developers has now endeavored into chatting app development. Our sole aim with the messaging apps development is to bring people closer with instant messaging facilities. The app developers at our company have helped us achieve the goal with utmost delicacy.

#cost to make an ios chat app #cost to make an android chat app #cost to build a messaging app #make a messaging app #custom mobile chat app development #how to make a chat app

How much does it cost to make an app like Paytm/Groupon?

Today, every country is shifting towards digitization to build a cashless marketplace. Where ever you go maybe shops, restaurants, and many more places you will find mobile wallet apps like Paytm and PhonePay. From a small street merchant to big business are everyone is opting for mobile payments. All mobile wallet apps are enormously helpful to users as it eases the transaction of money.

If you are reading this answer means you are a very much interested business person interested in knowing about the development of an app like Paytm.

Now, we are going to know in-depth details about the development of an app like Paytm and the cost involved in the complete development process.

About Paytm

Paytm is India’s largest mobile payment platform founded in 2010 by Vijay Sekhar Sharma. Offering Mobile prepaid recharge/post-paid, utility bill payments, UPI payments, Money transfers, Online shopping, IRCTC train ticket booking, movie tickets, LIC premium payment, Electricity bill payment, DTH recharge, bus/flight booking, bank to bank transfer, metro card recharge and many more services. It is available on android, iOS and Windows smartphones.

Owned by One97 Communications, Paytm began operations with online mobile recharge and later expanded to online shopping with the similar kind of products that are sold by Flipkart, Amazon.

Key Features of Paytm

  • Mobile Recharge & Bill Payment
  • Pay online or Offline
  • Money Transfer
  • Book Train/Bus/Flight tickets

More on Paytm

  • Online shopping (Women/men fashion, Mobiles and Electronics, Home/Kitchen, Baby and Kids and more)
  • Deals/Discount Vouchers
  • DTH Recharge
  • Water, gas and electricity bill payment

Paytm Wallet Admin Panel

  • Engaging Dashboard
  • Managing offers and contact
  • QR code
  • Data synchronization
  • Cloud operations
  • Add/remove features
  • Managing the Booking calendar

Types of Mobile Wallet App:

  • A retail application
  • A dedicated payment app
  • PoS (Point of Sale) payments

How Much Does it Cost to make an app like Paytm?

The Paytm app development cost or any online Recharge app development depends on the following main factors such as:

App Platform (Android & iOS)
App development costs may differ from one platform to another. Comparatively, the development cost of an android app like Paytm is lower than iOS, as it is directly implemented without testing.

App Design
To design Paytm like an app, UX/UI plays a very crucial role, where users can experience the smoothness and flawlessness of the app. This kind of engaging features come with expensive cost.

UX/UI design(40-60hrs $1500-$3000)

App Features
It is defined as a total number of basic or advance features developed within the e-wallet app like Paytm.

App team:
For creating an app like Paytm, we need Project Manager, Requirement analyst, Developers, QA engineers, Mobile app developers (IOS and Android) and UI/UX designers. One more thing you must include is the geographical location and size of the company when you are choosing for the Paytm developers.

  • Developers in India: $10-80/hour
  • Eastern Europe: $30-150/hour
  • USA cost: $50-250/hour,

The charges will differ from a small company to a multinational company.

#make an app like paytm #cost to make an app like paytm #develop a mobile wallet app like paytm #make paytm clone app #make an app like groupon