Greater than Neighbor

CONCEPTS USED:

Arrays

DIFFICULTY LEVEL:

Easy

PROBLEM STATEMENT(SIMPLIFIED):

Given an array A of N elements, your task is to print all those indexes that have values greater than its left and right neighbors. In case of extreme indexes like 0 and N-1, check with their single neighbor.

See original problem statement here

For Example:

SOLVING APPROACH:

The idea is to traverse the array from left and for each index, check if the value at it is greater than its neighbor. If Yes print its index. If no such index is found, simply print -1.

SOLUTIONS:

#include < stdio.h >

int main()
{
  int t; scanf("%d", &t);

  while(t--){
    int n; scanf("%d", &n);
    int arr[n];

    for(int i=0; i arr[i+1]){
                printf("%d ", i);
                flag = 1;
            }
        }

        else if(i == n-1){
            if(arr[i] > arr[i-1]){
                printf("%d ", i);
                flag = 1;
            }
        }
        else{
            if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){
                printf("%d ", i);
                flag = 1;
            }
        }  
    }
    if(flag == 0)
      printf("-1");
    printf("\n");
  }

  return 0;
}

#include < bits/stdc++.h >
  #include 
  using namespace std;

  int main()
  {
    int t; cin >> t;

    while(t--){
      int n; cin >> n;
      int arr[n];

      for(int i=0; i> arr[i];
      }

      int flag = 0;
      for(int i=0; i arr[i+1]){
              cout << i << " ";
              flag = 1;
            }
        }

        else if(i == n-1){
            if(arr[i] > arr[i-1]){
              cout << i << " ";
              flag = 1;
            }
        }

        else{
            if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){
                cout << i << " ";
                flag = 1;
            }
        } 
      }

      if(flag == 0)
        cout << "-1";
      cout << endl;
    }

    return 0;
}

import java.util.*;
  import java.io.*;

  public class Main {
    public static void main(String args[]) throws IOException {

      Scanner sc = new Scanner(System.in);
      int t = sc.nextInt();

      while(t != 0){
        int n = sc.nextInt();
        int arr[] = new int[n];

        for(int i=0; i arr[i+1]){
              System.out.print(i + " ");
              flag = 1;
            }
          }

          else if(i == n-1){
            if(arr[i] > arr[i-1]){
              System.out.print(i + " ");
              flag = 1;
            }
          }
          else{
            if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){
                System.out.print(i + " ");
                flag = 1;
            }
          }
        }
        if(flag == 0)
          System.out.print("-1");
        System.out.println();

        t--;
      }

    }
  }

# your code goes heren = int(input())
arr = list(map(int,input().split()))
flag = 0

for i in range(n):
	if i == 0:
		if arr[i] > arr[i + 1]:
			print(i, end = " ")
			flag = 1
	elif i == n - 1:
		if arr[i] > arr[i - 1]:
			print(i, end = " ")
			flag = 1
	else:
		if arr[i] > arr[i - 1] and arr[i] > arr[i + 1]:
			print(i, end = " ")
			flag = 1

if flag == 0:
	print(-1)


Space Complexity: O(1)

[forminator_quiz id="484"]

This article tried to discuss Arrays. Hope this blog helps you understand and solve the problem. To practice more problems on Arrays you can check out MYCODE | Competitive Programming.

Leave a Reply

Your email address will not be published. Required fields are marked *