Wednesday, March 8, 2023
HomeSoftware DevelopmentDiscover the worth of Okay after changing each index of the array...

Discover the worth of Okay after changing each index of the array by |ai – Okay|


Given an array, arr[] containing n integers, the duty is to seek out an integer (say Okay) such that after changing every index of the array by |ai  – Okay| the place ( i ∈ [1, n]), ends in a sorted array. If no such integer exists that satisfies the above situation then return -1.

Examples:

Enter: arr[ ] = [10, 5, 4, 3, 2, 1]
Output: 8
Clarification: Upon performing the operation |ai-8|, we get [2, 3, 4, 5, 6, 7] which is sorted.

Enter: arr[ ] = [1, 2, 3, 4, 5, 6] 
Output: 0
Clarification: Because the array is already sorted so the worth of Okay can be 0 on this case.

Strategy: The issue may be solved based mostly on the next commentary:

Observations:

For the array to be sorted every pair of adjoining parts must be sorted. Meaning few instances come up if we take look after specific ai and ai+1 and people are as follows:

  • Let (ai < ai+1 ), so the next inequality come up: 
    • If (Okay ≤ai) then upon  (ai – Okay ≤ ai+1 – Okay) the weather shall be as it’s (ai < ai+1).
    • If (Okay ≥ ai) then upon  ( Okay – ai ≤ Okay – ai+1 ) the weather shall be as it’s (ai > ai+1).
    • So, Okay must be halfway between ai and ai+1 that’s Okay must be Okay ≤ (ai + ai+1)/2 .
  • Equally for (ai > ai+1) the worth of ok can be Okay ≥ (ai + ai+1)/2 
  • Lastly we’ll take the minimal of all for which (Okay < ai) and most of all for which (Okay > ai).

Comply with the steps talked about beneath to implement the thought:

  • Initialize two variables l and r for the 2 values of ok defined above.
  • Iterate over the array and verify if (ai < ai+1) then retailer in r the minimal of r up to now and the current worth of Okay.
  • Else if, (ai > ai+1) shops it in l the utmost of l up to now and the current worth of Okay.
  • Lastly if (l > r) return -1;
  • Else, return l 

Under is the Implementation of the above method: 

C++

// C++ code for the above method:
#embody <bits/stdc++.h>
utilizing namespace std;

// Perform to seek out Okay
void findk(int a[], int n)
{

    // Initializing the 2 variables
    int l = 0, r = 1e9;

    for (int i = 0; i < n - 1; i++) {

        // If (a[i] < a[i+1]) then take
        // mimimum and retailer in variable

        if (a[i] < a[i + 1]) {
            r = min(r, (a[i] + a[i + 1]) / 2);
        }

        // If (a[i]>a[i+1]) then take
        // most and retailer in
        // seperate variable
        else if (a[i] > a[i + 1]) {
            l = max(l, (a[i] + a[i + 1] + 1) / 2);
        }
    }

    if (l > r) {
        cout << "-1";
    }

    else

        cout << l << endl;
}

// Driver operate
int principal()
{
    int arr[] = { 10, 5, 4, 3, 2, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);

    // Perform Name
    findk(arr, n);
    return 0;
}

Java

// Java Implementation

import java.util.*;

class GFG {
    public static void principal(String[] args)
    {
        int[] arr = { 10, 5, 4, 3, 2, 1 };
        int n = arr.size;
        discover(arr, n);
    }

    public static void discover(int[] arr, int n)
    {
        // Initializing the 2 variables
        int l = 0;
        int r = 1000000000;
        for (int i = 0; i < n - 1; i++) {

            // If (a[i]<a[i+1]) then take mimimum and retailer in variable)

            if (arr[i] < arr[i + 1]) {
                r = Math.min(r, (arr[i] + arr[i + 1]) / 2);
            }

            // If (a[i]>a[i+1]) then take most and retailer in seperate variable)

            else if (arr[i] > arr[i + 1]) {
                l = Math.max(l,
                             (arr[i] + arr[i + 1] + 1) / 2);
            }
        }

        if (l > r) {
            System.out.println(-1);
        }
        else {
            System.out.println(l);
        }
    }
}

Time Complexity: O(n), iterating the loop for as soon as solely.
Auxiliary House: O(1), no further house is used.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

situs slot gacor provider terbaik agen toto slot terpercaya 2023 agen toto togel terpercaya 2023 situs toto togel pasaran resmi terbaik bandar toto macau pasaran resmi toto togel bandar toto slot gacor 4d 2023 bo togel online pasaran terlengkap sepanjang masa bo toto slot terlengkap sepanjang masa situs toto togel 2023 bet 100 perak daftar toto slot dan toto togel 2023 bermain toto togel dengan bet hanya 100 perak daftar toto slot bonus new member terpercaya bermain toto slot pelayanan 24 jam nonstop agen slot gacor 4d hadiah terbesar bandar toto slot provider terbaik toto slot gacor 4d hingga toto togel toto togel pasaran resmi terpercaya bo togel online terbaik 2023 agen togel online terbesar 2023 situs togel online terpercaya 2023 bo togel online paling resmi 2023 toto togel pasaran togel hongkong resmi situs slot online pasti gacor agen slot online anti rungkad bo slot online deposit tanpa potongan situs toto togel dan toto slot bonus new member situs toto slot gacor 4d bo toto slot gacor 4d bo toto slot gacor dari toto togel 4d bo toto slot 4d terpercaya bo toto slot terpercaya toto macau resmi dari toto togel 4d agen togel terbesar dan situs toto slot terpercaya bandar toto togel dan slot online 2023 bo slot gacor terbaik sepanjang masa winsortoto winsortoto bo toto togel situs toto situs toto togel terpercaya situs toto slot terpercaya situs slot gacor 4d terbaik sepanjang masa agen toto togel dan situs toto slot terpercaya situs toto togel dan agen toto slot terpercaya bandar toto togel tersedia pasaran toto macau resmi agen toto togel bet 100 perak deposit 10rb ltdtoto